当前位置: 代码迷 >> SQL >> GROUP BY外移,拔高SQL运行速度
  详细解决方案

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

热度:69   发布时间:2016-05-05 10:41:44.0
GROUP BY外移,提高SQL运行速度

数据表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	) oGROUP BY	order_code
运行时间0.049秒






  相关解决方案