如何启用 mysql 为我的 table 使用索引
how to enable mysql to using index for my table
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使用explain,但我发现类型是All,性能很差,我该如何改进sql
这是sql解释的表演图片
您可以尝试将 trade_time 添加到复合索引中作为 (tenant_id, store_id, trade_time)
INDEX my_indx_name (tenant_id, store_id, trade_time)
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使用explain,但我发现类型是All,性能很差,我该如何改进sql
这是sql解释的表演图片
您可以尝试将 trade_time 添加到复合索引中作为 (tenant_id, store_id, trade_time)
INDEX my_indx_name (tenant_id, store_id, trade_time)