優化SQL中的or
快過年了,要保證庫的穩定,以免影響假期,早上來就跑了個一週的AWR,發現TOP1的sql很詭異
/* Formatted on 2012-1-12 13:43:28 (QP5 v5.149.1003.31008) */
SELECT *
FROM app.map p LEFT JOIN APP.PG m ON devid = p.loggerid
WHERE (devid = 'YQ6101250003'
AND (curdatetime BETWEEN TO_DATE ('2011-05-01 00', 'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00', 'YYYY-MM-DD HH24')))
OR (devid = 'YQ6101250004'
AND (curdatetime BETWEEN TO_DATE ('2011-05-01 00',
'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00',
'YYYY-MM-DD HH24')))
OR (devid = 'YQ6101250005'
AND (curdatetime BETWEEN TO_DATE ('2011-05-01 00',
'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00',
'YYYY-MM-DD HH24')))
OR (devid = 'YQ6101250002'
AND (curdatetime BETWEEN TO_DATE ('2011-05-01 00',
'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00',
'YYYY-MM-DD HH24')))
ORDER BY devid, curdatetime;
手工跑了一下,好傢伙18分45秒,這裡使用了大量的or,這會使查詢速度相當的慢,可以改用union all
手工跑了一下,好傢伙18分45秒,這裡使用了大量的or,這會使查詢速度相當的慢,可以改用union all
/* Formatted on 2012-1-12 14:15:00 (QP5 v5.149.1003.31008) */
SELECT *
FROM app.map p
LEFT JOIN
app.PG m
ON devid = p.loggerid
WHERE devid = 'YQ6101250003'
AND curdatetime BETWEEN TO_DATE ('2011-05-01 00', 'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00', 'YYYY-MM-DD HH24')
UNION ALL
SELECT *
FROM app.map p
LEFT JOIN
app.PG m
ON devid = p.loggerid
WHERE devid = 'YQ6101250004' AND curdatetime BETWEEN TO_DATE ('2011-05-01 00', 'YYYY-MM-DD HH24') AND TO_DATE ('2011-05-05 00', 'YYYY-MM-DD HH24')
UNION ALL
SELECT *
FROM app.map p
LEFT JOIN
app.PG m
ON devid = p.loggerid
WHERE devid = 'YQ6101250005'
AND curdatetime BETWEEN TO_DATE ('2011-05-01 00', 'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00', 'YYYY-MM-DD HH24')
UNION ALL
SELECT *
FROM app.map p
LEFT JOIN
app.PG m
ON devid = p.loggerid
WHERE devid = 'YQ6101250002'
AND curdatetime BETWEEN TO_DATE ('2011-05-01 00', 'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00', 'YYYY-MM-DD HH24')
再次查詢,2秒,哈哈就喜歡這種優化後這種幾何級增長的。聯絡了下開發人員,原來是在程式碼中拼SQL的,or的個數未知,看來要改程式碼了,都改成union all吧
當然這個例子也可以使用in來解決
當然這個例子也可以使用in來解決
/* Formatted on 2012-1-12 13:43:28 (QP5 v5.149.1003.31008) */
SELECT *
FROM app.map p LEFT JOIN app.PG_HH m ON devid = p.loggerid
WHERE devid in( 'YQ6101250003', 'YQ6101250004','YQ6101250005','YQ6101250002')
AND curdatetime BETWEEN TO_DATE ('2011-05-01 00',
'YYYY-MM-DD HH24')
AND TO_DATE ('2011-05-05 00',
'YYYY-MM-DD HH24')
ORDER BY devid, curdatetime
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25680865/viewspace-714819/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL優化的方法論SQL優化
- 資料庫優化 - SQL優化資料庫優化SQL
- sql優化之邏輯優化SQL優化
- SQL SERVER優化SQLServer優化
- SQL優化指南SQL優化
- Oracle效能優化-SQL優化(案例一)Oracle優化SQL
- Oracle效能優化-SQL優化(案例二)Oracle優化SQL
- Oracle效能優化-SQL優化(案例三)Oracle優化SQL
- Oracle效能優化-SQL優化(案例四)Oracle優化SQL
- sql優化專題SQL優化
- SQL效能優化技巧SQL優化
- SQL語句優化SQL優化
- 慢Sql優化思路SQL優化
- MySQL-SQL優化MySql優化
- SQL優化參考SQL優化
- Oracle SQL優化之sql tuning advisorOracleSQL優化
- SQL優化案例-使用with as優化Subquery Unnesting(七)SQL優化
- 關於SQL優化的闢謠SQL優化
- 優化 SQL 語句的步驟優化SQL
- MySQL的SQL效能優化總結MySql優化
- 對含distinct操作的SQL的優化SQL優化
- SQL優化案例-單表分頁語句的優化(八)SQL優化
- 神奇的 SQL 之效能優化 → 讓 SQL 飛起來SQL優化
- 遊戲陪玩app開發中,Mysql的sql優化方法遊戲APPMySql優化
- 優化sql查詢速度優化SQL
- MySQL SQL優化案例(一)MySql優化
- (轉)SQL 優化原則SQL優化
- MySQL之SQL優化技巧MySql優化
- [20201224]sql優化困惑.txtSQL優化
- sql語句效能優化SQL優化
- 資料庫優化SQL資料庫優化SQL
- MYSQL SQL語句優化MySql優化
- SQL優化之limit 1SQL優化MIT
- 史上最全SQL優化方案SQL優化
- mysql優化 | 儲存引擎,建表,索引,sql的優化建議MySql優化儲存引擎索引
- [20200320]SQL語句優化的困惑.txtSQL優化
- 關於SQL優化的小知識SQL優化
- SQL精華總結索引型別優化SQL優化事務大表優化思維導圖❤️SQL索引型別優化
- 效能調優——SQL最佳化SQL