ORACLE中SQL查詢優化研究

2021-06-16 08:20:50 字數 3038 閱讀 1226

**:

查詢計畫及主要統計資料如下:

執行計畫:

-----------------------------------------

……2    1   hash join (cost=5 card=14 bytes=224)

3    2   table access (full) of 'dept' (cost=2 card=4 bytes=52)

4    2   table access (full) of 'emp' (cost=2 card=14 bytes=42)

主要統計資料:

-----------------------------------------

305  recursive calls

46  consistent gets

建立物化檢視emp_dept:

create materialized view emp_dept build immediate

refresh on demand

enable query rewrite

asselect dept.deptno,dept.dname,count(*)

from emp,dept

where emp.deptno=dept.deptno

group by dept.deptno,dept.dname

/再次執行查詢,執行計畫及主要統計資料如下:

執行計畫:

-------------------------------------

……1    0   table access (full) of 'emp_dept' (cost=2 card=327 bytes=11445)

主要統計資料:

------------------------------------

79  recursive calls

28  consistent gets

可見,在建立物化檢視之前,首先執行兩個表的全表掃瞄,然後進行hash連線,再進行分組排序和選擇操作;而建立物化檢視後,cbo自動將上述複雜操作轉換為對物化檢視emp_dept的全掃瞄,相關的統計資料也有了很大的改善,遞迴呼叫(recursive calls)由305降到79,邏輯i/o(consistent gets)由46降為28。

4.2.3  將頻繁訪問的小表讀入cache

邏輯i/o總是快於物理i/o。如果資料庫中存在被

應用程式頻繁訪問的小表,可將這些表強行讀入keep池,從而避免物理i/o的發生。

最能體現查詢複雜性的就是多表連線,多表連線操作往往要耗費大量的cpu時間和記憶體,因此多表連線查詢效能優化往往是sql優化的重點與難點。

4.3.1  消除外部連線

通過消除外部連線,不僅使得到的查詢更易於讀取,而且效能也經常可以得到改善。一般的思路是,有以下形式的查詢:

select …,outer_joined_table.column

from some_table,outer_joined_to_table

where …=outer_joined_to_table(+)

可轉換為如下形式的查詢:

select …,(select column from outer_ joined_to_table where …)from some_table;

4.3.2  謂詞前推,優化中間結果

多表連線的效能低下多數是因為連線操作與過濾操作的次序不合理,大多數使用者在編寫多表連線查詢時,總是先進行連線操作再應用過濾條件,這導致伺服器做了太多的無用功。針對這類

問題,其優化思路就是盡可能將過濾謂詞前推,使不符合條件的記錄提前被篩選掉,只對符合條件的少數記錄進行連線處理,這樣可成倍的提高sql查詢效能。

如下圖所示的星形模型,現要統計最近三個月進貨的商品在各種銷售渠道上的銷售業績。

圖2  產品銷售的星形模型

標準連線查詢如下:

select a.prod_name,sum(b.sale_quant),

sum(c.sale_quant),sum(d.sale_quant)

from product a,tele_sale b,online_sale c,store_sale d

where a.prod_id=b.prod_id and a.prod_id=c.prod_id

and a.prod_id=d.prod_id and a.order_date>sysdate-90

group by a.prod_id;

啟用內嵌檢視,且將條件a.order_date>sysdate-90前移,優化後**如下:

select a.prod_name,b.tele_sale_sum,c.online_sale_sum,d.store_sale_sum from product a,

(select sum(sal_quant) tele_sale_sum from product,tele_sale

where product.order_date>sysdate-90 and product.prod_id =tele_sale.prod_id) b,

(select sum(sal_quant) online_sale_sum

from product,tele_sale

where product.order_date>sysdate-90 and product.prod_id =online_sale.prod_id) c,

(select sum(sal_quant) store_sale_sum

from product,store_sale

where product.order_date>sysdate-90 and product.prod_id =store_sale.prod_id) d,

where a.prod_id=b.prod_id and

a.prod_id=c.prod_id and a.prod_id=d.prod_id;

sql查詢優化 oracle

1.oracle自上而下解析where語句,表關聯語句寫在前面,過濾條件寫在後面 2.避免使用 查詢 操作是查詢資料字典,耗時 3.子查詢儘量減少對錶的查詢 select col1,col2 from t1 where col1 select col1 from t2 where 4.使用decod...

oracle中sql語句查詢優化 四

9 union操作符 union在進行表鏈結後會篩選掉重複的記錄,所以在表鏈結後會對所產生的結果集進行排序運算,刪除重複的記錄再返回結果。實際大部分應用中是不會產生重複的記錄,最常見的是過程表與歷史 表union。如 複製 如下 select from gc dfys union select fr...

oracle中sql語句查詢優化 五

10 sql書寫的影響 同一功能同一效能不同寫法sql的影響 如乙個sql在a程式設計師寫的為 select from zl yhjbqk b程式設計師寫的為 select from dlyx.zl yhjbqk 帶表所有者的字首 c程式設計師寫的為 select from dlyx.zlyhjbq...