gpt4 book ai didi

mysql - 为什么 MySQL (MariaDB) 使用以下查询计算记录数超过 3 分钟?

转载 作者:行者123 更新时间:2023-11-29 01:35:07 25 4
gpt4 key购买 nike

我正在寻找原因和建议。我的表有大约 140 万行,当我运行以下查询时,它花了 3 多分钟。我添加计数只是为了显示结果。我真正的查询是没有计数的。

MariaDB [ams]> SELECT count(asin) FROM asins where asins.is_active = 1 
and asins.title is null and asins.updated < '2018-10-28' order by sortorder,id;

+-------------+
| count(asin) |
+-------------+
| 187930 |
+-------------+


1 row in set (3 min 34.34 sec)

结构

id int(9) Primary
asin varchar(25) UNIQUE
is_active int(1) Index
sortorder int(9) Index

如果您需要更多信息,请告诉我。提前致谢。

编辑用 EXPLAIN 查询

MariaDB [ams]> EXPLAIN SELECT asin FROM asins where asins.is_active = 1 and asins.title is null and asins.updated < '2018-10-28' order by sortorder,id;

enter image description here

最佳答案

数据库正在扫描所有行以回答查询。我想你有一张很大的 table 。

对于这个查询,ORDER BY 是不必要的(但它应该不会影响性能:

SELECT count(asin)
FROM asins
WHERE asins.is_active = 1 AND
asins.title is null AND
asins.updated < '2018-10-28' ;

那么你想要一个关于 (is_active, title, updated) 的索引。

关于mysql - 为什么 MySQL (MariaDB) 使用以下查询计算记录数超过 3 分钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53033341/

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