sql—获取按日期排序的最后记录的最佳性能方法

ffdz8vbo  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(232)

我有一张超过6000万张唱片的table。这个表有510列。所有列都是双精度的。
我为日期字段创建了索引:

CREATE INDEX index_btree_date ON mytable USING BTREE (date);

表如下所示:

--------------------------------------------------------------
| id | date                | fk_system | col1 | col2 | col-n |
--------------------------------------------------------------
| 1  | 2020-08-05 15:00:00 | 1         | 1    | 2    | 3     |
--------------------------------------------------------------
| 2  | 2020-08-05 15:00:00 | 2         | 1    | 2    | 3     |
--------------------------------------------------------------
| 3  | 2020-08-05 15:01:00 | 1         | 1    | 2    | 3     |
--------------------------------------------------------------
| 4  | 2020-08-05 15:01:00 | 2         | 1    | 2    | 3     |
--------------------------------------------------------------
| 5  | 2020-08-05 15:02:00 | 1         | 1    | 2    | 3     |
--------------------------------------------------------------
| 6  | 2020-08-05 15:02:00 | 2         | 1    | 2    | 3     |
--------------------------------------------------------------
| 7  | 2020-08-05 15:03:00 | 1         | 1    | 2    | 3     |
--------------------------------------------------------------
| 8  | 2020-08-05 15:03:00 | 2         | 1    | 2    | 3     |
--------------------------------------------------------------

我尝试运行此查询:

SELECT t.id, t.date, t.fk_system, t.col1, t.col2, t.col3 
FROM mytable t 
WHERE t.fk_system = 106 
ORDER BY t.date DESC 
LIMIT 2880;

此查询运行时间超过5分钟,性能非常差。
有人能帮我吗?
谢谢!

wztqucjr

wztqucjr1#

对于此查询:

SELECT t.id, t.date, t.fk_system, t.col1, t.col2, t.col3
FROM mytable t
WHERE t.fk_system = 106
ORDER BY t.date DESC
LIMIT 2880;

你想要索引吗 (fk_system, date desc) . 索引,其中 date is first对这个查询没有帮助。

相关问题