SQL調整:‘以空間換效能’調整一例
如有錯誤,請指正。
客戶抱怨在前臺執行的時候很慢,經檢查發現問題為查詢一個檢視vw_hyb_tbgrjbxx_temp的時候很慢,查詢條件是dwbh和yzbz。檢視的定義為:
create or replace view vw_hyb_tbgrjbxx
as
select /*+first_rows*/ t1.*,t2.yzbz,t2.grbh_new
from tb_grjbxx t1,hyb_yzbz t2
where t1.grbh = t2.grbh
/
sidb@GDSI-HYQL> l
1* select *from vw_hyb_tbgrjbxx_temp where dwbh = '341004' and yzbz = '0'
sidb@GDSI-HYQL> /
未選定行
已用時間: 00: 00: 04.08
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=455 Card=466 Bytes=1
15102)
1 0 HASH JOIN (Cost=455 Card=466 Bytes=115102)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'TB_GRJBXX' (Cost=19 Ca
rd=466 Bytes=106714)
3 2 INDEX (RANGE SCAN) OF 'IDX_TB_GRJBXX_DWBH' (NON-UNIQUE
) (Cost=7 Card=466)
4 1 TABLE ACCESS (FULL) OF 'HYB_YZBZ' (Cost=403 Card=129281
Bytes=2327058)
Statistics
----------------------------------------------------------
0 recursive calls
5 db block gets
2773 consistent gets
2275 physical reads
112 redo size
3568 bytes sent via SQL*Net to client
372 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
sidb@GDSI-HYQL>
從執行計劃可知,表HYB_YZBZ執行了全表掃描,一般的做法是把全表掃描變為透過索引檢索資料。在該例中,發現表HYB_YZBZ上列DWBH建有索引,而且HYB_YZBZ的列GRBH/DWBH與TB_GRJBXX的GRBH/DWBH是一一對應的,因而調整檢視定義為:
create or replace view vw_hyb_tbgrjbxx_temp
as
select t1.*,t2.yzbz,t2.grbh_new
from tb_grjbxx t1,hyb_yzbz t2
where t1.grbh = t2.grbh
and t1.dwbh = t2.dwbh
/
我們來看看調整後的效果:
sidb@GDSI-HYQL> create or replace view vw_hyb_tbgrjbxx_temp
2 as
3 select t1.*,t2.yzbz,t2.grbh_new
4 from tb_grjbxx t1,hyb_yzbz t2
5 where t1.grbh = t2.grbh
6 and t1.dwbh = t2.dwbh
7 /
檢視已建立。
已用時間: 00: 00: 00.01
sidb@GDSI-HYQL> select *from vw_hyb_tbgrjbxx_temp where dwbh = '341004' and yzbz
= '0';
未選定行
已用時間: 00: 00: 00.01
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=27 Card=233 Bytes=60
114)
1 0 HASH JOIN (Cost=27 Card=233 Bytes=60114)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'HYB_YZBZ' (Cost=7 Card
=233 Bytes=6757)
3 2 INDEX (RANGE SCAN) OF 'IDX_HYB_YZBZ_DWBH' (NON-UNIQUE)
(Cost=7 Card=233)
4 1 TABLE ACCESS (BY INDEX ROWID) OF 'TB_GRJBXX' (Cost=19 Ca
rd=466 Bytes=106714)
5 4 INDEX (RANGE SCAN) OF 'IDX_TB_GRJBXX_DWBH' (NON-UNIQUE
) (Cost=7 Card=466)
Statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
31 consistent gets
6 physical reads
0 redo size
3568 bytes sent via SQL*Net to client
372 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
sidb@GDSI-HYQL> /
未選定行
已用時間: 00: 00: 00.01
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=27 Card=233 Bytes=60
114)
1 0 HASH JOIN (Cost=27 Card=233 Bytes=60114)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'HYB_YZBZ' (Cost=7 Card
=233 Bytes=6757)
3 2 INDEX (RANGE SCAN) OF 'IDX_HYB_YZBZ_DWBH' (NON-UNIQUE)
(Cost=7 Card=233)
4 1 TABLE ACCESS (BY INDEX ROWID) OF 'TB_GRJBXX' (Cost=19 Ca
rd=466 Bytes=106714)
5 4 INDEX (RANGE SCAN) OF 'IDX_TB_GRJBXX_DWBH' (NON-UNIQUE
) (Cost=7 Card=466)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
29 consistent gets
0 physical reads
0 redo size
3568 bytes sent via SQL*Net to client
372 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
sidb@GDSI-HYQL>
從統計資訊來看
5 db block gets
2773 consistent gets
2275 physical reads
112 redo size
變為
0 db block gets
29 consistent gets
0 physical reads
0 redo size
調整效果明顯。
小結:
習慣性的,通常在兩個表連線時只需要一個連線條件,而該例從業務規則上入手,透過在表連線上加入適當的‘冗餘’連線條件(在該例中是在DWBH上)進行sql調整。從另外一個方面看,HYB_YZBZ中的DWBH欄位屬於冗餘欄位,利用空間上的損失獲得了效能上面的提升。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-21541/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle SQL調整一例OracleSQL
- SQL Server 2005效能調整一(zt)SQLServer
- 資料庫效能分析及調整一例(zt)資料庫
- Oracle高效能SQL調整OracleSQL
- Oracle效能調整之--DML語句效能調整Oracle
- oracle 效能調整Oracle
- SQL語句效能調整原則(zt)SQL
- 工作筆記 - 調整索引和表空間筆記索引
- SQL Server 2005效能調整二(zt)SQLServer
- 怎麼整理 SQL Server效能調整清單SQLServer
- 「ArrayBuffer」應用-以自動調整照片方向為例
- oracle效能調整(1)Oracle
- oracle效能調整(2)Oracle
- ORACLE效能調整--1Oracle
- ORACLE效能調整---2Oracle
- Oracle 效能調整for HWOracle
- (zt)Oracle效能調整Oracle
- oracle效能調整2Oracle
- linux系統檢視調整swap空間Linux
- Oracle效能最佳化調整--調整重做機制Oracle
- 網路調整——效能調整手冊和參考
- 調整PL/SQL程式碼加速執行(2例)SQL
- 【自動化】使用PL/SQL輔助完成表空間的分類調整SQL
- 【資料操作】SQL語句效能調整原則SQL
- Oracle’s DBMS_Profiler:PL/SQL 效能調整 (轉)OracleSQL
- ORACLE sql 語句的執行過程(SQL效能調整)OracleSQL
- 手工段管理表空間遷移後的調整
- 調整vmware虛擬機器硬碟空間的方法虛擬機硬碟
- Oracle效能調整筆記Oracle筆記
- 【效能調整】等待事件(一)事件
- 【效能調整】等待事件(二)事件
- Oracle效能調整-1(轉)Oracle
- Oracle效能調整-2(轉)Oracle
- Oracle效能調整-3(轉)Oracle
- 用於效能調整的動態效能檢視——效能調整手冊和參考
- 有關效能調整的查詢和pub上的一個sql調優!SQL
- linux時間調整Linux
- oracle高效能sql調整-筆記1 (轉發)OracleSQL筆記