gpt4 book ai didi

mysql - MySQL 中的基本查询出乎意料地慢

转载 作者:搜寻专家 更新时间:2023-10-30 23:32:07 26 4
gpt4 key购买 nike

我正在对包含 189,000 条记录的表运行基本选择。表结构为:

items

id - primary key
ad_count - int, indexed
company_id - varchar, indexed
timestamps

选择查询是:

select * 
from `items`
where `company_id` is not null
and `ad_count` <= 100
order by `ad_count` desc, `items`.`id` asc
limit 50

在我的生产服务器上,执行的 MySQL 部分需要 300 - 400 毫秒

如果我运行 explain,我会得到:

select type: SIMPLE 
table: items
type: range
possible_keys: items_company_id_index,items_ad_count_index
key: items_company_id_index
key_len: 403
ref: NULL
rows: 94735
Extra: Using index condition; Using where; Using filesort

在我们的应用程序中获取此数据时,我们将其分页为 50 组,但上面的查询是“第一页”

我不太熟悉剖析explain 查询。我在这里遗漏了什么吗?

最佳答案

具有不同排序顺序的 ORDER BY 子句可能会导致创建临时表和文件排序。低于(包括)v5.7 的 MySQL 根本不能很好地处理这种情况,实际上没有必要在 ORDER BY 子句中索引字段,因为 MySQL 的优化器永远不会使用它们。因此,如果应用程序的要求允许,最好对 ORDER BY 子句中的所有列使用相同的顺序。

所以在这种情况下:

order by `ad_count` desc, `items`.`id` asc

将成为:

order by `ad_count` desc, `items`.`id` desc

P.S,作为阅读更多信息的小提示 - 似乎 MySQL 8.0 将改变一些事情,这些用例在发布时可能会表现得更好。

关于mysql - MySQL 中的基本查询出乎意料地慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100208/

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