I have indexes on products table:
- PRIMARY
- products_gender_id_foreign
- products_subcategory_id_foreign
- idx_products_cat_subcat_gender_used (multiple column index)
QUERY:
select `id`, `name`, `price`, `images`, `used`
from `products`
where `category_id` = '1' and
`subcategory_id` = '2' and
`gender_id` = '1' and
`used` = '0'
order by `created_at` desc
limit 24 offset 0
Question:
Why mysql uses index
products_subcategory_id_foreign
insted of
idx_products_cat_subcat_gender_used (multiple column index)
HERE IS EXPLAIN :
1 SIMPLE products NULL ref products_gender_id_foreign,products_subcategory_id... products_subcategory_id_foreign 5 const 2 2.50 Using index condition; Using where; Using filesort
SHOW CREATE TABLE <name>created_atpotentially could be added to the end of the index. Try not to quote number if the fields are numberic.created_atto possibly avoidfilesort.created_atwill not avoid filesort unless the index also handles all of theWHERE.