Reputation: 197
The query is as below,
select *
from lab this_
inner join visits v1_ on this_.visit_id=v1_.id
v1_.id
is primary key in the query.
It takes more than 1 minute to complete.
Below is the plan.
id select_type table type possible_keys key
1 SIMPLE v1_ ALL <null> <null>
1 SIMPLE this_ ALL <null> <null>
Not sure why primary key is picked as key. Also type is ALL.
Upvotes: 1
Views: 2116
Reputation: 34285
Mysql may ignore an index during the excution of the query if it belives that the alternative plan is more efficient. A couple of points it considers:
The size of the tables. If the visits table is small, then there is not too much point in using the index.
Selectivity. You do join the 2 tables, however there is no filtering and you want all fields from both tables. This could mean that mysql has to return most of the records from the visits table anyway and the index covers only the id column. Therefore, mysql would be forced to scan through most of the records of the visits table anyway to return the data, so there is not much more to gain by using the index.
Index on the fields on the other side of the join. You do not mention if labs.visit_id
field is indexed. If it is not, then again there is less to be gained from using the pk of the visits table.
The speed of producing the results does not depend on the indexes used, it also depends of the size of the resultset (both record and field count), mysql configuration, and the overall performance of the underlying system. Nevertheless, if you believe that mysql should use the pk of the visits table, then use an index hint in the query to emphasise that the index should be used. You can check with explain
if mysql was influenced by the index hint.
Upvotes: 5