简体   繁体   English

如何使该查询运行更快?

[英]How can I make this query run faster?

I'm just wondering if theres anything I could do to make the query run a bit faster? 我只是想知道是否可以做些什么来使查询运行更快?

I have this query: 我有这个查询:

SELECT * FROM posts
WHERE posts.exists = 'n'
ORDER BY posts.ratecount DESC
LIMIT 0,100

Takes: 2.47s @ 0,100 | 需要:2.47秒@ 0,100 | 6.18s @ 500,100 6.18秒@ 500,100

This works but it tends to get rather slow as the limit increases (100,100 > 200,100 etc). 这可以工作,但是随着限制的增加(100,100> 200,100等),它往往会变得相当慢。

Using an index doesn't seem to help either: 使用索引似乎也无济于事:

SELECT * FROM posts
USE INDEX(ratecount_ca)
WHERE posts.exists = 'n'
ORDER BY posts.ratecount DESC
LIMIT 0,100

Takes: 8.59s @ 0,100 | 耗时:8.59秒@ 0,100 | 28.98s @ 500,100 28.98秒@ 500,100

Strangely enough, without the WHERE it works perfect. 奇怪的是,没有WHERE,它就可以完美地工作。

A descending index would most likely fix the issue but since that doesn't seem to be implemented yet, I need another option. 降序索引最有可能解决此问题,但是由于似乎尚未实现,因此我需要另一种选择。 Doing the WHERE after it's ordered would mostly likely speed it up too, but since I'm rather new to SQL I have no idea how to do :< 在订购后执行WHERE很有可能也会加快速度,但是由于我对SQL不太熟悉,所以我不知道该怎么做:<

What version of MySQL are you using? 您正在使用哪个版本的MySQL? v5.0 docs suggest there is a way to change index order. v5.0文档建议有一种更改索引顺序的方法。

I would suggest to create index on both columns. 我建议在两个列上都创建索引。 It should look something like: 它看起来应该像这样:

CREATE INDEX index_2_cols ON posts(exists, ratecount DESC);

既然您说省略where条件使其运行起来很快,那么我将尝试在posts表的exists字段上创建索引

CREATE INDEX ON posts_exists ON posts (exists(10));

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

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