Oracle数据库数据恢复、性能优化

找回密码
注册
搜索
热搜: 活动 交友 discuz
发新帖

133

积分

0

好友

17

主题
1#
发表于 2012-6-11 12:04:25 | 查看: 9328| 回复: 7
之前发帖子http://t.askmaclean.com/thread-1159-1-1.html
知道scheduler job :BSLN_MAINTAIN_STATS_JOB是一个维护baseline的job,所以就学了一下11g的特性baseline

内容有点多,全贴出来太乱了,结贴的时候,再贴出来吧,内容全在txt里面。也方便看一下。最后把后缀改为.sql,用Uedit32等文本编辑工具,有高亮部分,容易看一些。

版本:

13:23:18 saup@PRI11G> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE
11.2.0.2.0
Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production


$ uname -a
Linux dbaone 2.6.18-238.el5 #1 SMP Sun Dec 19 14:22:44 EST 2010 x86_64 x86_64 x86_64 GNU/Linux

--这么做是因为测试机器,装10g的时候注释做的
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 4.6 (Tikanga)
#Red Hat Enterprise Linux Server release 5.6 (Tikanga)



关于数据倾斜data skew,绑定变量窥视

SQL> create test2 as select object_id as id,object_name from dba_objects;

SQL> create index test2_idx on test2(id)

11:02:04 saup@PRI11G> select count(1) from test2;

  COUNT(1)
----------
     73173


11:02:25 saup@PRI11G> select max(id) from test2;

   MAX(ID)
----------
     83061

saup@PRI11G> update test2 set id=1 where rownum<=70000;

saup@PRI11G> commit;

11:02:19 saup@PRI11G> select count(1) from test2 where id=1;

  COUNT(1)
----------
     70000

saup@PRI11G> exec dbms_stats.gather_table_stats('saup','test2',method_opt => 'for columns id size 254',cascade => true);

--开启自动获取
saup@PRI11G>alter system set OPTIMIZER_CAPTURE_PLAN_BASELINES=true;


saup@PRI11G>alter system flush shared_pool;

[ 本帖最后由 saup007 于 2012-6-11 18:05 编辑 ]

base_saup007_test.txt

20.77 KB, 下载次数: 883

2#
发表于 2012-6-11 12:08:09
请格式化你的输出  至少说明你做测试的版本

回复 只看该作者 道具 举报

3#
发表于 2012-6-11 13:09:31

回复 2# 的帖子

好的,我编辑好的贴进来就乱了。

吃饭回来,现在编辑。。

版本11.2.0.2

回复 只看该作者 道具 举报

4#
发表于 2012-6-11 22:21:53
完整的问题 贴出
  1. 之前发帖子http://t.askmaclean.com/thread-1159-1-1.html
  2. 知道scheduler job :BSLN_MAINTAIN_STATS_JOB是一个维护baseline的job,所以就学了一下11g的特性baseline

  3. 我整理格式的时候,又做了一次。结果如下:

  4. 版本:

  5. 13:23:18 saup@PRI11G> select * from v$version;

  6. BANNER
  7. --------------------------------------------------------------------------------
  8. Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
  9. PL/SQL Release 11.2.0.2.0 - Production
  10. CORE        11.2.0.2.0        Production
  11. TNS for Linux: Version 11.2.0.2.0 - Production
  12. NLSRTL Version 11.2.0.2.0 - Production


  13. $ uname -a
  14. Linux dbaone 2.6.18-238.el5 #1 SMP Sun Dec 19 14:22:44 EST 2010 x86_64 x86_64 x86_64 GNU/Linux

  15. --这么做是因为测试机器,装10g的时候注释做的
  16. $ cat /etc/redhat-release
  17. Red Hat Enterprise Linux Server release 4.6 (Tikanga)
  18. #Red Hat Enterprise Linux Server release 5.6 (Tikanga)



  19. 关于数据倾斜data skew,绑定变量窥视

  20. SQL> create test2 as select object_id as id,object_name from dba_objects;

  21. SQL> create index test2_idx on test2(id)

  22. 11:02:04 saup@PRI11G> select count(1) from test2;

  23.   COUNT(1)
  24. ----------
  25.      73173


  26. 11:02:25 saup@PRI11G> select max(id) from test2;

  27.    MAX(ID)
  28. ----------
  29.      83061

  30. saup@PRI11G> update test2 set id=1 where rownum<=70000;

  31. saup@PRI11G> commit;

  32. 11:02:19 saup@PRI11G> select count(1) from test2 where id=1;

  33.   COUNT(1)
  34. ----------
  35.      70000
  36.      
  37. saup@PRI11G> exec dbms_stats.gather_table_stats('saup','test2',method_opt => 'for columns id size 254',cascade => true);

  38. --开启自动获取
  39. saup@PRI11G>alter system set OPTIMIZER_CAPTURE_PLAN_BASELINES=true;


  40. saup@PRI11G>alter system flush shared_pool;

  41. 1、这个只有一条记录,肯定走索引

  42. saup@PRI11G> var v_id number;

  43. saup@PRI11G> exec :v_id := 83061 ;

  44. --需要执行2次,在v$sql,查询EXECUTIONS为1次
  45. saup@PRI11G> select * from test2 where id=:v_id;


  46. SQL> SELECT sql_handle, plan_name,sql_text, enabled, accepted,FIXED,AUTOPURGE,ORIGIN FROM dba_sql_plan_baselines where sql_text like 'select * from test2%';

  47. SQL_HANDLE            PLAN_NAME                      SQL_TEXT                            ENABLED ACCEPTED FIXED AUTOPURGE ORIGIN
  48. --------------------- ------------------------------ ----------------------------------- ------- -------- ----- --------- --------------
  49. SQL_69c431b33cb76beb  SQL_PLAN_6mj1jqcybfuzbe2a98ff7 select * from test2 where id=:v_id  YES     YES      NO    YES       AUTO-CAPTURE


  50. SQL> select * from table(dbms_xplan.display_sql_plan_baseline ('SQL_69c431b33cb76beb','SQL_PLAN_6mj1jqcybfuzbe2a98ff7'));

  51. PLAN_TABLE_OUTPUT
  52. --------------------------------------------------------------------------------
  53. --------------------------------------------------------------------------------
  54. SQL handle: SQL_69c431b33cb76beb
  55. SQL text: select * from test2 where id=:v_id
  56. --------------------------------------------------------------------------------
  57. --------------------------------------------------------------------------------
  58. Plan name: SQL_PLAN_6mj1jqcybfuzbe2a98ff7         Plan id: 3802763255
  59. Enabled: YES     Fixed: NO      Accepted: YES     Origin: AUTO-CAPTURE
  60. --------------------------------------------------------------------------------
  61. Plan hash value: 1905508226
  62. --------------------------------------------------------------------------------
  63. | Id  | Operation                   | Name      | Rows  | Bytes | Cost (%CPU)| T
  64. --------------------------------------------------------------------------------
  65. |   0 | SELECT STATEMENT            |           |    23 |   621 |     2   (0)| 0
  66. |   1 |  TABLE ACCESS BY INDEX ROWID| TEST2     |    23 |   621 |     2   (0)| 0
  67. |*  2 |   INDEX RANGE SCAN          | TEST2_IDX |    23 |       |     1   (0)| 0
  68. --------------------------------------------------------------------------------

  69. PLAN_TABLE_OUTPUT
  70. --------------------------------------------------------------------------------
  71. Predicate Information (identified by operation id):
  72. ---------------------------------------------------
  73.    2 - access("ID"=TO_NUMBER(:V_ID))

  74. 25 rows selected




  75. saup@PRI11G>exec :v_id := 1;

  76. saup@PRI11G>set autotrace traceonly stat;

  77. --此sql也需要查询2次,否则在baseline不会有记录
  78. saup@PRI11G>select * from test2 where id=:v_id;


  79. --上面的一条,为新自动获取的,是走全表扫描的,但accepted为no,baseline不能使用
  80. SQL> SELECT sql_handle, plan_name,sql_text, enabled, accepted,FIXED,AUTOPURGE,ORIGIN FROM dba_sql_plan_baselines where sql_text like 'select * from test2%';

  81. SQL_HANDLE                     PLAN_NAME                      SQL_TEXT                            ENABLED ACCEPTED FIXED AUTOPURGE ORIGIN
  82. ------------------------------ ------------------------------ ----------------------------------- ------- -------- ----- --------- --------------
  83. SQL_69c431b33cb76beb           SQL_PLAN_6mj1jqcybfuzb99963deb select * from test2 where id=:v_id  YES     NO       NO    YES       AUTO-CAPTURE
  84. SQL_69c431b33cb76beb           SQL_PLAN_6mj1jqcybfuzbe2a98ff7 select * from test2 where id=:v_id  YES     YES      NO    YES       AUTO-CAPTURE


  85. 产生新的plan_name SQL_PLAN_6mj1jqcybfuzb99963deb ,这个的确是走全表扫描的

  86. SQL> select * from table(dbms_xplan.display_sql_plan_baseline ('SQL_69c431b33cb76beb','SQL_PLAN_6mj1jqcybfuzb99963deb'));

  87. PLAN_TABLE_OUTPUT
  88. --------------------------------------------------------------------------------
  89. --------------------------------------------------------------------------------
  90. SQL handle: SQL_69c431b33cb76beb
  91. SQL text: select * from test2 where id=:v_id
  92. --------------------------------------------------------------------------------
  93. --------------------------------------------------------------------------------
  94. Plan name: SQL_PLAN_6mj1jqcybfuzb99963deb         Plan id: 2576760299
  95. Enabled: YES     Fixed: NO      Accepted: NO      Origin: AUTO-CAPTURE
  96. --------------------------------------------------------------------------------
  97. Plan hash value: 300966803
  98. ---------------------------------------------------------------------------
  99. | Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
  100. ---------------------------------------------------------------------------
  101. |   0 | SELECT STATEMENT  |       |    23 |   621 |   101   (1)| 00:00:02 |
  102. |*  1 |  TABLE ACCESS FULL| TEST2 |    23 |   621 |   101   (1)| 00:00:02 |
  103. ---------------------------------------------------------------------------

  104. PLAN_TABLE_OUTPUT
  105. --------------------------------------------------------------------------------
  106. Predicate Information (identified by operation id):
  107. ---------------------------------------------------
  108.    1 - filter("ID"=TO_NUMBER(:V_ID))

  109. 24 rows selected



  110. 查看SQL,如果绑定变量窥视的话,同sql_id,应该还有一个不同的plan_has_valeue的,但是没有

  111. --第一次走索引时,每次执行的逻辑读为4

  112. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  113. ------------- ------------ --------------- ---------- ---------- -- -- -
  114. 55d5cn125hxb0            0      1905508226          1          4 Y  N  Y


  115. EXECUTIONS表示执行2次,为id=1,所以每次执行4942
  116. 在id=1执行第二次的时候,结果也如下一样,EXECUTIONS、BG_PER_EX都没有变

  117. SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE,EXECUTIONS,BUFFER_GETS/EXECUTIONS BG_PER_EX,IS_BIND_SENSITIVE BS,IS_BIND_AWARE BA,IS_SHAREABLE S from v$sql where sql_text like 'select * from test2%';

  118. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  119. ------------- ------------ --------------- ---------- ---------- -- -- -
  120. 55d5cn125hxb0            0      1905508226          2       4942 Y  N  Y

  121. 但我再执行三次id=1的SQL,就变成这样了,BS=no 没有绑定变量的窥视

  122. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  123. ------------- ------------ --------------- ---------- ---------- -- -- -
  124. 55d5cn125hxb0            0      1905508226          1       9884 N  N  Y



  125. 看结果没有使用 绑定变量窥视 ,IS_BIND_AWARE=N,表明Oracle还没有使用extended cursor sharing。

  126. IS_SHAREABLE=Y,表明这个cursor可以被再次使用,即能够共享


  127. --接下来尝试修改baseline属性,并演化,通过在网上查询,是可以直接修改accepted的,但现在不行,报错如下:

  128. ERROR at line 1:
  129. ORA-38136: invalid attribute name ACCEPTED specified
  130. ORA-06512: at "SYS.DBMS_SPM", line 2477
  131. ORA-06512: at line 4


  132. 我现在修改fixed为yes,但演化accepted为yes

  133. 13:33:45 sys@PRI11G> DECLARE
  134.   ret PLS_INTEGER;
  135. BEGIN
  136.   ret := dbms_spm.alter_sql_plan_baseline(sql_handle      => '&sql_handle',
  137.                                           plan_name       => '&plan_name',
  138.                                           attribute_name  => '&attribute',
  139.                                           attribute_value => '&value');
  140.   dbms_output.put_line(ret || ' SQL plan baseline(s) dropped');
  141. END;
  142. /
  143. Enter value for sql_handle: SQL_69c431b33cb76beb
  144. old   4:   ret := dbms_spm.alter_sql_plan_baseline(sql_handle           => '&sql_handle',
  145. new   4:   ret := dbms_spm.alter_sql_plan_baseline(sql_handle           => 'SQL_69c431b33cb76beb',
  146. Enter value for plan_name: SQL_PLAN_6mj1jqcybfuzb99963deb
  147. old   5:                                           plan_name           => '&plan_name',
  148. new   5:                                           plan_name           => 'SQL_PLAN_6mj1jqcybfuzb99963deb',
  149. Enter value for attribute: fixed
  150. old   6:                                           attribute_name  => '&attribute',
  151. new   6:                                           attribute_name  => 'fixed',
  152. Enter value for value: yes
  153. old   7:                                           attribute_value => '&value');
  154. new   7:                                           attribute_value => 'yes');
  155. 1 SQL plan baseline(s) dropped

  156. PL/SQL procedure successfully completed.


  157. --可看走full table scan的plan_name=SQL_PLAN_6mj1jqcybfuzb99963deb的fixed为yes,此时还不能走全表扫描,需要演化accepted为yes

  158. SQL> SELECT sql_handle, plan_name,sql_text, enabled, accepted,FIXED,AUTOPURGE,ORIGIN FROM dba_sql_plan_baselines where sql_text like 'select * from test2%';

  159. SQL_HANDLE             PLAN_NAME                      SQL_TEXT                            ENABLED ACCEPTED FIXED AUTOPURGE ORIGIN
  160. ---------------------  ------------------------------ ----------------------------------- ------- -------- ----- --------- --------------
  161. SQL_69c431b33cb76beb   SQL_PLAN_6mj1jqcybfuzb99963deb select * from test2 where id=:v_id  YES     NO       YES   YES       AUTO-CAPTURE
  162. SQL_69c431b33cb76beb   SQL_PLAN_6mj1jqcybfuzbe2a98ff7 select * from test2 where id=:v_id  YES     YES      NO    YES       AUTO-CAPTURE


  163. 执行2次后,EXECUTIONS,只增长1次
  164. SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE,EXECUTIONS,BUFFER_GETS/EXECUTIONS BG_PER_EX,IS_BIND_SENSITIVE BS,IS_BIND_AWARE BA,IS_SHAREABLE S from v$sql where sql_text like 'select * from test2%';

  165. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  166. ------------- ------------ --------------- ---------- ---------- -- -- -
  167. 55d5cn125hxb0            0      1905508226          2       9882 N  N  Y


  168. --演化:

  169. 13:41:31 saup@PRI11G> select dbms_spm.evolve_sql_plan_baseline(sql_handle=>'SQL_69c431b33cb76beb',plan_name=>'SQL_PLAN_6mj1jqcybfuzb99963deb',time_limit=>10,verify=>'yes',commit=>'yes') from dual;

  170. DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE(SQL_HANDLE=>'SQL_69C431B33CB76BEB',PLAN_NAME=>
  171. --------------------------------------------------------------------------------

  172. -------------------------------------------------------------------------------
  173.                         Evolve SQL Plan Baseline Report
  174. -------------------------------------------------------------------------------

  175. Inputs:
  176. -------
  177.   SQL_HANDLE = SQL_69c431b33cb76beb
  178.   PLAN_NAME  = SQL_PLAN_6mj1jqcybfuzb99963deb
  179.   TIME_LIMIT = 10
  180.   VERIFY     = yes
  181.   COMMIT     = yes

  182. Plan: SQL_PLAN_6mj1jqcybfuzb99963deb
  183. ------------------------------------
  184.   Plan was verified: Time used .22 seconds.
  185.   Plan passed performance criterion: 1.63 times better than baseline plan.
  186.   Plan was changed to an accepted plan.

  187.                             Baseline Plan      Test Plan       Stats Ratio
  188.                             -------------      ---------       -----------
  189.   Execution Status:                 COMPLETE        COMPLETE
  190.   Rows Processed:                    70000           70000
  191.   Elapsed Time(ms):                  14.15           3.231              4.38
  192.   CPU Time(ms):                    13.997           3.221              4.35
  193.   Buffer Gets:                              573             351              1.63
  194.   Physical Read Requests:                0               0
  195.   Physical Write Requests:        0               0
  196.   Physical Read Bytes:                        0               0
  197.   Physical Write Bytes:                 0               0
  198.   Executions:                                1               1

  199. -------------------------------------------------------------------------------
  200.                                  Report Summary
  201. -------------------------------------------------------------------------------
  202. Number of plans verified: 1
  203. Number of plans accepted: 1


  204. SQL> SELECT sql_handle, plan_name,sql_text, enabled, accepted,FIXED,AUTOPURGE,ORIGIN FROM dba_sql_plan_baselines where sql_text like 'select * from test2%';

  205. SQL_HANDLE            PLAN_NAME                      SQL_TEXT                            ENABLED ACCEPTED FIXED AUTOPURGE ORIGIN
  206. --------------------- ------------------------------ ----------------------------------- ------- -------- ----- --------- --------------
  207. SQL_69c431b33cb76beb  SQL_PLAN_6mj1jqcybfuzb99963deb select * from test2 where id=:v_id  YES     YES      YES   YES       AUTO-CAPTURE
  208. SQL_69c431b33cb76beb  SQL_PLAN_6mj1jqcybfuzbe2a98ff7 select * from test2 where id=:v_id  YES     YES      NO    YES       AUTO-CAPTURE

  209. --可以看到逻辑读立场少了很多,此是v$sql里查询不到关于test2的SQL,再执行一次可以看到,结果在下面
  210. 13:39:50 saup@PRI11G> select * from test2 where id=:v_id;

  211. 70000 rows selected.

  212. Statistics
  213. ----------------------------------------------------------
  214.          21  recursive calls
  215.          15  db block gets
  216. 5017  consistent gets
  217.           0  physical reads
  218.        3088  redo size
  219.     2359278  bytes sent via SQL*Net to client
  220.       51845  bytes received via SQL*Net from client
  221.        4668  SQL*Net roundtrips to/from client
  222.           0  sorts (memory)
  223.           0  sorts (disk)
  224.       70000  rows processed

  225. --第二次执行,Note 显示是使用oracle baseline了
  226. 13:44:03 saup@PRI11G> set autotrace traceonly
  227. 13:45:04 saup@PRI11G> select * from test2 where id=:v_id;

  228. 70000 rows selected.


  229. Execution Plan
  230. ----------------------------------------------------------
  231. Plan hash value: 300966803

  232. ---------------------------------------------------------------------------
  233. | Id  | Operation          | Name  | Rows  | Bytes | Cost (%CPU)| Time          |
  234. ---------------------------------------------------------------------------
  235. |   0 | SELECT STATEMENT  |          |    23 |   621 |   101   (1)| 00:00:02 |
  236. |*  1 |  TABLE ACCESS FULL| TEST2 |    23 |   621 |   101   (1)| 00:00:02 |
  237. ---------------------------------------------------------------------------

  238. Predicate Information (identified by operation id):
  239. ---------------------------------------------------

  240.    1 - filter("ID"=TO_NUMBER(:V_ID))

  241. Note
  242. -----
  243.    - SQL plan baseline "SQL_PLAN_6mj1jqcybfuzb99963deb" used for this statement


  244. Statistics
  245. ----------------------------------------------------------
  246.           1  recursive calls
  247.           0  db block gets
  248.        4999  consistent gets
  249.           0  physical reads
  250.           0  redo size
  251.     2359278  bytes sent via SQL*Net to client
  252.       51845  bytes received via SQL*Net from client
  253.        4668  SQL*Net roundtrips to/from client
  254.           0  sorts (memory)
  255.           0  sorts (disk)
  256.       70000  rows processed


  257. SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE,EXECUTIONS,BUFFER_GETS/EXECUTIONS BG_PER_EX,IS_BIND_SENSITIVE BS,IS_BIND_AWARE BA,IS_SHAREABLE S from v$sql where sql_text like 'select * from test2%';

  258. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  259. ------------- ------------ --------------- ---------- ---------- -- -- -
  260. 55d5cn125hxb0            0       300966803          1       4999 Y  N  Y


  261. --修改属性fixed为no,这样二个baseline的状态一致,accepted都为yes,ENABLED为yes,fixed为no
  262. --此时id=1,依然走full table scan,这时oracle会算cost
  263. 13:48:49 saup@PRI11G> DECLARE
  264.   ret PLS_INTEGER;
  265.                                           attribute_name  => '&attribute',
  266.                                           attribute_value => '&value');
  267.   dbms_output.put_line(ret || ' SQL plan baseline(s) dropped');
  268. END;
  269. BEGIN
  270.   ret := dbms_spm.alter_sql_plan_baseline(sql_handle      => '&sql_handle',
  271.                                           plan_name       => '&plan_name',
  272.                                           attribute_name  => '&attribute',
  273.                                           attribute_value => '&value');
  274.   dbms_output.put_line(ret || ' SQL plan baseline(s) dropped');
  275. END;
  276. 13:54:53  10  /
  277. Enter value for sql_handle: SQL_69c431b33cb76beb
  278. old   4:   ret := dbms_spm.alter_sql_plan_baseline(sql_handle           => '&sql_handle',
  279. new   4:   ret := dbms_spm.alter_sql_plan_baseline(sql_handle           => 'SQL_69c431b33cb76beb',
  280. Enter value for plan_name: SQL_PLAN_6mj1jqcybfuzb99963deb
  281. old   5:                                           plan_name           => '&plan_name',
  282. new   5:                                           plan_name           => 'SQL_PLAN_6mj1jqcybfuzb99963deb',
  283. Enter value for attribute: fixed
  284. old   6:                                           attribute_name  => '&attribute',
  285. new   6:                                           attribute_name  => 'fixed',
  286. Enter value for value: no
  287. old   7:                                           attribute_value => '&value');
  288. new   7:                                           attribute_value => 'no');
  289. 1 SQL plan baseline(s) dropped

  290. PL/SQL procedure successfully completed.


  291. SQL> SELECT sql_handle, plan_name,sql_text, enabled, accepted,FIXED,AUTOPURGE,ORIGIN FROM dba_sql_plan_baselines where sql_text like 'select * from test2%';

  292. SQL_HANDLE             PLAN_NAME                      SQL_TEXT                             ENABLED ACCEPTED FIXED AUTOPURGE ORIGIN
  293. ---------------------- ------------------------------ ------------------------------------ ------- -------- ----- --------- --------------
  294. SQL_69c431b33cb76beb   SQL_PLAN_6mj1jqcybfuzb99963deb select * from test2 where id=:v_id   YES     YES      NO    YES       AUTO-CAPTURE
  295. SQL_69c431b33cb76beb   SQL_PLAN_6mj1jqcybfuzbe2a98ff7 select * from test2 where id=:v_id   YES     YES      NO    YES       AUTO-CAPTURE


  296. --再执行一次,此处的执行计划是不准确的,因为autotrace不能正确显示各个child cursor的实际执行计划
  297. 13:56:06 saup@PRI11G> select * from test2 where id=:v_id;

  298. 70000 rows selected.


  299. Execution Plan
  300. ----------------------------------------------------------
  301. Plan hash value: 1905508226

  302. -----------------------------------------------------------------------------------------
  303. | Id  | Operation                    | Name        | Rows        | Bytes | Cost (%CPU)| Time        |
  304. -----------------------------------------------------------------------------------------
  305. |   0 | SELECT STATEMENT            |                |    23 |   621 |     2   (0)| 00:00:01 |
  306. |   1 |  TABLE ACCESS BY INDEX ROWID| TEST2        |    23 |   621 |     2   (0)| 00:00:01 |
  307. |*  2 |   INDEX RANGE SCAN            | TEST2_IDX |    23 |        |     1   (0)| 00:00:01 |
  308. -----------------------------------------------------------------------------------------

  309. Predicate Information (identified by operation id):
  310. ---------------------------------------------------

  311.    2 - access("ID"=TO_NUMBER(:V_ID))

  312. Note
  313. -----
  314.    - SQL plan baseline "SQL_PLAN_6mj1jqcybfuzbe2a98ff7" used for this statement


  315. Statistics
  316. ----------------------------------------------------------
  317.           1  recursive calls
  318.           0  db block gets
  319.        4999  consistent gets
  320.           0  physical reads
  321.           0  redo size
  322.     2359278  bytes sent via SQL*Net to client
  323.       51845  bytes received via SQL*Net from client
  324.        4668  SQL*Net roundtrips to/from client
  325.           0  sorts (memory)
  326.           0  sorts (disk)
  327.       70000  rows processed


  328. --这是修改fixed为no,执行一次的结果
  329. SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE,EXECUTIONS,BUFFER_GETS/EXECUTIONS BG_PER_EX,IS_BIND_SENSITIVE BS,IS_BIND_AWARE BA,IS_SHAREABLE S from v$sql where sql_text like 'select * from test2%';

  330. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  331. ------------- ------------ --------------- ---------- ---------- -- -- -
  332. 55d5cn125hxb0            0       300966803          1       4999 Y  N  Y

  333. --这是修改fixed为no,执行二次的结果
  334. --可知道走index时的plan_hash_value为1905508226,而plan_hash_value=300966803,正是走的全表扫描
  335. SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE,EXECUTIONS,BUFFER_GETS/EXECUTIONS BG_PER_EX,IS_BIND_SENSITIVE BS,IS_BIND_AWARE BA,IS_SHAREABLE S from v$sql where sql_text like 'select * from test2%';

  336. SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS  BG_PER_EX BS BA S
  337. ------------- ------------ --------------- ---------- ---------- -- -- -
  338. 55d5cn125hxb0            0       300966803          2       4999 Y  N  Y


  339. 2个问题其实是1个问题,很纠结,不知道怎么描述,因为11g绑定变量ACS,应该走全表扫描,更好的执行计划啊。


  340. 1、开启自动获取:为什么呢?怎么能够走全表扫描。只能通过手动修改fixed属性,再演化,可以使accepted为yes,才能使用吗?
  341. 如果这样baseline没办法开启自动获取啊

  342. 2、如果手动添加baseline:走hints的索引等等,以后数据发生倾斜,还会生成accepted为no的baseline,如果新生成的执行计划更好,那只能手动来操作吗?


  343. 这个链接为11g,哪个版本不清楚,自动获取baseline为true,也可以实现。
  344. http://www.jiagulun.com/thread-106-1-1.html


  345. 如果设置baseline自动获取为false,,则这个绑定变量窥视是是成功的,参考:
  346. http://space.itpub.net/15415488/viewspace-621535

  347. alter system set optimizer_capture_sql_plan_baselines=false;


  348. --写的有点多,非常感谢!
复制代码

回复 只看该作者 道具 举报

5#
发表于 2012-6-11 22:36:21
saup007 同学你的钻研精神 值得鼓励!

但是请注意 做实验之前 请熟读文档

Interacting with Adaptive Cursor Sharing 
SQL Plan Management (SPM):
– If OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES is set to
TRUE, only the first generated plan is used.
– As a workaround, set this parameter to FALSE, and run
your application until all plans are loaded in the cursor
cache.
– Manually load the cursor cache into the corresponding
plan baseline.

When using the SPM automatic plan capture, the first plan captured for a SQL statement
with bind variables is marked as the corresponding SQL plan baseline. If another plan is
found for that same SQL statement (which maybe the case with Adaptive Cursor Sharing), it
is added to the SQL statements plan history and marked for verification. It will not be used.
So even though Adaptive Cursor Sharing has come up with a new plan based on a new set of
bind values, SPM does not let it be used until the plan has been verified. Thus reverting back
to 10g behavior, only the plan generated based on the first set of bind values will be used by
all subsequent executions of the statement. One possible workaround is to run the system for
some time with automatic plan capture set to FALSE, and after the cursor cache has been
populated with all the plans a SQL statement with bind will have, load the entire plan
directly from the cursor cache into the corresponding SQL plan baseline. By doing this, all
the plans for a single SQL statement are marked as SQL baseline plans by default


简单来说

当 OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE 且 _optimizer_adaptive_cursor_sharing 默认打开时


对于绑定变量的 SQL语句 仅仅其第一个执行计划被被加入 SQL plan baseline ;  之后由 Adaptive Cursor Sharing产生的执行计划 会被加入到 SQL statements plan history 并标记为 待 验证 verification, 而不被使用 。  但是不通过OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES ,而通过load sql plan from cursor cache 则可以将一个SQL的所有执行计划都加入到SQL baseline plans中。


Oracle 官方推荐 在Adaptive Cursor Sharing启用的情况下, 不要设置OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TURE , 而使用 dbms_spm.load_plans_from_cursor_cache 来创建SQL baseline plan

回复 只看该作者 道具 举报

6#
发表于 2012-6-12 09:54:51

回复 5# 的帖子

thanks

能给下链接吗?或者哪篇文章。

回复 只看该作者 道具 举报

7#
发表于 2012-6-12 11:25:36

回复 6# 的帖子

Oracle Database 11g: Performance Student Guide http://t.askmaclean.com/thread-1196-1-1.html

回复 只看该作者 道具 举报

8#
发表于 2016-6-19 12:25:21
不错 不错 不错 不错

回复 只看该作者 道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|Archiver|Oracle数据库数据恢复、性能优化

GMT+8, 2024-12-21 02:21 , Processed in 0.053518 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部
TEL/電話+86 13764045638
Email service@parnassusdata.com
QQ 47079569