Assuming time indexed, you can get the following entry almost for free:
SELECT * FROM table WHERE time > 1250710000 ORDER BY time LIMIT 1
And if I'm not mistaken, then the same should apply to the previous record, MySQL will just read the index in the reverse order. Use the UNION of the two, order them by diff and voila! The result will look like this:
SELECT * FROM ( (SELECT *, ABS(time - 1250710000) AS time_diff FROM table WHERE time > 1250710000 ORDER BY time ASC LIMIT 1) UNION ALL (SELECT *, ABS(time - 1250710000) AS time_diff FROM table WHERE time < 1250710000 ORDER BY time DESC LIMIT 1) ) AS tmp ORDER BY time_diff LIMIT 1
Ideally, instead of > and < you should use >= and <= and exclude the reference record using your primary identifier to account for records sharing the same timestamp.
Josh davis
source share