我在产品表上有索引:
查询:
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
题:
为什么mysql使用索引
products_subcategory_id_foreign
代替
idx_products_cat_subcat_gender_used(多列索引)
这里解释:
1 SIMPLE产品NULL ref products_gender_id_foreign,products_subcategory_id ... products_subcategory_id_foreign 5 const 2 2.50使用索引条件;用在哪里;使用filesort
正如在MySQL documentation中所解释的那样,在一些环境中可以忽略指数。那些可以适用于你的情况,因为一个索引已经被使用,是:
我的猜测是category_id的值不够稀疏
正如我所说的here,这个
where `category_id` = '1' and
`subcategory_id` = '2' and
`gender_id` = '1' and
`used` = '0'
order by `created_at` desc
limit 24 offset 0
需要一个5列的综合指数:
INDEX(category_id, subcategory_id, gender_id, used, -- in any order
created_at)
到达LIMIT
,从而不必获取大量行并对它们进行排序。
关于你选择哪个指数的实际问题......可能一个指数不足的基数比另一个指数要好。