Oracle case when改寫SQL
Oracle case when 改寫 SQL
--- 說明:案例來自《 收穫,不止SQL 最佳化 》
建立測試資料:
SQL > drop table t1 purge ;
SQL > drop table t2 purge ;
SQL > create table t1 as select * from dba_objects ;
SQL > create table t2 as select * from dba_objects ;
SQL > update t2 set status = 'INVALID' WHERE ROWNUM <= 10000 ;
SQL > update t2 set generated = 'Y' WHERE ROWNUM <= 10000 ;
SQL > update t2 set temporary = 'Y' WHERE ROWNUM <= 10000 ;
SQL > update t2 set temporary = 'M' WHERE temporary <> 'Y' ;
SQL > update t2 set temporary = 'Q' WHERE temporary <> 'Y' or temporary <> 'M' ;
SQL > COMMIT ;
SQL > set autotrace traceonly
SQL > set linesize 1000
原 SQL :
SQL> select t1.object_name, t1.object_id, (select count(*) from t2 where temporary = 'Y' and t2.object_id = t1.object_id) CNT_TEMPORARY_Y, (select count(*) from t2 where created >= sysdate - 365 and t2.object_id = t1.object_id) CNT_CREATED_NEW, (select sum(object_id) from t2 where status <> 'VALUD' and t2.object_id = t1.object_id) SUM_OBJID_STATUS_V, (select sum(object_id) from t2 where generated = 'Y' and t2.object_id = t1.object_id) SUM_OBJID_GENERATED_Y, (select sum(object_id) from t2 where generated = 'M' and t2.object_id = t1.object_id) SUM_OBJID_GENERATED_M, (select sum(object_id) from t2 where generated = 'Q' and t2.object_id = t1.object_id) SUM_OBJID_GENERATED_Q from t1 where t1.object_id <= 50;
case when改造 後的 SQL :
with w_t2 as (select t2.object_id, count(case when t2.temporary='Y' then 1 end ) CNT_TEMPORARY_Y, count(case when created >=sysdate-365 then 1 end ) CNT_CREATED_NEW, sum(case when t2.status<>'VALID' then t2.object_id end ) SUM_OBJID_STATUS_V, sum(case when t2.generated = 'Y' then t2.object_id end ) SUM_OBJID_GENERATED_Y, sum(case when t2.generated = 'M' then t2.object_id end ) SUM_OBJID_GENERATED_M, sum(case when t2.generated = 'Q' then t2.object_id end ) SUM_OBJID_GENERATED_Q from t2 group by t2.object_id) select t1.object_name,t1.object_id,w_t2.* from t1,w_t2 where t1.object_id=w_t2.object_id and t1.object_id<=50;
結論: SQL 改寫後 T2 表訪問次數由 6 次降到 1 次,邏輯讀 consistent gets 由 320100 降到 2580 ,效能有所提升。
歡迎關注我的微信公眾號"IT小Chen",共同學習,共同成長!!!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2693912/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql case when, Exist ,group by ,聚合SQL
- SQL Server CASE WHEN ... THEN ... ELSE ... ENDSQLServer
- Oracle vs PostgreSQL Develop(30) - Index&Case whenOracleSQLdevIndex
- case when 語句
- Oracle 條件索引 case when 報錯解決方案Oracle索引
- SQL語句case when外用sum與count的區別SQL
- SQL中的case when then else end用法 【詳細】轉載SQL
- mysql中case when的使用MySql
- Oracle group by與case when統一單位後統計數量Oracle
- SQLServer使用case when中的order bySQLServer
- Oracle 11g新特新--SQL Test Case BuilderOracleSQLUI
- DB2 SQL改寫DB2SQL
- Oracle Rownum分頁改寫Oracle
- When Rubber Meets the Road: Unveiling the Curious Case of Volvo Truck Engine Failures!AI
- SQL CASE 表示式SQL
- MySQL的SQL等價改寫MySql
- Oracle到PostgreSQL等價改寫OracleSQL
- sqlserver使用order by case when進行優先順序排序SQLServer排序
- SQL改寫的方法,select group by sumSQL
- 一種提升SQL改寫效率的方法SQL
- SQL 改寫系列七:謂詞移動SQL
- SQL 改寫系列六:謂詞推導SQL
- 語法解析器續:case..when..語法解析計算
- MySQL效能優化之簡單sql改寫MySql優化
- MySQL中case?when對NULL值判斷的踩坑記錄RTGYMySqlNull
- SQL KEEP 視窗函式等價改寫案例SQL函式
- 瞭解GaussDB SQL中CASE表示式SQL
- SQL 抽象語法樹及改寫場景應用SQL抽象語法樹
- SQL抽象語法樹及改寫場景應用SQL抽象語法樹
- SQL 改寫系列十:半連線轉內連線SQL
- 異構資料庫遷移 sql等價改寫資料庫SQL
- 神奇的 SQL 之 CASE表示式,妙用多多 !SQL
- GaussDB SQL基本語法示例-CASE表示式SQL
- 雲端計算學習路線圖素材、課件,msyql中CASE WHEN語法
- sql改寫優化:簡單規則重組實現SQL優化
- (一)《SQL進階教程》學習記錄--CASESQL
- Oracle EBS when-validate-record 個性化無效果Oracle
- Django筆記二十三之case、when操作條件表示式搜尋、更新等操作Django筆記