理解CBO
- 在Init.ora 引數檔案中設定 optimizer_mode=choose,並且SQL 語句中至少有一個table統計資訊存在
- 執行 alter session set optimizer_mode=choose 命令,並且SQL 語句中至少有一個table統計資訊存在
- 執行 alter session set optimizer_mode=first_rows(或者 all_rows) 命令,並且SQL 語句中至少有一個table統計資訊存在
- SQL 語句使用 First_rows 或者 all_rows 提示(如: select /*+ first_rows*+/)
FROM emp
WHERE emp_no = 2
AND emp_name = 'PJWANG'
AND dept_no = '12'
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=HINT: FIRST_ROWS (Cost=2 Card=1 B
ytes=22)
Bytes=22)
Primary key and/or UNIQUE index equality
A UNIQUE index's selectivity is recognized as 100%. No other indexed access method is more precise. For this reason, a unique index is always used when available.
主鍵和/或 Unique index 的等同性
Unique index的選擇性被認定是100%.其他的index 都沒有Unique index 精確.
基於這個原因,當unique index 是合法時,他能一直被用到.
Non-UNIQUE index equality
For non-UNIQUE indexes, index selectivity is calculated. The cost-based optimizer makes the assumption that the table (and subsequent indexes) have uniform data spread unless you use the FOR ALL INDEXED COLUMNS option of the ANALYZE. That option will make the cost-based optimizer aware of how the data in the indexed columns is skewed.
非-Unique index 等同性
非-unique index ,index 的選擇是計算出來的.
Range evaluation
For index range execution plans, selectivity is evaluated. This evaluation is based on a column's most recent high-value and low-value statistics. Again, the cost-based optimizer makes the assumption that the table (and subsequent indexes) have uniform data spread unless you use the FOR ALL INDEXED COLUMNS option when analyzing the table.
範圍評估
對於Index 範圍的執行計劃,選擇性是可評估的. 此評估基於欄位最近的最高和最低值的統計資訊.
再次重申,當分析table 時,CBO假定table (和併發的Index) 都有相同的資料分佈,除非你使用 for all indexed columns 選項.
Range evaluation over bind variables
For index range execution plans, selectivity is guessed. Prior to Oracle9i, because bind variable values are not available at parse time (values are passed to the cursor after the execution plan has been decided), the optimizer cannot make decisions based on bind variable values. The optimizer assumes a rule of thumb of 25% selectivity for unbounded bind variable ranges (e.g., WHERE dept_no = :b1) and 50% selectivity for bounded ranges (WHERE dept_no > :b1 AND dept_no < :b2). Beginning with Oracle9i, the cost-based optimizer obtains bind variable values prior to determining an execution plan.
範圍評估優於繫結變數
對於Index 範圍執行計劃,選擇是猜測的.在Oracle9i 以前版本,因為繫結變數的值不能在分析時間提供(
在值傳入遊標之前執行計劃已經決定),所以最佳化器不能以繫結變數的值來決定執行計劃. 最佳化器的經驗法則是 對於無範圍限制的選擇機率是 25%(如where dept_no=:b1) ,對於有範圍限制的選擇機率是50%
(where dept_no>:b1 and dept_no<:b2>
Histograms
Prior to the introduction of histograms in Oracle 7.3, The cost-based optimizer could not distinguish grossly uneven key data spreads.
柱狀圖
在Oracle7.3 以前的柱狀圖.CBO 不能辨別粗略的辨別主要資料資料的分佈.
System resource usage
By default, the cost-based optimizer assumes that you are the only person accessing the database. Oracle9i gives you the ability to store information about system resource usage, and can make much better informed decisions based on workload (read up on the DBMS_STATS.GATHER_SYSTEM_STATS package).
系統資源使用率:
在預設情況下.CBO假定只有一人訪問資料庫. Oracle9i 提供保留System resource Usage 的資源資訊,並能對資源進行基於負載的最優分配.(參考 DBMS_STATS.GATHER_SYSTEM_STATS 包)
Current statistics are important
The cost-based optimizer can make poor execution plan choices when a table has been analyzed but its indexes have not been, or when indexes have been analyzed but not the tables.
You should not force the database to use the cost-based optimizer via inline hints when no statistics are available for any table involved in the SQL.
Using old (obsolete) statistics can be more dangerous than estimating the statistics at runtime, but keep in mind that changing statistics frequently can also blow up in your face, particularly on a mission-critical system with lots of online users. Always back up your statistics before you re-analyze by using DBMS_STATS.EXPORT_SCHEMA_STATS.
Analyzing large tables and their associated indexes with the COMPUTE option will take a long, long time, requiring lots of CPU, I/O, and temporary tablespace resources. It is often overkill. Analyzing with a consistent value, for example, estimate 3%, will usually allow the cost-based optimizer to make optimal decisions
當前統計資訊很重要:
CBO 當在Table 進行分析,但所涉及的Index沒有被分析或是Index被分析但table 沒有分析的情況下所能選擇的執行計劃很少.
你不能強制對於那些沒有統計資訊的Table 所組成的SQL語句採用inhint 方法使用CBO.
使用Old(廢棄的) 統計資訊比在執行是估計統計資訊更危險.但要緊記頻繁改動的統計資訊,也能使你很生氣.
顯然一個大點的系統有很多線上使用者,.在你重新分析時要經常用DBMS_STATS.EXPORT_SCHEMA-STATS來備份你的統計資訊.
用Compute 選項分析大Table 和相關聯的index 將會花費很長時間,需要大量的CPU,I/O 和臨時表空間等資源.
經常太花資源. 分析一致性,例如使用estimate 3% 選項來做CBO將是一個明智選擇.
1.2.2.4 EXPLAIN PLAN for the cost-based optimizer
Oracle provides information on the cost of query execution via the EXPLAIN PLAN facility. EXPLAIN PLAN can be used to display the calculated execution cost(s) via some extensions to the utility. In particular, the plan table's COST column returns a value that increases or decreases to show the relative cost of a query. For example:
oracle 是透過Explain Plan 工具提供基於Cost上的查詢資訊.
Explain Plan 能透過擴充套件功能顯示計算執行的Cost.詳細的說,Plan table的Cost 欄位返回此查詢的花費是增加還是減少. 例如:
EXPLAIN PLAN FOR
SELECT count(*) FROM winners, horses
WHERE winners.owner=horses.owner AND winners.horse_name LIKE 'Mr %'
COLUMN "SQL" FORMAT a56
SELECT lpad(' ',2*level)||operation||'' ||options ||' '||object_name|| decode(OBJECT_TYPE, '', '', '('||object_type||')') "SQL", cost "Cost", cardinality "Num Rows"
FROM plan_table
CONNECT BY prior id = parent_id START WITH id = 0;
SQL Cost Num Rows
-----------------------------------------------
SELECT STATEMENT 44 1
SORT AGGREGATE
HASH JOIN 44 100469
INDEX RANGE SCAN MG1(NON-UNIQUE)
2 1471
INDEX FAST FULL SCAN OWNER_PK(UNIQUE)
4 683
By manipulating the cost-based optimizer (i.e., via inline hints, by creating/removing indexes, or by adjusting the way that indexes or tables are analyzed), we can see the differences in the execution cost as calculated by the optimizer. Use EXPLAIN PLAN to look at different variations on a query, and choose the variation with the lowest relative cost. For absolute optimal performance, many sites have the majority of the tables and indexes analyzed but a small number of tables that are used in isolation are not analyzed. This is usually to force rule-based behavior on the tables that are not analyzed. However, it is important that tables that have not been analyzed are not joined with tables that have been analyzed.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/34596/viewspace-799584/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【cbo計算公式】CBO基本概念(一)公式
- CBO_ORACLEOracle
- Oracle Optimizer CBO RBOOracle
- Oracle CBO 與 RBOOracle
- How restore CBO statisticsREST
- CBO RBO簡介
- CBO成本計算初探
- 【調優】CBO基礎
- cbo機制的研究
- [筆記]CBO的繆論筆記
- 【調優】CBO基礎(八)
- 【調優】CBO基礎(六)
- 【調優】CBO基礎(五)
- 【調優】CBO基礎(四)
- 【調優】CBO基礎(三)
- 【調優】CBO基礎(二)
- Oracle優化器(RBO與CBO)Oracle優化
- CBO的相關原理 系列一
- 【cbo計算公式】No Bind Peeking(五)公式
- 聊聊CBO的連線排列(Join Permutation)
- Oracle最佳化器(RBO與CBO)Oracle
- CBO,RBO在ORACLE中的應用Oracle
- Oracle "腦殘" CBO 最佳化案例Oracle
- 【cbo計算公式】Join 選擇率(六)公式
- SQL效能的度量 - CBO最佳化方式SQL
- oracle聯合索引在CBO下的分析Oracle索引
- Oracle優化器的RBO和CBO方式Oracle優化
- CBO與動態統計量取樣
- 【sql調優】繫結變數與CBOSQL變數
- zt_dbsnake_cbo_sql_從75到6000SQL
- CBO各種型別成本計算公式型別公式
- 【cbo計算公式】單表選擇率(二)公式
- oracle基於cbo成本計算方式說明Oracle
- Oracle最佳化器RBO和CBO(轉載)Oracle
- Oracle的優化器的RBO和CBO方式Oracle優化
- [zt] CBO在查詢中如何計算成本
- 學習筆記--how the cbo evaluates IN-list iterators筆記
- 關於ORACLE自動統計CBO統計資訊Oracle