简体   繁体   English

SELECT查询很慢(不需要索引),为什么这么慢?

[英]SELECT query is slow (no index needed), why is it so slow?

I have a table with over 1 million entries. 我的桌子上有超过100万个条目。

The problem is with the speed of the SELECT queries. 问题在于SELECT查询的速度。 This one is very fast: 这个非常快:

SELECT * 
  FROM tmp_pages_data 
 WHERE site_id = 14294

Showing rows 0 - 29 (1,273,042 total, Query took 0.0009 sec)

And this one is very slow: 而且这很慢:

SELECT * 
  FROM tmp_pages_data 
 WHERE page_status = 0

Showing rows 0 - 29 (15,394 total, Query took 0.3018 sec)

There is an index on the id column only, not needed in any of the selects. 仅id列上有一个索引,任何选择都不需要。 So there is no index on site_id or page status. 因此,没有site_id或页面状态的索引。

The 0.30 seconds query is very disturbing, especially when there are thousands requests. 0.30秒的查询非常令人不安,尤其是在有数千个请求时。

So how can this be possible? 那怎么可能呢? What can I do to see what's slowing it down? 我该怎么做才能看到速度变慢了?

What can I do to see what's slowing it down? 我该怎么做才能看到速度变慢了?

It's quite obvious what is slowing it down - as you've already pointed out you don't have an index on the page_status column, and you should have one. 很明显,这正在减慢它的速度-正如您已经指出的,您在page_status列上没有索引, 应该有一个索引。

The only surprise is that your first query is so fast without the index. 唯一的意外是没有索引,您的第一个查询是如此之快。 Looking at it more closely it seems that whatever client you are running these queries on is adding an implicit LIMIT 30 that you aren't showing in your question. 仔细观察一下,似乎无论您在运行这些查询的任何客户端上都添加了一个隐含的LIMIT 30 (您未在问题中显示)。 Because there are so many rows that match it doesn't take long to find the first 30 of them, at which point it can stop searching and return the result. 由于匹配的行太多,因此很快就可以找到它们的前30行,此时它可以停止搜索并返回结果。 However your second query returns fewer matching rows so it takes longer to find them. 但是,第二个查询返回的匹配行较少,因此查找它们所需的时间更长。 Adding the index would solve this problem and make your query almost instant. 添加索引将解决此问题并使查询几乎立即进行。

Short answer: add an index on the column page_status . 简短的答案:在page_status列上添加索引。

Ok, from our discussion in the comments we now know that the db somehow knows that the first query will returns all rows. 好的,从注释的讨论中我们现在知道db以某种方式知道第一个查询将返回所有行。 That's why it's so fast. 这就是为什么它这么快。

The second query is slow because it doesn't have an index. 第二个查询很慢,因为它没有索引。 OMG Ponies already stated that a normal index won't work because the value set is too small. OMG Ponies已经声明正常索引将不起作用,因为设置的值太小。 I'd just like to point you to 'bitmap indexes' . 我只想指出您“位图索引” I've not used them myself yet but they are known to be designed for exactly this case. 我还没有亲自使用过它们,但众所周知它们是专门为这种情况而设计的。

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

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