[英]MySQL - Why in this case a secondary index doesn't reduce the time of this query?
[英]MySQL query - why doesn't it use the index in this case?
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”上有一个索引,Explain中的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.