Oracle 鎖定暫存資料表統計資訊及鎖住後是否能用動態採集的hint,oraclehint

來源:互聯網
上載者:User

Oracle 鎖定暫存資料表統計資訊及鎖住後是否能用動態採集的hint,oraclehint

  全域暫存資料表的統計資訊是不能被收集的,如果被收集,它的統計資訊肯定為0,會造成執行計畫不準,所以要鎖定它的統計資訊,禁止系統自動收集。

--先解鎖表的統計資訊,然後刪除表的統計資訊,最後鎖住表的統計資訊

declare

  v_sqlvarchar2(500);

  cursor rowList1 is

   select'begin dbms_stats.unlock_table_stats(user,''' || table_name ||'''); end;'

     from user_tables s

   where s.temporary = 'Y'; 

  cursor rowList2 is

   select'begin dbms_stats.delete_table_stats(user,''' || table_name ||'''); end;'

     from user_tables s

   where s.temporary = 'Y';

  cursor rowList3 is

   select'begin dbms_stats.lock_table_stats(user,''' || table_name ||'''); end;'

     from user_tables s

   where s.temporary = 'Y';  

begin

  open rowList1;

  open rowList2;

  open rowList3;

  loop

    fetch rowList1  into v_sql;

       executeimmediate v_sql;

    exitwhen rowList1%notfound;

  endloop;

  loop

    fetch rowList2  into v_sql;

       executeimmediate v_sql;

    exitwhen rowList2%notfound;

  endloop;

  loop

    fetch rowList3  into v_sql;

       executeimmediate v_sql;

    exitwhen rowList3%notfound;

  endloop;   

  close rowList1;

  close rowList2;

  close rowList3;

end;

-- STATTYPE_LOCKED='ALL'意思是表的統計資訊被鎖

select s.table_name,s.STATTYPE_LOCKED  from user_TAB_STATISTICS s  where s.STATTYPE_LOCKED='ALL';

--當表的統計資訊被鎖後,用動態採集的hint是否生效,實驗結果是hint是生效的

SQL> drop table test purge;
SQL> create table test as select * from dba_objects;
SQL> exec dbms_stats.lock_table_stats(user,'test');
SQL> select s.num_rows, s.last_analyzed
      from user_tables s
     where s.table_name = 'TEST';
  NUM_ROWS LAST_ANALYZED
---------- --------------

--說明表被鎖了
SQL> select s.STATTYPE_LOCKED from user_TAB_STATISTICS s
     where s.table_name='TEST';
STATTYPE_L
----------
ALL
SQL> select count(*) from test;
  COUNT(*)
----------
     70384
SQL> set autotrace traceonly
SQL> select /*+ dynamic_sampling(test 1) */ * from test;
已選擇70384行。
執行計畫
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   160K|    31M|   199   (3)| 00:00:03 |
|   1 |  TABLE ACCESS FULL| TEST |   160K|    31M|   199   (3)| 00:00:03 |
--------------------------------------------------------------------------
Note
-----
   - dynamic sampling used for this statement (level=2)
統計資訊
----------------------------------------------------------
          4  recursive calls
          0  db block gets
       5239  consistent gets
          0  physical reads
          0  redo size
    3186713  bytes sent via SQL*Net to client
      51949  bytes received via SQL*Net from client
       4694  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      70384  rows processed


SQL> select /*+ dynamic_sampling(test 5) */ * from test;
已選擇70384行。
執行計畫
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  9747 |  1970K|   195   (0)| 00:00:03 |
|   1 |  TABLE ACCESS FULL| TEST |  9747 |  1970K|   195   (0)| 00:00:03 |
--------------------------------------------------------------------------
Note
-----
   - dynamic sampling used for this statement (level=2)
統計資訊
----------------------------------------------------------
          4  recursive calls
          0  db block gets
       5239  consistent gets
          0  physical reads
          0  redo size
    3186713  bytes sent via SQL*Net to client
      51949  bytes received via SQL*Net from client
       4694  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      70384  rows processed


SQL> select /*+ dynamic_sampling(test 10) */ * from test;
已選擇70384行。
執行計畫
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 70384 |    13M|   197   (2)| 00:00:03 |
|   1 |  TABLE ACCESS FULL| TEST | 70384 |    13M|   197   (2)| 00:00:03 |
--------------------------------------------------------------------------
Note
-----
   - dynamic sampling used for this statement (level=2)
統計資訊
----------------------------------------------------------
          0  recursive calls
          0  db block gets
       5165  consistent gets
          0  physical reads
          0  redo size
    3186713  bytes sent via SQL*Net to client
      51949  bytes received via SQL*Net from client
       4694  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      70384  rows processed

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.