Oracle 11g 新特性 -- 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明
?
?
一.自適應(yīng)游標(biāo)共享(Adaptive Cursor Sharing) 說明
?
1.1 ACS概述
綁定變量使Oracle DB 可以為多條SQL 語句共享單個(gè)游標(biāo),以減少分析SQL 語句所使用的共享內(nèi)存量。然而,游標(biāo)共享和SQL 優(yōu)化是兩個(gè)相互沖突的目標(biāo)。用文字編寫SQL 語句為優(yōu)化程序提供了更多的信息,這無疑會導(dǎo)致更好的執(zhí)行計(jì)劃,但大量的硬分析會導(dǎo)致內(nèi)存和CPU 開銷增加。
?
Oracle9i Database首次嘗試推出了一個(gè)折衷的解決方案:允許共享使用不同文字值的相似SQL 語句。對于使用綁定變量的語句,Oracle9i 還引入了綁定掃視(Bind Peek)概念。使用綁定掃視,優(yōu)化程序會在首次執(zhí)行語句時(shí)查看綁定值。然后,它使用這些值來確定一個(gè)執(zhí)行計(jì)劃,語句的所有其它執(zhí)行均共享該執(zhí)行計(jì)劃。為了從綁定掃視中受益,假定使用游標(biāo)共享,且假定語句的不同調(diào)用使用相同的執(zhí)行計(jì)劃。如果語句的不同調(diào)用從不同的執(zhí)行計(jì)劃中獲得很大收益,則綁定掃視對生成有效的執(zhí)行計(jì)劃就不再有用。
?
一個(gè)計(jì)劃并不總是適用于所有綁定值,為了盡可能解決此問題,Oracle Database 11g 引入了自適應(yīng)游標(biāo)共享。此功能是一項(xiàng)更復(fù)雜的策略,它并不會盲目地共享游標(biāo),如果與分析時(shí)間和內(nèi)存使用量開銷相比,使用多個(gè)執(zhí)行計(jì)劃所帶來的收益更重要,則會為使用綁定變量的每條SQL 語句生成多個(gè)執(zhí)行計(jì)劃。然而,由于使用綁定變量的目的是共享內(nèi)存中的游標(biāo),因此對于需要生成的子游標(biāo)數(shù)目必須采取一種折衷的方法。
?
Adaptive Cursor Sharing 作用如下:
? 通過自適應(yīng)游標(biāo)共享,可以僅針對使用綁定變量的語句智能地共享游標(biāo)。
? 自適應(yīng)游標(biāo)共享用于協(xié)調(diào)游標(biāo)共享和優(yōu)化之間的矛盾。
? 自適應(yīng)游標(biāo)共享具有如下優(yōu)點(diǎn):
– 自動(dòng)檢測不同執(zhí)行受益于不同執(zhí)行計(jì)劃的時(shí)間
– 將生成的子游標(biāo)數(shù)限制到最小
– 是自動(dòng)機(jī)制,無法關(guān)閉
?
1.2 ACS體系結(jié)構(gòu)
?
1.2.1示例
?
?
上圖使用自適應(yīng)游標(biāo)共享執(zhí)行下列步驟:
?
1. 游標(biāo)照常隨硬分析啟動(dòng)。如果發(fā)生綁定掃視(BindPeek),且使用直方圖計(jì)算包含綁定變量的謂詞選擇性,則該游標(biāo)將被標(biāo)記為對綁定敏感的游標(biāo)。此外,還會存儲一些有關(guān)包含綁定變量的謂詞的信息,包括謂詞選擇性。在上圖中,所存儲的謂詞選擇性是一個(gè)以(0.15,0.0025) 為中心的立方體。由于進(jìn)行了初始硬分析,將使用已掃視的綁定確定初始執(zhí)行計(jì)劃。執(zhí)行游標(biāo)后,綁定值和游標(biāo)的執(zhí)行統(tǒng)計(jì)信息存儲在該游標(biāo)中。
當(dāng)使用新的一組綁定值執(zhí)行下一語句時(shí),系統(tǒng)會執(zhí)行常規(guī)軟分析,并查找供執(zhí)行使用的相匹配的游標(biāo)。執(zhí)行結(jié)束時(shí),會將執(zhí)行統(tǒng)計(jì)信息與當(dāng)前存儲在游標(biāo)中的執(zhí)行統(tǒng)計(jì)信息進(jìn)行比較。然后,系統(tǒng)觀察所有先前運(yùn)行的統(tǒng)計(jì)信息模式并確定是否將游標(biāo)標(biāo)記為能識別綁定的游標(biāo)。
?
2. 下一次對此查詢進(jìn)行軟分析時(shí),如果游標(biāo)能夠識別綁定,則會使用能識別綁定的游標(biāo)匹配。假定具有新的一組綁定值的謂詞選擇性現(xiàn)在是(0.18,0.003)。由于選擇性用作能識別綁定的游標(biāo)匹配的一部分,并且該選擇性位于現(xiàn)有立方體中,因此該語句使用現(xiàn)有子游標(biāo)的執(zhí)行計(jì)劃運(yùn)行。
?
3. 下一次對此查詢進(jìn)行軟分析時(shí),假設(shè)具有一組新綁定值的謂詞選擇性是(0.3,0.009)。由于該選擇性不在現(xiàn)有立方體中,所以找不到子游標(biāo)匹配項(xiàng)。因此,系統(tǒng)會執(zhí)行硬分析,在本例中生成了一個(gè)具有第二個(gè)執(zhí)行計(jì)劃的新子游標(biāo)。此外,新選擇性立方體將存儲為該新子游標(biāo)的一部分。執(zhí)行該新子游標(biāo)后,系統(tǒng)會將綁定值和執(zhí)行統(tǒng)計(jì)信息存儲在該游標(biāo)中。
?
4. 下一次對此查詢進(jìn)行軟分析時(shí),假設(shè)具有一組新綁定值的謂詞選擇性是(.28,0.004)。由于該選擇性不在現(xiàn)有的某個(gè)立方體中,系統(tǒng)將執(zhí)行硬分析。假設(shè)此時(shí)硬分析生成與第一個(gè)執(zhí)行計(jì)劃相同的執(zhí)行計(jì)劃。因?yàn)樵撚?jì)劃與第一個(gè)子游標(biāo)相同,所以將合并這兩個(gè)子游標(biāo)。也就是說,這兩個(gè)立方體將合并為一個(gè)較大的新立方體,并刪除其中一個(gè)子游標(biāo)。下次執(zhí)行軟分析時(shí),如果選擇性位于該新立方體中,子游標(biāo)將匹配。
?
?
1.2.2 說明
在Oracle 10g 和11g中對綁定變量的處理,已經(jīng)有所不同, 在Oracle 10g中,綁定變量相對比較簡單,當(dāng)使用綁定變量的SQL 第一次執(zhí)行時(shí),會進(jìn)行硬解析,生成plan 和cursor。 在這個(gè)過程中,Oracle 會使用bind peeking,即將綁定變量的值帶入,從而選擇最優(yōu)的一個(gè)plan。以后每次執(zhí)行都使用這個(gè)plan。
在以后的執(zhí)行時(shí),如果因?yàn)槠渌驅(qū)е耤ursor 不可重用,那么就會生成一個(gè)child_cursor. 這個(gè)cursor 不可重用的原因可以查看:v$sql_shared_cursor視圖。
?
那么這就有一個(gè)問題。如果列上有列上有嚴(yán)重的數(shù)據(jù)傾斜,某個(gè)字段中99%是值1,1%是值0. 當(dāng)我們用0 來進(jìn)行peeking的時(shí)候,這時(shí)候會走索引,并且以后的所有plan 都是使用這個(gè)。 如果我們的綁定值變成了1. 這個(gè)時(shí)候,明顯走全表掃描比索引劃算。
但是Oracle 10g 下還是會使用第一次的plan,即使這個(gè)plan 不是最優(yōu)的。所以在Oracle 10g下,如果數(shù)據(jù)存在數(shù)據(jù)傾斜,那么最好不要使用綁定變量。
?
在Oracle 11g 以后在綁定變量這塊有所以改變,會生成一個(gè)范圍值的執(zhí)行計(jì)劃。然后每次傳變量進(jìn)去就對比范圍,選擇最優(yōu)的執(zhí)行計(jì)劃。與這個(gè)功能相關(guān)的參數(shù)保存在v$sql視圖中:is_bind_sensitive,is_bind_aware,is_shareable。這幾個(gè)字段,在Oracle 10g的v$sql 視圖里是沒有的。
?
我們這里要說明的Adaptive Cursor Sharing特性,其允許一個(gè)使用綁定變量的SQL語句使用多個(gè)執(zhí)行計(jì)劃。對于同一個(gè)SQL, 為了得到合適的查詢,oracle 會監(jiān)控使用不同綁定變量的情況,已確保對不同綁定變量值的cursor(執(zhí)行計(jì)劃)都是最優(yōu)的。比如因?yàn)閿?shù)據(jù)傾斜的原因?qū)壎ㄗ兞恐礎(chǔ) 使用執(zhí)行計(jì)劃A,對綁定變量值B 使用執(zhí)行計(jì)劃B。雖然他們的SQL 是相同的,但執(zhí)行計(jì)劃不同。
?
Adaptive Cursor Sharing 默認(rèn)啟動(dòng)的。不過要注意的是,該特性只有在綁定變量的參數(shù)個(gè)數(shù)不超過14個(gè)的情況才有效。
?
有關(guān)Oracle 10g和11g 綁定變量更多區(qū)別參考:
Oracle 10g 與 11g 綁定變量(Bind Variable) 區(qū)別 說明
http://blog.csdn.net/tianlesoftware/article/details/6591222
?
1.3自適應(yīng)游標(biāo)共享視圖
?
1.3.1 V$SQL 中已新增了兩個(gè)新列
(1)IS_BIND_SENSITIVE:指示游標(biāo)是否為對綁定敏感,值為YES | NO。符合以下情況的查詢稱為對綁定敏感的查詢:計(jì)算謂詞選擇性時(shí)優(yōu)化程序?yàn)槠鋻咭暯壎ㄗ兞恐?#xff0c;并且綁定變量值的更改可能導(dǎo)致不同計(jì)劃。
(2)IS_BIND_AWARE:指示游標(biāo)是否為能標(biāo)識綁定的游標(biāo),值為YES | NO。游標(biāo)高速緩存中已標(biāo)記為使用能識別綁定的游標(biāo)共享的游標(biāo)稱為能標(biāo)識綁定的游標(biāo)。
?
1.3.2 V$SQL_CS_HISTOGRAM
顯示跨三個(gè)存儲桶執(zhí)行歷史記錄直方圖的執(zhí)行計(jì)數(shù)的分布情況。
?
1.3.3 V$SQL_CS_SELECTIVITY
顯示為包含綁定變量且在游標(biāo)共享檢查中使用了其選擇性的每個(gè)謂詞存儲在游標(biāo)中的選擇性立方體或范圍。它包含謂詞文本和選擇性范圍的下限值和上限值。
?
1.3.4 V$SQL_CS_STATISTICS
自適應(yīng)游標(biāo)共享監(jiān)視查詢的執(zhí)行,并在一段時(shí)間內(nèi)收集相關(guān)的信息,使用此信息可確定是否切換到對該查詢使用能識別綁定的游標(biāo)。該視圖匯總了所收集的信息以讓您作出以下決定:對于執(zhí)行示例,它跟蹤已處理的行數(shù)、緩沖區(qū)獲取數(shù)和CPU 時(shí)間。如果使用綁定集來構(gòu)建游標(biāo),則PEEKED 列的值為YES,否則為NO。
?
?
二.MOS 說明
MOS上對ACS的說明:
Adaptive Cursor Sharing in 11G[ID 836256.1]
Adaptive Cursor SharingOverview [ID 740052.1]
?
2.1 Introduction? 介紹
With the introduction of theCBO a number of changes were made to calculate the selectivity of a predicate, whichin turn affected how the query was optimized.? The selectivity was basedon the number of distinct values for a given column or a predefined percentageof rows depending on the relational operator that was used. This worked wellfor data that was evenly distributed but had limitations in applications wheredata was skewed.
?
Note:68992.1?"PredicateSelectivity".
?
With 9i a new feature"Bind Peeking" was introduced to try to get around the issuesassociated with guessing the selectivity of the bind. This meant that duringhard parsing of a query using bind variables, we would peek at the binds andgenerate selectivity based on the bind and the underlying column statistics. Thismethod could sometimes lead to plans being generated that were notrepresentative of the general query usage if the bind selectivity of theinitial execution of a statement varied from the selectivity of subsequentexecutions with different sets of binds.
?
In Oracle 10g, this wasespecially noticeable as the default statistical gathering methodology changedto gather histograms automatically. This meant that selectivity that previouslyused only a formula based on the number of distinct values was now generatedbased on histograms, which gave the optimizer better information about thedistribution data that was skewed.?
?
The impact of this was that endusers were reliant on the first execution of a query using binds that wouldgenerate an execution plan that was representative of the general query usage.
Note:387394.1?Query usingBind Variables is suddenly slow
Note:430208.1?Bind PeekingBy Example
In Oracle 11g Adaptive CursorSharing has been introduced to get around some of these? issues. Thisfeature monitors the execution statistics for candidates queries and makes itpossible for? the same query to generate and use different execution plansfor different set of binds values.
--在Oracle 11g中引入了AdaptiveCursor Sharing 特性,該特性監(jiān)控查詢語句執(zhí)行的統(tǒng)計(jì)信息,并盡可能的根據(jù)相同的SQL語句,不同的綁定變量值,使用不同的執(zhí)行計(jì)劃。
2.2 Advantages of Adaptive Cursor Sharing(ACS的優(yōu)勢)
Adaptive cursor sharing is asolution to give us the shareability of binds, with the plan adaptability of literals.With adaptive cursor sharing the database can adapt execution plans based onthe selectivity of values used in binds.
This is a workaround to issueswhere different sets of bind values for a given query may have differentselectivity, leading to a situation where there may be suboptimal plans fordifferent bind sets.
?
Previously, workaroundsemployed would either use literal values instead of binds, which could lead toexcessive hard parsing, or apply a fixed plan that would be a compromise ofperformance between different bind selectivities.
2.3 Disadvantages of Adaptive Cursor Sharing(ACS的劣勢)
There is some extra overheadassociated with Adaptive Cursor Sharing in the form of :-
(1)More Hard Parses (CPU) -Extra Hard Parses will be required when a cursor becomes "Bind Aware"as we attempt to generate the better matched execution plans for the bindselectivity.
(2) More Child Cursors(SGA) - It is recommended that some consideration be taken to increase the sizeof the shared_pool on upgrade from 10g to 11g, given the extra cursors that maybe required to? accommodate this feature.
(3)More Work to Match theCursor (CPU) - More Child Cursors and the requirement to match a query to thebest execution plan for its predicate selectivity.
?
2.4 Extended Cursor Sharing ( Bind Sensitivity)
When a query is executed withbind peeking and binds using either one of the following relational operators =< > <= >= !=, or a user defined bind operator e.g.contains(e.job,:job,1)>0, and a change? in the bind variable value maylead to a different plan, the cursor will be marked as bind sensitive.
??? --當(dāng)我們在SQL中使用<= 等操作時(shí),在改變綁定變量就會生成一個(gè)不同的執(zhí)行計(jì)劃(Cursor),并將這個(gè)cursor標(biāo)記為bind sensitive,Bind-Sensitive Cursor是根據(jù)綁定變量值得到的最優(yōu)執(zhí)行計(jì)劃的一個(gè)cursor。這個(gè)就是ECS。
?
The "LIKE" operatoris supported from 11.2.0.2 onwards.
?
Apart from checking for a validoperator there are also a number of subsequent bind sensitivity checks thatneed to be performed before it can be marked as bind sensitive, if it fails anyof these the cursor will not be marked as bind sensitive and adaptive cursorsharing would not occur.??
?
If any of the following checks fail ECS will bedisabled :
--在以下情況會禁用ECS:
(1)Extended cursor sharingis disabled
(2)The query has no binds
(3)Parallel query is used
(4)Certain parameters like("bind peeking"=false) are set?
(5)You are using a /*+NO_BIND_AWARE */ hint
(6)Outlines are being used
(7)It is a recursive query
(8)The number of binds in agiven sql statement are greater than 14.
這里綁定變量個(gè)數(shù)不能超過14個(gè),因?yàn)锳CS 在綁定變量超過14個(gè)的時(shí)候會失效。
?
When using SQL Plan Baselines,and there is more than one plan enabled,? ACS will still be enabled to usethose plans.
?
When all the criteria are metthe cursor is marked as bind sensitive and a "sharing context"containing information about execution statistics of the cursor is created andstored against the cursor.
?
Cursors that are marked asbind-sensitive can been identified by the column IS_BIND_SENSITIVE=Y in V$SQL orV$SQLAREA.
?
2.5 Adaptive Cursor Sharing (Bind Aware)
If there is significantvariation in the row source cardinality for executions of the same sqlstatement in consecutive executions a cursor will be marked as bind aware.
?
For more information about this please see :-
Note:836256.1?AdaptiveCursor Sharing in 11G
?
2.6 Monitoring
V$SQL can be used to see if acursor is_bind_sensitive, is_bind_aware, or is_shareable.
The bind context informationcan be viewed via V$SQL_CS_SELECTIVITY, V$SQL_CS_STATISTICS andV$SQL_CS_HISTOGRAM
?
V$SQL_CS_SELECTIVITY exposesthe valid selectivity ranges for a child cursor in extended?cursor sharingmode. A valid range consists of a low and high value for each predicatecontaining binds. Each predicate's selectivity (with the current bind value)must?fall between the corresponding low and high values in order for thechild cursor to be shared.
?
V$SQL_CS_STATISTICS containsthe raw execution statistics used by the monitoring component? of adaptivecursor sharing. A sample of the executions is monitored.
?
This view exposes whichexecutions were sampled, and what the statistics were for those? executions.The statistics are cumulative for each distinct set of bind values.
?
V$SQL_CS_HISTOGRAM summarizesthe monitoring information stored by adaptive cursor? sharing. Thisinformation is used to decide whether to enable extended cursor sharing for aquery. It? is stored in a histogram, whose bucket's contents are exposedby this view.
?
2.7 Issues with Excessive Child Cursors
There is also a possibilitythat Adaptive Cursor Sharing may compound problems in whichexcessive?numbers of child cursors are generated.? This may lead toperformance degradation as large?numbers of child cursor can put spacepressure on the shared pool, and may also lead to an increase in mutex X waitsfor that cursor.
?
Things to check.
1. Ensure that cursor_sharing is not set to SIMILAR. In 11g, this setting isnot recommended and?this parameter will eventually be deprecated.
Note:1169017.1:ANNOUNCEMENT:Deprecating the cursor_sharing = 'SIMILAR' setting
2. If there are high version counts check v$sql_shared_cursor. And search MyOracle Support for notes that may allude to the cause of the excessive cursors.
?Note:438755.1?:Formated V$SQL_SHARED_CURSOR Report by SQLID or Hash Value
?
If there are still excessivechild cursors, then Oracle Support should be contacted to assist withdiagnosing the issue.
?
三.ACS 啟用與關(guān)閉
?
與ACS 相關(guān)的3個(gè)參數(shù)是:
_optimizer_adaptive_cursor_sharing
_optimizer_extended_cursor_sharing
_optimizer_extended_cursor_sharing_rel
?
在Oracle 11gR2的參考手冊里并沒有搜到這個(gè)參數(shù)的解釋,不過我們可以通過查看Oracle 的參數(shù)來了解這3個(gè)參數(shù)的作用。
?
??? 這里我們要用到一個(gè)視圖:all_parameters. 該視圖的源碼參考:
Oracle all_parameters 視圖
http://blog.csdn.net/tianlesoftware/article/details/6641281
?
SQL>select * from all_parameters where name like '%_optimizer_%_cursor_sharing%';
?
返回結(jié)果:
?
?
我們可以在db 運(yùn)行時(shí)修改這3個(gè)參數(shù),并且能即時(shí)生效。
?
關(guān)閉ACS的操作如下:
SQL> alter session set"_optimizer_extended_cursor_sharing_rel"=none;
SQL> alter session set"_optimizer_extended_cursor_sharing"=none;
SQL> alter session set"_optimizer_adaptive_cursor_sharing"=false;
?
?
關(guān)于ACS的問題,itpub上的 viadeazhu 有個(gè)更深入的研究,寫得非常詳細(xì),網(wǎng)址如下:http://space.itpub.net/15415488/viewspace-621535
?
?
關(guān)于Oracle 10g和11g中這些參數(shù)值的變化,MOS上有篇文檔說明:
PARAMETERS TO CHANGE 11.2.0.1 TO 10.2.0.4 [ID1274553.1]
?
These are the parameters thatare changed when setting optimizer_features_enable=10.2.0.4 in 11.2.0.1database. These values represent OFE=10.2.0.4 and -- is the 11.2.0.1 value
?
alter session set "_optimizer_undo_cost_change"= '10.2.0.4'; -- 11.2.0.1
alter session set "_optimizer_null_aware_antijoin" = false; -- true
alter session set "_optimizer_extend_jppd_view_types" = false; --true
alter session set "_replace_virtual_columns" = false; -- true
alter session set "_first_k_rows_dynamic_proration" = false; --true?
alter session set "_bloom_pruning_enabled" = false; -- true
alter session set "_optimizer_multi_level_push_pred" = false; --true?
alter session set "_optimizer_group_by_placement" = false; -- true
alter session set "_optimizer_extended_cursor_sharing_rel" = none; --simple
alter session set "_optimizer_adaptive_cursor_sharing" = false; --true
alter session set "_optimizer_improve_selectivity" = false ; -- true
alter session set "_optimizer_enable_density_improvements" = false;-- true
alter session set "_optimizer_native_full_outer_join" = off; -- force
alter session set "_optimizer_enable_extended_stats" = false; -- true
alter session set "_nlj_batching_enabled" = 0; -- 1
alter session set "_optimizer_extended_stats_usage_control" = 255; --224
alter session set "_bloom_folding_enabled" = false; -- true
alter session set "_optimizer_coalesce_subqueries" = false; -- true
alter session set "_optimizer_fast_pred_transitivity" = false; -- true
alter session set "_optimizer_fast_access_pred_analysis" = false; --true
alter session set "_optimizer_unnest_disjunctive_subq" = false; --true
alter session set "_optimizer_unnest_corr_set_subq" = false; -- true
alter session set "_optimizer_distinct_agg_transform" = false; --true
alter session set "_aggregation_optimization_settings" = 32; -- 0
alter session set "_optimizer_connect_by_elim_dups" = false; -- true
alter session set "_optimizer_eliminate_filtering_join" = false; --true?
alter session set "_connect_by_use_union_all" = old_plan_mode; --true
alter session set "_optimizer_join_factorization" = false; -- true
alter session set "_optimizer_use_cbqt_star_transformation" = false;-- true
alter session set "_optimizer_table_expansion" = false ; -- true
alter session set "_and_pruning_enabled" = false ; -- true
alter session set "_optimizer_distinct_placement" = false ; -- true
alter session set "_optimizer_use_feedback" = false ; -- true
alter session set "_optimizer_try_st_before_jppd" = false ; -- true
?
?
?
?
?
?
-------------------------------------------------------------------------------------------------------
版權(quán)所有,文章允許轉(zhuǎn)載,但必須以鏈接方式注明源地址,否則追究法律責(zé)任!
Skype: ???tianlesoftware
QQ:???? ? tianlesoftware@gmail.com
Email: ???tianlesoftware@gmail.com
Blog:?? ? http://blog.csdn.net/tianlesoftware
Weibo: ???http://weibo.com/tianlesoftware
Twitter:? http://twitter.com/tianlesoftware
Facebook: http://www.facebook.com/tianlesoftware
Linkedin: http://cn.linkedin.com/in/tianlesoftware
?轉(zhuǎn)載于:https://www.cnblogs.com/tianlesoftware/archive/2012/05/16/3609291.html
總結(jié)
以上是生活随笔為你收集整理的Oracle 11g 新特性 -- 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js的with语句使用方法
- 下一篇: 【jquery模仿net控件】初步Gri