为DISTINCT、GROUP BY和ORDER BY效率问题提提速

废话不多说,直击问题处理过程...

问题描述

原SQL,多表关联,order表4w,order_trace表24w数据,按照正常逻辑,就算关联7表,查询其他表得字段很多,查询分页得到数据速度平均在1.300s,肯定存在问题。

SELECT DISTINCT (a.order_no), a.`name` order_name, a.eid, a.uid, a.device_type_id, a.num, a.`desc`, a.attach, a.total_amount amount, a.ctime, a.publish_time, a.finish_time, a.`status`, a.ent_name, a.ent_user_name, a.ent_user_phone, a.cust_id, a.cust_name, a.start_work_time, a.expire_time, IF ( ( NOW() > a.expire_time AND a.`status` = 4 ) OR ( a.cancel_time > a.expire_time AND a.`status` = 5 ) OR b.`type` = 2, '1', '2' ) 'is_expire', d.parent_id, e.`name` parent_name, d.`name` device_type_name, b.eid AS `user.eid`, b.uid AS `user.uid`, b.ctime AS `user.ctime`, c.`name` AS `orderAddress.name`, c.phone AS `orderAddress.phone`, c.province_id AS `orderAddress.province_id`, c.province_name AS `orderAddress.province_name`, c.city_id AS `orderAddress.city_id`, c.city_name AS `orderAddress.city_name`, c.county_id AS `orderAddress.county_id`, c.county_name AS `orderAddress.county_name`, c.detail_address AS `orderAddress.detail_address`, c.`desc` AS `orderAddress.desc`, c.lng AS `orderAddress.lng`, c.lat AS `orderAddress.lat`, b.`type` AS `userOrder.type`, b.`cntr_id` AS `userOrder.cntr_id`, b.`name` AS `userOrder.name`, b.`phone` AS `userOrder.phone`, b.dispatch_uid AS `userOrder.dispatch_uid`, b.dispatch_time AS `userOrder.dispatch_time`, b.type AS `user_order_type`, g.attn_name AS `order_attr.attn_name`, g.attn_phone AS `order_attr.attn_phone`, a.settle_type FROM `order` a LEFT JOIN user_order b ON a.order_no = b.order_no AND b.`status` = 1 LEFT JOIN order_address c ON a.order_no = c.order_no LEFT JOIN category d ON a.device_type_id = d.id LEFT JOIN category e ON d.parent_id = e.id LEFT JOIN order_item f ON f.order_no = a.order_no LEFT JOIN order_attr g ON g.order_no = a.order_no WHERE 1 = 1 AND ( a.`status` IN (4, 5, 6, 7, 12) AND a.payway IS NOT NULL OR a.`status` = 14 OR a.`status` = 10 ) AND ( a.`status` != 4 OR a.audit_time IS NOT NULL ) ORDER BY a.order_no DESC LIMIT 12 OFFSET 0;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfxwd.html