Reputation: 2013
Can anyone please tell me why mysql is not using 'playerLead_status_INDEX' index?
CREATE TABLE `bcs_player_lead`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) DEFAULT NULL,
`last_name` varchar(100) DEFAULT NULL,
`email_id` varchar(100) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`state` varchar(100) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`status` int(11) DEFAULT NULL COMMENT '1-New,2-invitation send',
PRIMARY KEY (`id`),
KEY `playerLead_status_INDEX` (`status`)
) ENGINE=InnoDB AUTO_INCREMENT=510 DEFAULT CHARSET=latin1;
EXPLAIN SELECT email_id,id,NULL,first_name,last_name,NULL,state,city,NULL,(2) as type FROM bcs_player_lead WHERE status='1'
id|select_type|table |type|possible_keys |key |key_len |ref |rows |Extra
1 |SIMPLE |bcs_player_lead |ALL |playerLead_status_INDEX|{null}|{null}|{null}| 458 |Using where
bcs_player_lead have two keys -
Thanks in advance!
Upvotes: 4
Views: 13054
Reputation: 678
Query planner calculates costs for several paths to execute a given query. The path with the least cost is selected. Using an index scan VS table scan are 2 paths. Let's consider both paths:
Thus, Case 1 can incur a lot more cost than case2. You should check the index cardinality to get an idea on this. Usually, indexes with very low cardinality and very low selectivity are bad choices i.e. unique values are very low(status) and actual # of rows is very high.
Upvotes: 0
Reputation: 77876
I am no DBA but AFAIK, sometimes index is not used even if there is one: when table statistics are not updated (OR) probably less number of rows to retrieve as @Leo said (since in your case only 458 rows to retrieve).
You can force the use of index though in your query using FORCE INDEX (index_name)
. Try it once and check the EXPLAIN PLAN
SELECT email_id,id,NULL,first_name,last_name,NULL,state,city,NULL,(2) as type
FROM bcs_player_lead
FORCE INDEX (playerLead_status_INDEX)
WHERE status='1'
You might want to check this post once MySQL not using indexes with WHERE IN clause?
Upvotes: 7