優化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 SERVER中SQL優化SQLServer優化
- 優化SQL Server中的備份優化SQLServer
- sql server中的hash應用優化SQLServer優化
- 【SQL優化】SQL優化的10點注意事項SQL優化
- 【SQL優化】SQL優化工具SQL優化
- mysql的sql優化MySql優化
- SQL Server優化之SQL語句優化SQLServer優化
- SQL優化SQL優化
- with as優化sql優化SQL
- 效能優化案例-SQL優化優化SQL
- SQL優化的方法論SQL優化
- SQL語句的優化SQL優化
- 優化sql的利器SQLT優化SQL
- 一個sql的優化SQL優化
- 資料庫優化 - SQL優化資料庫優化SQL
- sql優化之邏輯優化SQL優化
- SQL優化:limit分頁優化SQL優化MIT
- MySQL 效能優化之SQL優化MySql優化
- SQL優化--用各種hints優化一條SQLSQL優化
- 優化 SQL Server 中的備份和還原效能優化SQLServer
- sql優化講課中引出的各種問題!SQL優化
- SQL優化指南SQL優化
- SQL SERVER優化SQLServer優化
- sql效能優化SQL優化
- Sql優化方法SQL優化
- oracle sql優化OracleSQL優化
- SQL優化-索引SQL優化索引
- SQL優化(一)SQL優化
- oracle sql 優化OracleSQL優化
- sql 效能優化SQL優化
- 【中亦安圖】SQL優化之基於SQL特徵的改寫(9)SQL優化特徵
- Oracle效能優化-SQL優化(案例一)Oracle優化SQL
- Oracle效能優化-SQL優化(案例二)Oracle優化SQL
- Oracle效能優化-SQL優化(案例三)Oracle優化SQL
- Oracle效能優化-SQL優化(案例四)Oracle優化SQL
- msyql 簡單的sql優化SQL優化
- 不懂業務的SQL優化方法SQL優化
- SQL查詢優化的方法SQL優化