码迷,mamicode.com
首页 > 数据库 > 详细

GROUP BY外移,提高SQL运行速度

时间:2015-03-17 15:55:47      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

数据表dh_order_detail 里一共有169247条数据

原始SQL

		SELECT
			FROM_UNIXTIME(order_time, '%H:%i') 'time',
			city,
			district,
			LEFT (company, 6) company,
			goods_num,
			order_price,
			order_code,
			order_time
		FROM
			dh_order_detail
		WHERE
			province_id = 16
                GROUP BY
	                order_code
		ORDER BY
			order_time DESC
		LIMIT 50

运行时间4秒多

改进,将GROUP BY外移

SELECT
	*
FROM
	(
		SELECT
			FROM_UNIXTIME(order_time, '%H:%i') 'time',
			city,
			district,
			LEFT (company, 6) company,
			goods_num,
			order_price,
			order_code,
			order_time
		FROM
			dh_order_detail
		WHERE
			province_id = 16
		ORDER BY
			order_time DESC
		LIMIT 50
	) o
GROUP BY
	order_code
运行时间0.049秒






GROUP BY外移,提高SQL运行速度

标签:

原文地址:http://blog.csdn.net/szy361/article/details/44341691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!