10G中一些SQL優(yōu)化的亮點(diǎn)
1、優(yōu)化器默認(rèn)為CBO,OPTIMIZER_MODE默認(rèn)值為ALL_ROWS。不再使用古老的RBO模式,但RULE、CHOOSE并沒(méi)有徹底消失,有些時(shí)候仍然可以作為我們調(diào)試的工具。
2、CPU Costing的計(jì)算方式現(xiàn)在默認(rèn)為CPU+I/O兩者之和.可通過(guò)DBMS_XPLAN.DISPLAY_CURSOR觀察更為詳細(xì)的執(zhí)行計(jì)劃。
3、增加了幾個(gè)有用SQL Hints:
INDEX_SS[[@block] tabs [inds]],INDEX_SS_ASC,INDEX_SS_DESC;SS為SKIP SCAN的縮寫(xiě)。skip scan以前討論的很多。
NO_USE_N[[@block] tabs],NO_USE_HAHS,NO_USE_MERGE,NO_INDEX_FFS,NO_INDEX_SS,NO_STAR_TRANSFORMATION,NO_QUERY_TRANSFORMATION.
這幾個(gè)HINT不用解釋?zhuān)豢淳椭滥康氖鞘裁础?/p>
USE_NL_WITH_INDEX([@block] tabs [index]):這個(gè)提示和Nested Loops有關(guān),通過(guò)提示我們可以指定Nested Loops循環(huán)中的內(nèi)部表,也就是開(kāi)始循環(huán)連接其他表的表。CBO是否會(huì)執(zhí)行取決于指定表是否有索引鍵關(guān)聯(lián)。
QB_NAME(@blockname) 這個(gè)提示可以給某個(gè)查詢(xún)定義一個(gè)name,并且可以在其他hints中使用這個(gè)name,并且將這個(gè)hints作用到這個(gè)name對(duì)應(yīng)的查詢(xún)中.其實(shí)從10G開(kāi)始,Oracle對(duì)一些特定的查詢(xún)自動(dòng)使用queryblockname
4、10G中支持在hint中使用queryblockname
select * from a1 where id in (select /*+ qb_name(sub1) */ id
from a1 where id in (2,10,12));
Execution Plan
----------------------------------------------------------
Plan hash value: 173249654
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 34 | 3 (34)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| A1 | 1 | 14 | 1 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 2 | 34 | 3 (34)| 00:00:01 |
| 3 | SORT UNIQUE | | 2 | 6 | 1 (0)| 00:00:01 |
| 4 | INLIST ITERATOR | | | | | |
|* 5 | INDEX RANGE SCAN | IDX_A1_ID | 2 | 6 | 1 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IDX_A1_ID | 1 | | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
---------------
select * from a1 where id in (select /*+ qb_name(sub1) full(@sub1 a1) */ id
from a1 where id in (2,10,12));
Plan hash value: 1882950619
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 34 | 17 (6)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| A1 | 1 | 14 | 1 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 2 | 34 | 17 (6)| 00:00:01 |
| 3 | SORT UNIQUE | | 2 | 6 | 15 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL | A1 | 2 | 6 | 15 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | IDX_A1_ID | 1 | | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
-----------
修改成錯(cuò)誤的queryblockname
select * from a1 where id in (select /*+ qb_name(sub1) full(@sub2 a1) */ id from a1 where id in (2,10,12));
Execution Plan
----------------------------------------------------------
Plan hash value: 173249654
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 34 | 3 (34)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| A1 | 1 | 14 | 1 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 2 | 34 | 3 (34)| 00:00:01 |
| 3 | SORT UNIQUE | | 2 | 6 | 1 (0)| 00:00:01 |
| 4 | INLIST ITERATOR | | | | | |
|* 5 | INDEX RANGE SCAN | IDX_A1_ID | 2 | 6 | 1 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IDX_A1_ID | 1 | | 0 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
如果指定的queryblockname未定義,還是保持以前的執(zhí)行計(jì)劃,證明queryblockname起作用了.
5、新的hints.spread_no_analysis、spread_min_analysis 用于優(yōu)化analyze查詢(xún).具體以后測(cè)試下
6、10GR2的一些變化.
增強(qiáng)了AWR的報(bào)告, 提供了專(zhuān)門(mén)的ash報(bào)告,可以通過(guò)新的ashrpt.sql($ORACLE_HOME/rdbms/admin下)腳本產(chǎn)生我們需要的ash報(bào)告;提供了類(lèi)似于statspack獲取AWR庫(kù)中某個(gè)sql(通過(guò)腳本)的統(tǒng)計(jì)信息和執(zhí)行信息
·streams_pool_size現(xiàn)在成為ASSM中的一員
·自動(dòng)調(diào)節(jié)DB_FILE_MULTIBLOCK_READ_COUNT參數(shù),Oracle會(huì)根據(jù)數(shù)據(jù)庫(kù)的訪問(wèn)自動(dòng)調(diào)節(jié)該參數(shù)
·增加了SQL的優(yōu)化模式,提供了SQL Tuning Adsivor,SQL Profile等工具.可自動(dòng)優(yōu)化sql語(yǔ)句
·兩個(gè)比較重要的視圖:v$PROCESS_MEMORY,動(dòng)態(tài)監(jiān)控每個(gè)進(jìn)程的pga使用,v$sqlstats某種情況下可以替換v$sql視圖
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄