1 SELECT 2 om.COLLEGE_NAME AS collegeName, 3 COUNT(1) AS QUANTITY, 4 SUM(od.INQUIRE_PRICE) AS AMOUNT, 5 SUM(CASE WHEN od.INQUIRE_PRICE > bo.BID_PRICE THEN (od.INQUIRE_PRICE - bo.BID_PRICE)/od.INQUIRE_PRICE 6 ELSE (od.INQUIRE_PRICE - a.MIN_BID_PRICE)/od.INQUIRE_PRICE END)/COUNT(1) AS saveRate 7 FROM cp_bid_order bo 8 LEFT JOIN co_order_detail od ON bo.DETAIL_ID = od.DETAIL_ID 9 LEFT JOIN co_order_main om ON bo.ORDER_MAIN_ID=om.ORDER_MAIN_ID 10 LEFT JOIN 11 (SELECT 12 DETAIL_ID AS DETAIL_ID, 13 MIN(BID_PRICE) AS MIN_BID_PRICE 14 FROM cp_bid_order 15 GROUP BY DETAIL_ID) a 16 ON bo.DETAIL_ID = a.DETAIL_ID
完整的语句还有条件的
WHERE
om.CHOOSE_OPTION != 2
AND om.ORDER_TYPE != 2
AND bo.BID_STATUS = 'Y'
AND od.INQUIRE_PRICE != 0
om.CHOOSE_OPTION,om.ORDER_TYPE都是varchar型的,
而我做判断的时候,让它们跟数字进行判断
昨天我这样写的时候在mysql执行需要74秒
今天我改成如下写法,让那两个字段跟字符型做比较
执行完成只需39秒左右
WHERE
om.CHOOSE_OPTION != '2'
AND om.ORDER_TYPE != '2'
AND bo.BID_STATUS = 'Y'
AND od.INQUIRE_PRICE != 0
想问一下这是什么原因呢?
case when 是性能杀手
left join是性能杀手
那有什么方法可以替代一下吗?
@kcher: 你那个case when是不需要的,
直接 ABS(od.INQUIRE_PRICE - bo.BID_PRICE)/od.INQUIRE_PRICE,
Simple is better.
另外,Left join可能也不需要,这个得你自己看。
先用性能监视器查出性能瓶颈在哪里呀!!!
创建个临时表,分开来一步步UPDATE
支持 楼上 先用性能监视器查出性能瓶颈在哪里呀!!!
SELECT 12 DETAIL_ID AS DETAIL_ID, 13 MIN(BID_PRICE) AS MIN_BID_PRICE 14 FROM cp_bid_order 15 GROUP BY DETAIL_ID 先查出来放在临时表里面
用公用表达式With
目测是:cp_bid_order 的 GROUP BY 慢,没有任何条件的查询.
试着查询每个表,看哪个表慢,加条件试,再 join