简体   繁体   English

MySQL搜索速度很慢,查询或索引错误?

[英]MySQL search very slow, bad query or indexes?

We have a table in an application, which is VASTLY HUGE. 我们的应用程序中有一个表格,它非常庞大。 Easily millions of rows. 轻松地百万行。

The structure is basically as follows: 结构基本上如下:

SERIES_ID | YEAR | DAY_SINCE_EPOCH | HOUR | MINUTE | VALUE

We have indexes on YEAR and DAY_SINCE_EPOCH. 我们在YEAR和DAY_SINCE_EPOCH上都有索引。

The problem is that certain kind of queries are very slow, such as: 问题是某些查询非常慢,例如:

SELECT
 ...
WHERE
   SERIES_ID = 3 AND
   DAY_SINCE_EPOCH < 16000 AND
   YEAR = 2012
ORDER BY 
   DAY_SINCE_EPOCH DESC,
   HOUR DESC,
   MINUTE DESC
LIMIT 1

This takes about 10 seconds in a table with 2M rows, and well over 20 seconds in a table with 18M rows. 在具有2M行的表中,这大约需要10秒,而在具有18M行的表中,则需要20秒以上。

The intent is to find the last record of series 3, before day 16000. The YEAR=2012 is there to speed up the lookup. 目的是在16000天之前找到系列3的最后一条记录。YEAR = 2012可以加快查找速度。

So I was wondering, do we have the indexes set up right? 所以我想知道,我们是否正确设置了索引? Perhaps it'd be faster without the year index? 也许没有年份指数会更快? Or with added SERIES_ID index or something like that? 或添加了SERIES_ID索引或类似内容?

Or just restructuring the query would help? 或者只是重组查询会有所帮助?

Any idea how to speed up the search will be welcome! 任何想法如何加快搜索将受到欢迎!

该索引可能会有所帮助:

> create index on your_table(series_id, day_since_epoch, year);

Check these conditions 检查这些条件

SERIES_ID = 3 AND
DAY_SINCE_EPOCH < 16000 AND
YEAR = 2012

when you are updating the table(eg. saving new records) then save the true/false info on a "bit" column then check for it just like 当您更新表格时(例如,保存新记录),然后将“ true / false”信息保存在“ bit”列上,然后像进行检查一样

where checked=1 .....

 SERIES_ID | YEAR | DAY_SINCE_EPOCH | HOUR | MINUTE | VALUE | checked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM