【发布时间】:2018-03-11 02:15:47
【问题描述】:
这个查询的正确索引是什么。
我尝试为这个查询提供不同的索引组合,但它仍在使用 from using tempory , using filesort 等。
总表数据 - 7,60,346
product= '连衣裙' - 总行数 = 122 554
CREATE TABLE IF NOT EXISTS `product_data` (
`table_id` int(11) NOT NULL AUTO_INCREMENT,
`id` int(11) NOT NULL,
`price` int(11) NOT NULL,
`store` varchar(255) NOT NULL,
`brand` varchar(255) DEFAULT NULL,
`product` varchar(255) NOT NULL,
`model` varchar(255) NOT NULL,
`size` varchar(50) NOT NULL,
`discount` varchar(255) NOT NULL,
`gender_id` int(11) NOT NULL,
`availability` int(11) NOT NULL,
PRIMARY KEY (`table_id`),
UNIQUE KEY `table_id` (`table_id`),
KEY `id` (`id`),
KEY `discount` (`discount`),
KEY `step_one` (`product`,`availability`),
KEY `step_two` (`product`,`availability`,`brand`,`store`),
KEY `step_three` (`product`,`availability`,`brand`,`store`,`id`),
KEY `step_four` (`brand`,`store`),
KEY `step_five` (`brand`,`store`,`id`)
) ENGINE=InnoDB ;
查询:
SELECT id ,store,brand FROM `product_data` WHERE product='dresses' and
availability='1' group by brand,store order by store limit 10;
excu..time :-(总共 10 个,查询耗时 1.0941 秒)
解释计划:
可能的键:- step_one、step_two、step_three、step_four、step_five
键:- step_two
ref :- const,const
行:- 229438
额外:-使用 where;使用临时的;使用文件排序
我试过这些索引
Keystep_one (product,availability)
Keystep_two (product,availability,brand,store)
Keystep_three (product,availability,brand,store,id)
Keystep_four (brand,store)
Keystep_five (brand,store,id)
【问题讨论】:
-
在您的问题中提供
SHOW CREATE TABLE product_data输出。 -
@kuldeepupadhyay 请给我们每个索引组合的时间结果,你提到过
-
@Andy K 是的,先生,我的查询选择第二个索引,他们查询花了 1.0941 秒
-
他们选择自动第二步,我认为这是最好的组合,但仍然需要更多时间和使用临时,使用文件排序
-
“分组依据”是做什么的?它通常只与聚合函数有关。
标签: mysql sql indexing database-administration