我在mysql 5.6上,innoDB,这是查询:
SELECT *
FROM activities use index (user_id_start_time_end_time)
WHERE user_id=219439
AND end_time <= 1414524849
AND start_time >= 1413867600
ORDER BY end_time DESC
LIMIT 1000
从5.5升级后开始发生,大约需要5-10秒才能完成。 th user_id_start_time_end_time
是user_id,start_time和end_time的复合索引。
如果解释查询,我得到:
额外列中的 Using index condition; Using filesort
和合理的行数(~500)。
如果我取出ORDER BY end_time DESC LIMIT 1000
实际情况更糟,则查询会陷入sending data
状态,并在> 10秒内完成。
该表有大约150M行。
EDIT1:
SHOW CREATE TABLE
声明
CREATE TABLE `activities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`start_time` int(11) NOT NULL,
`end_time` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `start_time` (`start_time`),
KEY `end_time` (`end_time`),
KEY `user_id` (`user_id`),
KEY `user_id_start_time_end_time` (`user_id`,`start_time`,`end_time`)
) ENGINE=InnoDB AUTO_INCREMENT=193763922 DEFAULT CHARSET=utf8
(略加编辑)。
7.5 GB ram(在AWS RDS上)。
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 5839126528; in additional pool allocated 0
Dictionary memory allocated 374910
Buffer pool size 348224
Free buffers 7784
Database pages 318670
Old database pages 117550
Modified db pages 51295
Pending reads 18
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 8233948, not young 97502110
267.01 youngs/s, 1694.11 non-youngs/s
Pages read 11060592, created 116307, written 5005689
285.42 reads/s, 2.67 creates/s, 63.31 writes/s
Buffer pool hit rate 992 / 1000, young-making rate 7 / 1000 not 51 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 318670, unzip_LRU len: 0
I/O sum[144032]:cur[750], unzip sum[0]:cur[0]