Profilo di haiOracle & StarcraftBlogElenchi Strumenti Guida
28 dicembre

dynamic sampling

dynamic sampling对于我们来说其实不算太陌生,从9i r2开始,dynamic sampling其实已经不动声色地融入到数据库中了。
我们经常会碰到由于一些表没有分析导致执行计划错误的情况,但是dynamic sampling的出现一定程度的减少了错误的产生。dynamic sampling针对没有分析过的表可以采样估计表的选择性,对于生产正确的执行计划有一定的帮助。
 
dynamic sampling分为10个级别,从0-10,由参数optimizer_dynamic_sampling控制
 
下面是取自"Performance Tuning Guide and Reference "的对10种级别的定义
 
  • Level 0: Do not use dynamic sampling.
  • Level 1: Sample all tables that have not been analyzed if the following criteria are met: (1) there is at least 1 unanalyzed table in the query; (2) this unanalyzed table is joined to another table or appears in a subquery or non-mergeable view; (3) this unanalyzed table has no indexes; (4) this unanalyzed table has more blocks than the number of blocks that would be used for dynamic sampling of this table. The number of blocks sampled is the default number of dynamic sampling blocks (32).
  • Level 2: Apply dynamic sampling to all unanalyzed tables. The number of blocks sampled is the default number of dynamic sampling blocks.
  • Level 3: Apply dynamic sampling to all tables that meet Level 2 criteria, plus all tables for which standard selectivity estimation used a guess for some predicate that is a potential dynamic sampling predicate. The number of blocks sampled is the default number of dynamic sampling blocks.
  • Level 4: Apply dynamic sampling to all tables that meet Level 3 criteria, plus all tables that have single-table predicates that reference 2 or more columns. The number of blocks sampled is the default number of dynamic sampling blocks.
  • Level 5: Apply dynamic sampling to all tables that meet the Level 4 criteria using 2 times the default number of dynamic sampling blocks.
  • Level 6: Apply dynamic sampling to all tables that meet the Level 5 criteria using 4 times the default number of dynamic sampling blocks.
  • Level 7: Apply dynamic sampling to all tables that meet the Level 6 criteria using 8 times the default number of dynamic sampling blocks.
  • Level 8: Apply dynamic sampling to all tables that meet the Level 7 criteria using 32 times the default number of dynamic sampling blocks.
  • Level 9: Apply dynamic sampling to all tables that meet the Level 8 criteria using 128 times the default number of dynamic sampling blocks.
  • Level 10: Apply dynamic sampling to all tables that meet the Level 9 criteria using all blocks in the table.
dynamic_sampling hint的级别定义如下
 
  • Level 0: Do not use dynamic sampling.
  • Level 1: The number of blocks sampled is the default number of dynamic sampling blocks (32).
  • Level 2: The number of blocks sampled is 2 times the default number of dynamic sampling blocks.
  • Level 3: The number of blocks sampled is 4 times the default number of dynamic sampling blocks.
  • Level 4: The number of blocks sampled is 8 times the default number of dynamic sampling blocks.
  • Level 5: The number of blocks sampled is 16 times the default number of dynamic sampling blocks.
  • Level 6: The number of blocks sampled is 32 times the default number of dynamic sampling blocks.
  • Level 7: The number of blocks sampled is 64 times the default number of dynamic sampling blocks.
  • Level 8: The number of blocks sampled is 128 times the default number of dynamic sampling blocks.
  • Level 9: The number of blocks sampled is 256 times the default number of dynamic sampling blocks.
  • Level 10: Read all blocks in the table.

 

下面做一些测试

 

SQL> create table test as select object_id a from dba_objects;

Table created.

 

SQL>  show parameter optimizer_dynamic_sampling

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling           integer     1


SQL> set autotrace trace explain;


SQL> select count(*) from test;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   SORT (AGGREGATE)
   2    1     TABLE ACCESS (FULL) OF 'TEST'

 

SQL> alter session set optimizer_dynamic_sampling=10;

Session altered.

 

SQL> select count(*) from test;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   SORT (AGGREGATE)
   2    1     TABLE ACCESS (FULL) OF 'TEST'


dynamic sampling没有起作用,因为在9i里面dynamic sampling需要在多表关联的语句里面才会起作用。在10g里面optimizer_dynamic_sampling在2及以上级别时单表就能dynamic sampling。

 

SQL 10G>alter session set optimizer_dynamic_sampling=2;

Session altered.

 

SQL 10G>select count(*) from test;

 

Execution Plan
----------------------------------------------------------
Plan hash value: 1950795681

-------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    21   (5)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |            |          |
|   2 |   TABLE ACCESS FULL| TEST | 48065 |    21   (5)| 00:00:01 |
-------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement

 

 

看看关联查询的情况

 

SQL> create table test1 as select * from test;

Table created.

 

SQL> select count(*) from test a,test1 b where a.a=b.a;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE
   1    0   SORT (AGGREGATE)
   2    1     MERGE JOIN
   3    2       SORT (JOIN)
   4    3         TABLE ACCESS (FULL) OF 'TEST1'
   5    2       SORT (JOIN)
   6    5         TABLE ACCESS (FULL) OF 'TEST'

 

如果两个表都没分析,那么dynamic sampling不起作用,分析其中一个表

 

SQL> analyze table test1 compute statistics;

Table analyzed.

 

SQL> select count(*) from test a,test1 b where a.a=b.a;

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=19 Card=1 Bytes=17)
   1    0   SORT (AGGREGATE)
   2    1     HASH JOIN (Cost=19 Card=23706 Bytes=403002)
   3    2       TABLE ACCESS (FULL) OF 'TEST1' (Cost=6 Card=23706 Byte
          s=94824)

   4    2       TABLE ACCESS (FULL) OF 'TEST' (Cost=6 Card=23706 Bytes
          =308178)

 

dynamic sampling起作用了。

 

在10g里面有点区别,即使2个表都没分析过,dynamic sampling也可以起作用

 

SQL 10G>alter session set optimizer_dynamic_sampling=1;

Session altered.

 

SQL 10G>select count(*) from test a,test1 b where a.a=b.a;

Execution Plan
----------------------------------------------------------
Plan hash value: 2909046986

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

| Id  | Operation           | Name  | Rows  | Bytes |TempSpc| Cost (%CPU)| Time
    |

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

|   0 | SELECT STATEMENT    |       |     1 |    26 |       |   151   (4)| 00:00
:02 |

|   1 |  SORT AGGREGATE     |       |     1 |    26 |       |            |
    |

|*  2 |   HASH JOIN         |       | 43244 |  1097K|  1056K|   151   (4)| 00:00
:02 |

|   3 |    TABLE ACCESS FULL| TEST1 | 43235 |   548K|       |    21   (5)| 00:00
:01 |

|   4 |    TABLE ACCESS FULL| TEST  | 45439 |   576K|       |    21   (5)| 00:00
:01 |

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


Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("A"."A"="B"."A")

 

Note
-----
   - dynamic sampling used for this statement

 

其实归根到底是由于10g没有了基于规则的优化器,在9i里面如果一个查询中的所有表没有统计数据,那么他将选择基于规则的优化器而忽略dynamic sampling,而在10g因为这个原因所以dynamic sampling生效。

 

 

Commenti (7)

Attendere...
Il commento immesso è troppo lungo. Immetti un commento più breve.
Immissione non effettuata. Riprova.
Impossibile aggiungere il commento al momento. Riprova più tardi.
Per aggiungere un commento è necessaria l'autorizzazione di un genitore. Chiedi autorizzazione
I tuoi genitori hanno disattivato i commenti.
Impossibile eliminare il commento al momento. Riprova più tardi.
Hai raggiunto il numero massimo di commenti pubblicabili giornalmente. Riprova tra 24 ore.
Impossibile lasciare commenti. La funzionalità è stata disattivata perché i sistemi hanno rilevato una possibile attività di spamming dal tuo account. Se ritieni che il tuo account è stato disattivato per errore, contatta il supporto tecnico di Windows Live.
Esegui il seguente controllo di protezione per completare la pubblicazione del commento.
I caratteri digitati nel controllo di protezione devono corrispondere ai caratteri dell'immagine o della riproduzione audio.

Per aggiungere un commento, accedi con il tuo Windows Live ID (se utilizzi Hotmail, Messenger o Xbox LIVE possiedi già un Windows Live ID). Accedi


Non hai ancora un Windows Live ID? Registrati

jyjikyui fhtha scritto:
http://www.topbattery.com.au/dell/latitude-d531.htm dell latitude d531 battery
http://www.topbattery.com.au/dell/d9200.php Notebook Laptop Battery for Dell D9200 D5318 G5260 laptop battery ,
http://www.topbattery.com.au/dell/inspiron-1200.htm dell inspiron 1200 battery
http://www.topbattery.com.au/toshiba/satellite-p200.htm toshiba satellite p200 battery
http://www.topbattery.com.au/toshiba/satellite-p205.htm toshiba satellite p205 battery
http://www.topbattery.com.au/toshiba/pa3534u-1brs.htm toshiba pa3534u-1brs battery
http://www.topbattery.com.au/toshiba/satellite-a205.htm toshiba satellite a205 battery
http://www.topbattery.com.au/toshiba/satellite-a100.htm toshiba satellite a100 battery
http://www.topbattery.com.au/toshiba/satellite-a105.htm toshiba satellite a105 battery
http://www.topbattery.com.au/toshiba/satellite-a80.htm toshiba satellite a80 battery
http://www.topbattery.com.au/toshiba/satellite-m110.htm toshiba satellite m110 battery
http://www.topbattery.com.au/acer/travelmate-4200.htm acer travelmate 4200 battery ,
http://www.topbattery.com.au/dell/inspiron-1100-series.htm dell inspiron 1100 series battery ,
http://www.topbattery.com.au/toshiba/satellite-a205.htm toshiba satellite a205 battery
http://www.topbattery.com.au/acer/3ur18650y-2-qc236.htm acer 3ur18650y-2-qc236 battery
http://www.topbattery.com.au/asus/904ha.htm asus 904ha battery
http://www.topbattery.com.au/hp/b2800.htm hp b2800 battery
http://www.topbattery.com.au/hp/a32.htm hp a32 battery
http://www.topbattery.com.au/hp/m9.htm hp m9 battery
http://www.topbattery.com.au/toshiba/pa3331u-1brs.htm toshiba pa3331u-1brs battery
http://www.topbattery.com.au/toshiba/pa3331u-1bas.htm toshiba pa3331u-1bas battery
http://www.topbattery.com.au/toshiba/satellite-m30.htm toshiba satellite m30 battery
http://www.topbattery.com.au/toshiba/pa3480u-1brs.htm toshiba pa3480u-1brs battery
http://www.topbattery.com.au/toshiba/p100.htm toshiba p100 battery
http://www.topbattery.com.au/toshiba/p105.htm toshiba p105 battery
http://www.topbattery.com.au/toshiba/pa3479u-1brs.htm toshiba pa3479u-1brs battery
http://www.topbattery.com.au/hp/4411.htm hp 4411 battery
3 Nov.
29 Ago.
Senza nomeha scritto:

Hi,Do you have used lcd screens, lcd monitor used, surplus lcds and scrap LCDs? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels. website:www.sstar-hk.com[hghhagefibfiji]

23 Set.
Senza nomeha scritto:
A friend to buy wow goldTo wow power leveling?On the world's most concessions to the most reputable sites under the single!
17 Set.
Senza nomeha scritto:

Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of screen advertisings, digital sign, digital signages and LCDs. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

amberdigital Contact Us
Southern Stars Enterprises Co (Hong Kong Office)
Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
Tel:+852 2681 4099
Fax:+852 2681 4586

Southern Stars Enterprises Co (Shenzhen Office)
Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
Tel:+86 755 2592 9100
Fax:+86 755 2592 7171

E-mail:sstar@netvigator.com
website:www.amberdigital.com.hk
alibaba:amberdigital.en.alibaba.com[chgbhg

28 Ago.
Immagine di Anonimo
wanghai ha scritto:
这个问题很难给出一个简单的答案,执行的速度跟oracle的版本,你的sql,还有当时系统的负载,很多种原因有关联,你说的这些情况我也都碰到过,cbo出问题的话还是10053结合v$session_wait去找出原因。
14 Gen.
Immagine di Anonimo
柔嘉维则 ha scritto:
帅哥,请教个问题。
所谓的cbo,它有时候可能反而比rbo产生的执行计划更糟糕,比如说我可以理解rbo下跑1分钟的sql,在cbo下要跑10分钟,但是我不能理解它怎么可以一天也不见反应。前两天我分析了表,结果平常几分钟的sql从清晨跑到了晚上也不见出来,最后我把统计信息删了才好。
我如何才能继续用分析统计信息,并且让sql跑回来给我结果呢?它为什么要说倾向于用cbo?如果没有一个好一点的解释,我就是想不明白,并且不能原谅cbo的这种算法。
10 Gen.

Riferimenti

L'URL di riferimento per questo intervento è:
http://wzwanghai.spaces.live.com/blog/cns!56626E237AFBD116!290.trak
Blog che fanno riferimento a questo intervento
  • Nessuno