tech_memo / ORACLE / statistics


tech_memo/ORACLE

統計情報のロック解除

  • ロックされているテーブルの一覧
    select table_name, stattype_locked from dba_tab_statistics where owner = 'HOGE' and stattype_locked is not null;
  • ロックの解除
    begin
        dbms_stats.unlock_table_stats('HOGE', 'HOGE_TABLE');
    end;
    /

統計情報の最終取得時刻確認

select TABLE_NAME,num_rows,last_analyzed from DBA_TABLES where owner='[ユーザー]' order by last_analyzed desc;

パーティションの場合

  • 参考 : http://d.hatena.ne.jp/yohei-a/20100125/1264409523
  • パーティション表
    select table_owner, table_name, partition_name, to_char(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') "LAST_ANALYZED" 
    	from dba_tab_partitions 
    		where table_owner in ('SCOTT')
    	order by table_owner, table_name, partition_name;
  • パーティションインデックス
    select index_owner, index_name, partition_name, to_char(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') "LAST_ANALYZED" 
    	from dba_ind_partitions 
    		where index_owner in ('SCOTT')
    	order by index_owner, index_name, partition_name;

サブパーティションの場合

  • サブパーティション表
    select table_owner, table_name, partition_name, subpartition_name, to_char(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') "LAST_ANALYZED" 
    	from dba_tab_subpartitions 
    		where table_owner in ('SCOTT')
    	order by table_owner, table_name, partition_name, subpartition_name;
  • サブパーティションインデックス
    select index_owner, index_name, partition_name, subpartition_name, to_char(last_analyzed, 'YYYY-MM-DD HH24:MI:SS') "LAST_ANALYZED" 
    	from dba_ind_subpartitions 
    		where index_owner in ('SCOTT')
    	order by index_owner, index_name, partition_name, subpartition_name;

指定したテーブルの統計情報の更新

  • http://itnavi.com/totteORA/Tips21.asp
    • ↑ analyze文は古い
  • DBMS_STATSプロシージャを利用。
    BEGIN
        DBMS_STATS.GATHER_TABLE_STATS('USERNAME', 'TABLE_NAME', granularity=>'ALL');
    END;
    /
    • なお、テーブルの統計情報を取得すると、同時にINDEXの統計情報も取得される。

サブパーティションの統計情報も取得する

自動メンテナンスタスクの状態確認

  • optimizer_stats列がENABLEだと有効
    set linesize 200
    col window_name for a20
    col window_next_time for a40
    SELECT window_name, window_next_time, window_active, autotask_status, optimizer_stats, segment_advisor, sql_tune_advisor
    	FROM dba_autotask_window_clients;

統計情報の取得 ON/OFF

  • 取得無効化
    BEGIN
     dbms_auto_task_admin.disable(client_name => 'auto optimizer stats collection',operation => NULL,window_name => NULL);
    END;
    /
  • 取得有効化
    BEGIN
     dbms_auto_task_admin.enable(client_name => 'auto optimizer stats collection',operation => NULL,window_name => NULL);
    END;
    /

統計情報のコントロール

  • 11g
    • http://d.hatena.ne.jp/yohei-a/20120105/1325747902
    • 確認
      set linesize 200
      col window_name for a20
      col window_next_time for a40
      SELECT window_name, window_next_time, window_active, autotask_status, optimizer_stats, segment_advisor, sql_tune_advisor
         FROM dba_autotask_window_clients;
  • 取得停止
    BEGIN
       dbms_auto_task_admin.disable(client_name => 'auto optimizer stats collection',operation => NULL,window_name => NULL);
    END;
    /