oracle實用sql(2)--segment advisor

selectshen發表於2016-06-03
segment advisor可以幫助分析表或索引的碎片,行連結問題.透過對問題表或索引收縮,移動或重建後減少儲存空間提高查詢速度.

點選(此處)摺疊或開啟

  1. --os:centos 6.6
  2. --db:oracle 11.2.0.4

  3. --建測試表
  4. create table scott.t_test01 as select * from dba_objects;
  5. --插入資料
  6. insert into scott.t_test01 select * from scott.t_test01;
  7. commit;
  8. insert into scott.t_test01 select * from scott.t_test01;
  9. commit;
  10. insert into scott.t_test01 select * from scott.t_test01;
  11. commit;
  12. --刪除一些資料
  13. delete from scott.t_test01 where rownum<300000;
  14. commit;

  15. --建立執行scott.T_TEST01的segment advisor task
  16. declare
  17.   v_id number;
  18.   v_task_name varchar2(100);
  19.   v_object_owner varchar2(50):='SCOTT';
  20.   v_object_name varchar2(50):='T_TEST01';
  21.   v_object_type varchar2(50):='TABLE';
  22. begin
  23.     dbms_advisor.create_task
  24.       (advisor_name => 'Segment Advisor'
  25.       ,task_id => v_id
  26.       ,task_name => v_task_name
  27.       ,task_desc => v_object_type||' '||v_object_owner||'.'||v_object_name
  28.       ,template => null
  29.       ,is_template => 'FALSE');
  30.     dbms_output.put_line('task_id:'||v_id);
  31.     dbms_advisor.create_object
  32.       (task_name => v_task_name
  33.       ,object_type => v_object_type
  34.       ,attr1 => v_object_owner
  35.       ,attr2 => v_object_name
  36.       ,attr3 => null
  37.       ,attr4 => null
  38.       ,attr5 => null
  39.       ,object_id => v_id);
  40.     dbms_advisor.set_task_parameter
  41.       (task_name => v_task_name
  42.       ,parameter => 'RECOMMEND_ALL'
  43.       ,value => 'TRUE');
  44.     dbms_advisor.set_task_parameter
  45.       (task_name => v_task_name
  46.       ,parameter => 'MODE'
  47.       ,value => 'COMPREHENSIVE');
  48.     dbms_advisor.set_task_parameter
  49.       (task_name => v_task_name
  50.       ,parameter => 'TIME_LIMIT'
  51.       ,value => 'UNLIMITED');
  52.     dbms_advisor.execute_task
  53.       (task_name => v_task_name);
  54. end;
  55. --輸入任務id
  56. /*task_id:21*/

  57. --根據上一步輸出的task_id檢視segment advisor任務資訊
  58. SELECT at.task_id, at.task_name, at.description, count(*) object_count, at.status, at.status_message,
  59.          at.pct_completion_time, at.created, at.last_modified,
  60.          at.execution_start, at.execution_end, at.owner, at.how_created
  61. FROM dba_advisor_tasks at, dba_advisor_objects ao
  62. WHERE at.task_id = ao.task_id
  63. AND at.task_id=21
  64. AND at.owner = ao.owner
  65. GROUP BY at.task_id, at.task_name, at.description, at.status, at.status_message,
  66.          at.pct_completion_time, at.created, at.last_modified,
  67.          at.execution_start, at.execution_end, at.owner, at.how_created;

  68. --根據task_id檢視segment advisor
  69. SELECT af.owner task_owner, af.task_name, ao.type Object_Type,
  70.        ao.attr1 object_owner, ao.attr2 object_name,
  71.        ao.attr3 subobject_name, to_char(ao.attr4) Tablespace_Name,
  72.        af.message, af.more_info, to_char(ao.task_id) task_id, to_char(ao.object_id) object_id
  73. FROM dba_advisor_findings af, dba_advisor_tasks at,
  74.        dba_advisor_objects ao
  75. WHERE af.task_id = at.task_id
  76. AND af.task_id=21
  77. AND af.owner = at.owner
  78. AND af.task_id = ao.task_id
  79. AND ao.object_id = af.object_id;

  80. --根據task_id檢視segment advisor recommendations
  81. SELECT aa.task_id, aa.object_id, aa.attr1, aa.attr2, aa.attr3,
  82.        initcap(ar.annotation_status) status, ar.rec_id
  83. FROM dba_advisor_actions aa, dba_advisor_tasks at,
  84.        dba_advisor_recommendations ar
  85. WHERE aa.task_id = at.task_id
  86. AND aa.task_id=21
  87. AND aa.owner = at.owner
  88. AND ar.task_id = at.task_id
  89. AND ar.rec_id = aa.rec_id;



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28539951/viewspace-2113728/,如需轉載,請註明出處,否則將追究法律責任。

相關文章