此查询的执行时间超过 2 秒(对于 10k 行)。是否可以优化此查询?
SELECT id, MIN(ABS(timestamp_a - timestamp_b))
FROM a
INNER JOIN b ON ( timestamp_a between (timestamp_b - 5 * 60)
AND (timestmap_b + 5 * 60) )
GROUP BY id
示例结果(id、timestamp_a、timestamp_b、diff):
1 1349878538 1349878539 1
2 1349878679 1349878539 2
3 1349878724 1349878539 1
5 1349878836 1349878539 1
6 1349878890 1349878641 1
表一
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`timestamp_a` bigint(20) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
INDEX `a` (`timestamp_a`) USING BTREE
)
表b
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`timestamp_b` bigint(20) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
INDEX `b` (`timestamp_b`) USING BTREE
)
两个表之间不相关 - 我从表 'a' 中搜索位于表 'b' 中的时间戳之间的记录。
编辑:简单的解决方案(运行非常快):
SELECT id, MIN(ABS(timestamp_a - timestamp_b))
FROM (SELECT id, timestamp, (timestamp - 5 * 60) timestamp_a, (timestamp + 5 * 60) timestamp_b) a
INNER JOIN b ON ( timestamp between timestamp_a AND timestamp_b )
GROUP BY id