2012-12-05 62 views
0

MySQL查询(版本61年1月5日)MySQL查询 - 为什么在这种情况下不使用索引?

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”和行为的完整计数表中的行。

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

谢谢!

+2

请发布EXPLAIN的输出。 –

+0

对不起,我简化了实际查询并错过了一些问题点!当我再次找到时会更新这个。 – iss42

回答

1

索引没有被选中,因为你没有使用它,既不在select中,也不在where子句中。

你需要的,如果是大于0,就像device.name

添加索引你可以做一两件事,迫使发动机要考虑指标,如果它是一个主键,它的值:

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