最近我一直在让 MySQL 挂起特定的查询。我有一张包含 500,000 多条记录的表。这是正在运行的查询:
SELECT * FROM
items
WHERE (itemlist_id = 115626) ORDER BY tableOrder DESC LIMIT 1
这里是解释:
| 1 | SIMPLE | items | ALL | NULL | NULL | NULL | NULL | 587113 | Using where; Using filesort |
这是 process_list 条目:
| 252996 | root | localhost | itemdb | Query | 0 | Sorting result | SELECT * FROM
items
WHERE (itemlist_id = 115642) ORDER BY tableOrder DESC LIMIT 1 |
知道是什么导致此查询需要 10 分钟才能处理吗?当我手动运行它时,它很快就完成了。 (一组中的 1 行(0.86 秒))
谢谢
最佳答案
您需要在items (itemList_id, TableOrder)
上创建一个索引并稍微重写查询:
SELECT *
FROM items
WHERE itemlist_id = 115626
ORDER BY
itemlist_id DESC, tableOrder DESC
LIMIT 1
ORDER BY
中的第一个条件可能看起来是多余的,但它有助于 MySQL
选择正确的计划(不排序)。
关于MySQL排序结果需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2297748/