近期在做数据库的慢SQL优化,从数据库慢日志看到一个SQL执行时间大概1.7s,高峰时候达到十几秒
mysql> SELECT id, service_seq, order_item_seq, status, order_time_type
-> , reserve_time, user_name, user_contact, service_address_type, service_address_id
-> , service_address, service_mode, insured_name, insured_identity, policy_no
-> , create_time, update_time, over_time, artisan_id, product_id
-> , user_id, longitude, latitude, refund_status, settle_status
-> , comment_status, remark, insured_name, insured_identity, is_del
-> , postpone_status, last_overdue_time, source_from, operator_id
-> FROM order_service_item
-> WHERE status = '10'
-> AND reserve_time BETWEEN '2019-07-10 06:00:00.085' AND '2019-07-10 06:10:00.085'
-> AND is_del = 0
-> ORDER BY id DESC;
Empty set (1.77 sec)
执行计划:
使用了index,但是扫描行数为489300,说明索引效率不高。
查看表结构:
show create table order_service_item;
| order_service_item | CREATE TABLE `order_service_item` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`service_seq` varchar(40) COLLATE utf8_bin NOT NULL COMMENT '服务序列号',
`order_item_seq` varchar(40) COLLATE utf8_bin NOT NULL COMMENT '订单项序列号',
`status` varchar(2) COLLATE utf8_bin DEFAULT NULL COMMENT '服务状态: 00:新订单 10:等待服务 20:取消处理 30:进行中 40:在路上 50:到达 60:美甲师处理完成 90:用户处理完成',
`reserve_time` datetime DEFAULT NULL COMMENT '预约时间',
`user_name` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '客户姓名',
`user_contact` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '客户联系电话',
`service_address_id` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`service_address` varchar(400) COLLATE utf8_bin DEFAULT NULL COMMENT '服务地址',
`service_mode` tinyint(2) DEFAULT '0' COMMENT '服务类型:0:普通 1:班课',
`policy_no` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '保单号',
`insured_name` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '被保险人姓名',
`insured_identity` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '被保险人身份证',
`create_time` datetime NOT NULL COMMENT '建立时间',
`update_time` datetime NOT NULL COMMENT '更新时间',
`artisan_id` varchar(40) COLLATE utf8_bin DEFAULT NULL COMMENT '手艺人ID',
`product_id` varchar(40) COLLATE utf8_bin DEFAULT NULL COMMENT '作品ID',
`user_id` varchar(40) COLLATE utf8_bin DEFAULT NULL COMMENT '客户ID',
`longitude` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '经度',
`latitude` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '纬度',
`refund_status` tinyint(2) DEFAULT '0' COMMENT '退款状态:0 无退款 2 待处理 3 退款请求失败 4 退款请求成功 5 退款失败 6 处理完成 8 退款成功',
`settle_status` tinyint(2) DEFAULT '0' COMMENT '结算状态',
`comment_status` tinyint(2) DEFAULT '0' COMMENT '评论状态 0 未评论 1 已评论',
`remark` varchar(200) COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
`last_update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`order_time_type` tinyint(4) DEFAULT NULL COMMENT '服务时间类型\n:0 随叫随到\n 1 预约美甲 2 买约分离',
`service_address_type` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '服务地址类型:home(顾客家), store(独立手艺人自定义的门店), store_self(门店手艺人所属门店), studio(原小聚点)',
`over_time` datetime DEFAULT NULL COMMENT '服务完成时间',
`is_del` tinyint(1) DEFAULT '0' COMMENT '删除标记 0 未删除 1 已删除',
`postpone_status` tinyint(2) DEFAULT '0' COMMENT '延期标识:0 未过期 1 已过期 2 延期申请中 3 已延期 4 超期取消 5 拒绝延期',
`last_overdue_time` datetime DEFAULT NULL COMMENT '最后过期时间',
`source_from` int(3) DEFAULT NULL COMMENT '20代表套餐预约来的',
`operator_id` varchar(40) COLLATE utf8_bin DEFAULT NULL COMMENT '具体执行服务的手艺人的ID',
`verificat_code` varchar(20) COLLATE utf8_bin DEFAULT '' COMMENT '天猫核销码',
PRIMARY KEY (`id`),
UNIQUE KEY `index_service_seq` (`service_seq`),
KEY `index_order_item_seq` (`order_item_seq`),
KEY `index_user_id` (`user_id`),
KEY `index_artisan_id` (`artisan_id`),
KEY `index_status` (`status`),
KEY `index_product_id` (`product_id`),
KEY `idx_last_update_time` (`last_update_time`),
KEY `idx_comment_status` (`comment_status`,`status`)
) ENGINE=InnoDB AUTO_INCREMENT=11593152 DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC COMMENT='订单服务项表' |
针对这个SQL,在status,reserve_time,is_del和id字段创建联合索引,
alter table order_service_item add index idx_sta_rt_del_id (status,reserve_time,is_del,id);
查看执行计划:
执行计划走了新建的联合索引,并且检索行数变成了1行。
查看执行时间:
mysql> SELECT id, service_seq, order_item_seq, status, order_time_type
-> , reserve_time, user_name, user_contact, service_address_type, service_address_id
-> , service_address, service_mode, insured_name, insured_identity, policy_no
-> , create_time, update_time, over_time, artisan_id, product_id
-> , user_id, longitude, latitude, refund_status, settle_status
-> , comment_status, remark, insured_name, insured_identity, is_del
-> , postpone_status, last_overdue_time, source_from, operator_id
-> FROM order_service_item
-> WHERE status = '10'
-> AND reserve_time BETWEEN '2019-07-10 06:00:00.085' AND '2019-07-10 06:10:00.085'
-> AND is_del = 0
-> ORDER BY id DESC;
Empty set (0.00 sec)
执行时间已经降到了ms级别。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30135314/viewspace-2650105/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30135314/viewspace-2650105/