哪些字段应该一起索引?分组依据?订购依据?

发布于 2024-09-26 04:53:28 字数 948 浏览 1 评论 0原文

我正在尝试加快当前的查询速度:

SELECT * 
FROM `events` 
WHERE (field1 = 'some string' or field1 = 'some string') 
    and is_current = true 
GROUP BY event_id 
ORDER BY pub_date

这大约需要 30 秒。

field1 是 varchar(150)

我当前正在索引 field1、is_current、event_id、pub_data 慈善机构、pub_date、is_current 以及所有字段单独...

我真的不确定哪些字段应该一起索引,当我删除 order by 时,查询速度可达 8 秒左右,如果我同时删除 order by 和 group by,则它是不到1秒...

在这种情况下到底应该对什么进行索引以加快查询速度?

编辑: 我已经对修改后的查询运行了解释(不再包含分组依据):

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
    1   SIMPLE  events  range   is_current,field1_2,field1_3,field1_4,field1    field1_3    153     NULL    204336  Using where; Using filesort

这表明它正在使用关键 field1_3,即:field1 & is_current
虽然它没有使用包含这两个字段和 pub_date 的密钥(用于排序..?)

它也使用 FILESORT,这似乎是主要问题..

即使 pub_date 字段也被索引,但它为什么使用文件排序的任何想法(与其他字段)?

I'm trying to speed up a query which I currently have as:

SELECT * 
FROM `events` 
WHERE (field1 = 'some string' or field1 = 'some string') 
    and is_current = true 
GROUP BY event_id 
ORDER BY pub_date

this takes roughly 30seconds.

field1 is a varchar(150)

I'm currently indexing
field1, is_current, event_id, pub_data
charity, pub_date, is_current
and all the fields individually...

I'm really not sure what fields should be indexed together, when I remove the order by, the query speeds up to around 8 seconds, and if I removed both the order by and group by, it's less than 1 second...

What exactly should be indexed in this case to speed up the query?

Edit:
I've run explain on the modified query (which no longer includes the group by):

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
    1   SIMPLE  events  range   is_current,field1_2,field1_3,field1_4,field1    field1_3    153     NULL    204336  Using where; Using filesort

Which indicates it's using the key field1_3 which is: field1 & is_current
Although it's not using the key which includes those two fields and pub_date (for the ordering..?)

It's also using FILESORT which seems to be the main problem..

any ideas why it's using a filesort even though the pub_date field is also indexed (with the other fields)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-残月青衣踏尘吟 2024-10-03 04:53:28

所有内容(field1is_currentevent_idpub_date)都在一个索引中。 MySQL 在查询中只能对每个连接表使用一个索引。

使用 EXPLAIN 来查看执行此操作时会发生什么。

另外,顺便说一句 - 正如 KoolKabin 所说,* 很少是一个好主意。有时MySQL会复制临时表中的行;然后是沟通成本。你要求的越少,事情就会进展得越快。

更新:我实际上错了。对不起。首先,如果您的分组与顺序不同,您就无法充分利用索引。其次,您是否有一个索引,其中您的订购键 (pub_date) 位于第一个?如果没有,请尝试是否可以解决订购问题。

Everything, (field1, is_current, event_id, pub_date) in one index. MySQL can only use one index per joined table in a query.

Use EXPLAIN to see what happens when you do.

Also, an aside - as KoolKabin says, * is rarely a good idea. Sometimes MySQL will copy the rows in a temporary table; and then there are the communication costs. The less you ask from it the faster things will work.

UPDATE: I was actually wrong. Sorry. First off, you can't get full use of indexing if your grouping is different than your ordering. Second, do you have an index where your ordering key (pub_date) is first? If not, try if that fixes the ordering thing.

黄昏下泛黄的笔记 2024-10-03 04:53:28

任何想法为什么它使用文件排序,即使 pub_date 字段也被索引(与其他字段一起)?

这是因为 mysql 优化器正在尝试使用索引“field1”,而您希望数据按 pub_date 排序。如果你使用的是mysql 5.1(以下查询在早期版本中会报错),你可以强制mysql使用pub_date索引作为order by,像这样

SELECT * 
FROM `events` 
force index for order by (pub_date)
WHERE (field1 = 'some string' or field1 = 'some string') 
    and is_current = true 
GROUP BY event_id 
ORDER BY pub_date

any ideas why it's using a filesort even though the pub_date field is also indexed (with the other fields)?

This is because the mysql optimizer is trying to use index "field1" and you want the data ordered by pub_date. If you are using mysql 5.1 (the following query will give error in earlier versionn), you can force mysql to use the pub_date index for order by, something like this

SELECT * 
FROM `events` 
force index for order by (pub_date)
WHERE (field1 = 'some string' or field1 = 'some string') 
    and is_current = true 
GROUP BY event_id 
ORDER BY pub_date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文