0

MySQL query (ver 5.1.61)

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

There is an index on "alerts.device_id" MySQL in Explain does not use the index, it shows type "ALL" and rows being the full count of rows in the table.

I can't understand why it doesn't use this index. What am I missing?

Thanks!

2
  • 2
    Please post the output of EXPLAIN. Commented Dec 5, 2012 at 13:03
  • Sorry I over simplified the actual query and missed out the problem bits! Will update this when I find it again. Commented Dec 5, 2012 at 15:50

1 Answer 1

1

The index are not getting picked, because you have not used it, neither in the select nor in the where clause.

You need to add an index on device.name

You can do one thing to force the engine to consider the index if it is a primary key and its value if is more than 0, like :

SELECT alerts.*,
       devices.user_id
  FROM alerts
       left JOIN devices
          ON alerts.device_id = devices.id
 WHERE    devices.name = 'myval' and alerts.deviceid>0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.