I have a simple select, involving a join:
SELECT `user`.*, `group`.`alias` AS `group_alias`
FROM `user` INNER JOIN `group` ON `user`.`group_id` = `group`.`id`
WHERE `user`.`state` = 1
AND `group`.`state` = 0
ORDER BY `user`.`id` DESC LIMIT 5 OFFSET 0;
When i set group.state to 0 in the where clause, it uses a ref strategy for joins and takes around 0.001s:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: group
type: ref
possible_keys: PRIMARY,idx_group_state
key: idx_group_state
key_len: 1
ref: const
rows: 11
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: user
type: ref
possible_keys: idx_group_id,idx_user_group_id
key: idx_group_id
key_len: 4
ref: db.group.id
rows: 5608
Extra: Using where
When I set it to 1 however, it uses a range strategy and takes a lot longer (~2 sec)
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: group
type: range
possible_keys: PRIMARY,idx_group_state
key: idx_group_state
key_len: 1
ref: NULL
rows: 35
Extra: Using index condition; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: user
type: ref
possible_keys: idx_group_id,idx_user_group_id
key: idx_group_id
key_len: 4
ref: db.group.id
rows: 5608
Extra: Using where
I have an index on state in both tables, user and group.
How can I dig further into this?
Can I change the behavior somehow, that querys with state = 1 are faster than state = 0?
Interestingly, if I remove group.alias from the selected values, ref strategy is also used everywhere (even with state = 1), which also confuses me a lot.