gpt4 book ai didi

MySQL 查询 - 为什么在这种情况下不使用索引?

转载 作者:行者123 更新时间:2023-11-29 14:09:38 26 4
gpt4 key购买 nike

MySQL 查询(5.1.61 版)

SELECT alerts.*,
devices.user_id
FROM alerts
left JOIN devices
ON alerts.device_id = devices.id
WHERE devices.name = 'myval'

“alerts.device_id”上有一个索引解释中的 MySQL 不使用索引,它显示类型“ALL”,行数是表中行的完整计数。

我不明白为什么它不使用这个索引。我错过了什么?

谢谢!

最佳答案

索引没有被选取,因为您没有在 select 或 where 子句中使用它。

您需要在device.name上添加索引

您可以做一件事来强制引擎考虑索引(如果它是主键)并且其值大于 0,例如:

SELECT alerts.*,
devices.user_id
FROM alerts
left JOIN devices
ON alerts.device_id = devices.id
WHERE devices.name = 'myval' and alerts.deviceid>0

关于MySQL 查询 - 为什么在这种情况下不使用索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723861/

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