如何启用mysql为我的表使用索引

时间:2018-01-08 07:02:39

标签: mysql explain

CREATE TABLE `sqq_merchant`.`other_coupon_order` (
`id`  int NOT NULL AUTO_INCREMENT ,
`tenant_id`  varchar(32) NOT NULL ,
`store_id`  varchar(64) NOT NULL ,
`order_no`  varchar(64) NOT NULL ,
`out_order_no`  varchar(64) NOT NULL ,
`total_fee`  integer NOT NULL DEFAULT 0 ,
`other_coupon_fee`  integer NOT NULL DEFAULT 0 ,
`act_total_fee`  integer NOT NULL DEFAULT 0 ,
`pay_method`  tinyint(4) NOT NULL DEFAULT 0 ,
`trade_time`  datetime NOT NULL ,
`create_time`  datetime NOT NULL ,
`update_time`  datetime NOT NULL ,
PRIMARY KEY (`id`),
INDEX `tenant_store_id` (`tenant_id`, `store_id`) USING BTREE ,
UNIQUE INDEX `order_no` (`order_no`) USING BTREE 
);

我想查询定义的租户,定义的商店和定义的交易时间范围的订单,例如:

select * from other_coupon_order where tenant_id = '9001' and store_id = '1151610006' and 
trade_time > '2018-01-08 00:00:00' AND trade_time < '2018-01-08 23:59:59'

虽然我用这个sql解释,我发现类型是All,性能不好,怎样才能改进sql

这是解释

的sql性能图片

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试将trade_time添加到复合索引(tenant_id, store_id, trade_time)

 INDEX my_indx_name  (tenant_id, store_id, trade_time)