【发布时间】:2019-01-24 23:24:18
【问题描述】:
我在 products 表上有索引:
- 初级
- products_gender_id_foreign
- products_subcategory_id_foreign
- idx_products_cat_subcat_gender_used (多列索引)
查询:
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 products NULL ref products_gender_id_foreign,products_subcategory_id... products_subcategory_id_foreign 5 const 2 2.50 使用索引条件;使用哪里;使用文件排序
【问题讨论】:
-
您的行数似乎很少,所以具体的索引并不重要。
-
任何query-optimization 问题都应包含
SHOW CREATE TABLE <name>的输出 -
虽然很难阅读您的索引名称,但
created_at可能会添加到索引的末尾。如果字段是数字,请尽量不要引用数字。 -
你需要给
created_at添加一个索引来避免filesort。 -
@LuisMuñoz - 添加
created_at将不会避免文件排序除非索引还处理所有的@987654328 @.
标签: mysql sql optimization indexing query-optimization