您好,登錄后才能下訂單哦!
這篇文章主要介紹“數(shù)據(jù)庫(kù)中謂詞越界問(wèn)題分析”,在日常操作中,相信很多人在數(shù)據(jù)庫(kù)中謂詞越界問(wèn)題分析問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”數(shù)據(jù)庫(kù)中謂詞越界問(wèn)題分析”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
開(kāi)發(fā)環(huán)境,碰見(jiàn)一個(gè)謂詞越界的問(wèn)題,模擬這條SQL,如下所示,其中A_ID是表test的外鍵,并且存在索引,
SELECT 1 FROM test WHERE A_ID = 6052138 AND IS_VALID = 1
這張表的數(shù)據(jù)量,大約10萬(wàn),
SQL> select count(*) from test; COUNT(*) ---------- 99044
查看select 1這條SQL的10053,
*************************************** BASE STATISTICAL INFORMATION *********************** Table Stats:: Table: TEST Alias: TEST #Rows: 265702 #Blks: 13157 AvgRowLen: 180.00 ChainCnt: 0.00 Index Stats:: Index: IDX_TEST_01 Col#: 2 LVLS: 2 #LB: 1777 #DK: 119696 LB/K: 1.00 DB/K: 1.00 CLUF: 118505.00 Index: IDX_TEST_02 Col#: 3 LVLS: 2 #LB: 2339 #DK: 381 LB/K: 6.00 DB/K: 272.00 CLUF: 103794.00 Index: IDX_TEST_03 Col#: 7 LVLS: 2 #LB: 786 #DK: 2292 LB/K: 1.00 DB/K: 36.00 CLUF: 82804.00 Index: PK_TEST_ID Col#: 1 LVLS: 2 #LB: 1652 #DK: 265702 LB/K: 1.00 DB/K: 1.00 CLUF: 238444.00 Access path analysis for TEST *************************************** SINGLE TABLE ACCESS PATH Single Table Cardinality Estimation for TEST[TEST] Column (#2): A_ID( AvgLen: 6 NDV: 119696 Nulls: 0 Density: 0.000008 Min: 5586857 Max: 5726449 Column (#60): IS_VALID( AvgLen: 3 NDV: 1 Nulls: 0 Density: 0.000002 Min: 1 Max: 1 Histogram: Freq #Bkts: 1 UncompBkts: 10049 EndPtVals: 1 Using prorated density: 0.000002 of col #2 as selectvity of out-of-range/non-existent value pred Table: TEST Alias: TEST Card: Original: 265702.000000 Rounded: 1 Computed: 0.50 Non Adjusted: 0.50 Access Path: TableScan Cost: 3577.48 Resp: 3577.48 Degree: 0 Cost_io: 3565.00 Cost_cpu: 460365831 Resp_io: 3565.00 Resp_cpu: 460365831 Using prorated density: 0.000002 of col #2 as selectvity of out-of-range/non-existent value pred Access Path: index (AllEqRange) Index: IDX_TEST_01 resc_io: 4.00 resc_cpu: 30301 ix_sel: 0.000002 ix_sel_with_filters: 0.000002 Cost: 4.00 Resp: 4.00 Degree: 1 Best:: AccessPath: IndexRange Index: IDX_TEST_01 Cost: 4.00 Degree: 1 Resp: 4.00 Card: 0.50 Bytes: 0 *************************************** ... CBRID: TEST @ SEL$1 TableLookup allocation - Failure - : disabled by parameter
看見(jiàn)提示,#2這列,即A_ID,對(duì)于超出范圍的、不存在的值,使用0.000002作為選擇率,即這種選擇率,是預(yù)估的值,不是實(shí)際計(jì)算的,換句話說(shuō),有可能對(duì)執(zhí)行成本的計(jì)算,產(chǎn)生偏差,
Using prorated density: 0.000002 of col #2 as selectvity of out-of-range/non-existent value pred
我們從這張表,A_ID字段實(shí)際的存儲(chǔ),看下是否存在他所說(shuō)的,“超出范圍”,
SQL> select min(A_ID), max(A_ID) from TEST; MIN(A_ID) MAX(A_ID) --------- --------- 6006992 6052756
上述結(jié)果展示,A_ID的取值范圍是6006992-6052756,而trace中,標(biāo)記A_ID的min和max則是5586857-5726449,因此,這條SQL,出現(xiàn)了傳說(shuō)中的“謂詞越界”,
Min: 5586857 Max: 5726449
trace中的min和max,怎么得來(lái)的?他是讀取的dba_tab_col_statistics視圖,通過(guò)換算得到的,
SQL> select table_name, column_name, utl_raw.cast_to_number(low_value) low, 2 utl_raw.cast_to_number(high_value) hight 3 from dba_tab_col_statistics 4 WHERE table_name='TEST' AND column_name='A_ID' 5 and owner='BISAL'; TABLE_NAME COLUMN_NAME LOW HIGHT ------------------------ ------------------------------ -------- ----------- TEST A_ID 5586857 5726449
但是慶幸的是,雖然出現(xiàn)了謂詞越界的問(wèn)題,并沒(méi)有因?yàn)槌杀局涤?jì)算偏差,導(dǎo)致CBO選擇錯(cuò)誤的執(zhí)行計(jì)劃,我覺(jué)得和這條SQL的謂詞條件比較簡(jiǎn)單,有一定的關(guān)系,可選擇的執(zhí)行計(jì)劃就這兩種,
SELECT /*+gather_plan_statistics*/ 1 FROM test WHERE A_ID = 6052138 AND IS_VALID = 1 select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST')); Plan hash value: 1000423460 ----------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ----------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 2 |00:00:00.01 | 6 | |* 1 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 1 | 2 |00:00:00.01 | 6 | |* 2 | INDEX RANGE SCAN | IDX_TEST_01 | 1 | 1 | 2 |00:00:00.01 | 4 | ----------------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("IS_VALID"=1) 2 - access("A_ID"=6052138)
因此這個(gè)案例中,雖然出現(xiàn)了“謂詞越界”,對(duì)COST的計(jì)算,會(huì)有誤差,但并未影響執(zhí)行計(jì)劃的選擇,如果是一條謂詞復(fù)雜的SQL,包含多種執(zhí)行計(jì)劃的可能,出現(xiàn)“謂詞越界”,選錯(cuò)執(zhí)行計(jì)劃,形成性能問(wèn)題,就是大概率了。
解決方法,就是重采集統(tǒng)計(jì)信息,以讓COST的計(jì)算,更接近實(shí)際,避免使用默認(rèn)值,讓CBO作出正確選擇。
到此,關(guān)于“數(shù)據(jù)庫(kù)中謂詞越界問(wèn)題分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。