问题:查询慢。
table1
大约有 5 000 行table2
大约有 50 000 行- 时间戳格式是
int(11)
- MySQL - 20 秒(带索引)
PostgreSQL - 0,04 秒(带索引)
SELECT * FROM table1 LEFT JOIN table2 ON table2_timestamp BETWEEN table1_timestamp - 500 AND table1_timestamp + 500 ;
任何人都可以帮我优化这个 MySQL 查询吗?
解释:
1 SIMPLE a index a 9 2 Using index
1 SIMPLE b index b b 9 5 Using index
表:
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`table1_timestamp` bigint(20) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
INDEX `a` (`table1_timestamp`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=3
ROW_FORMAT=COMPACT
;
CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`table2_timestamp` bigint(20) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
INDEX `a` (`table2_timestamp`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=3
ROW_FORMAT=COMPACT
;