gpt4 book ai didi

mysql order by vs where子句性能

转载 作者:行者123 更新时间:2023-11-29 05:42:54 24 4
gpt4 key购买 nike

我有一个包含以下信息的数据库:

表格报告:166211条记录
表报告内容:166211条记录
表格公司:13188条记录

此查询需要 41.7324 秒来处理:

select rc.* from `reports` r
left join `report_content` rc on rc.`report`=r.`id`
left join `companies` c on c.`id`=r.`company`
where not isnull(r.`published`) and not r.`deleted`
order by rc.`company` asc
limit 50

因为这个查询需要 1.6146 秒来处理:
我添加了 rc.company != ''

select rc.* from `reports` r
left join `report_content` rc on rc.`report`=r.`id`
left join `companies` c on c.`id`=r.`company`
where not isnull(r.`published`) and not r.`deleted`
and rc.`company` != ''
order by rc.`company` asc
limit 50

我在 rc.company 上有基数为 11872 的全文索引所有其他子句/连接字段都有 btree 索引(主要是主索引)

为什么会这样?我应该在 varchar(255) 上使用全文/btree 索引吗?这个想法是没有 rc.company != ''

仅供引用,表是 MyISAM

注意:添加的条件不会改变结果,只是在条件中添加了rc.company(这样可以加快查询速度),不知是否优雅?


更新:谢谢 Frail,结果如下:

查询 A:

1   SIMPLE  r   range   published   published   9   NULL    156085  Using where; Using temporary; Using filesort
1 SIMPLE rc ref report report 4 database.r.id 1
1 SIMPLE c eq_ref PRIMARY PRIMARY 4 database.r.company 1 Using index

查询 B:

1   SIMPLE  rc  ALL     report,company  NULL    NULL    NULL    166339  Using where; Using filesort
1 SIMPLE r eq_ref PRIMARY,published PRIMARY 4 database.rc.report 1 Using where
1 SIMPLE c eq_ref PRIMARY PRIMARY 4 database.r.company 1 Using index

最佳答案

据我所知全文索引不是用来排序的。并且您正在使用公司列对结果进行排序。

我试着简单的给你解释一下全文:

id company
1 "brown fox"
2 "something of fox"

您的全文索引将为单词创建一个 btree:“brown, something, fox”,因此您可以匹配这些单词,但据我所知它不会帮助您排序。

因此,如果您正在使用全文索引来获取“狐狸”公司,请保留它。但为了排序目的,还要在 company 上放置一个 btree 索引。

关于mysql order by vs where子句性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5178316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com