GATHER_INDEX_STATS_PRD.sql 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. --/
  2. CREATE PROCEDURE GATHER_INDEX_STATS_PRD
  3. (in_table varchar2, in_indexes IN VARCHAR2)
  4. -- create by dufs 2019-01-10 用于索引的统计信息收集
  5. AS
  6. v_cnt number;
  7. v_is_part varchar2(32);
  8. v_is_lock varchar2(32);
  9. v_index varchar2(32);
  10. v_table varchar2(32);
  11. v_indexes varchar2(4000);
  12. BEGIN
  13. v_table:=upper(in_table);
  14. v_indexes:=upper(in_indexes);
  15. select count(*) into v_cnt from user_tables where table_name=v_table;
  16. if v_cnt = 0
  17. then
  18. dbms_output.put_line('not found table' || v_table);
  19. return;
  20. end if;
  21. if v_indexes is null
  22. then
  23. -- 如果传入参数中,索引为空,则取该表所有索引进行统计信息收集
  24. dbms_output.put_line('v_indexes is null , get all indexes on table ||' || v_table);
  25. select listagg(index_name,',') within group(order by index_name) into v_indexes from user_indexes where table_name=v_table;
  26. end if;
  27. for cur in (select COLUMN_VALUE as index_name from table(str2varlist_new(v_indexes))
  28. )
  29. loop
  30. v_index:=cur.index_name;
  31. select PARTITIONED into v_is_part from user_indexes where index_name=v_index and table_name=v_table;
  32. if v_is_part is null
  33. then
  34. dbms_output.put_line('not found v_index' || v_index);
  35. continue;
  36. elsif v_is_part='NO'
  37. then
  38. dbms_output.put_line('v_index ' || v_index || ' is not PARTITIONED');
  39. select max(stattype_locked) into v_is_lock from user_tab_statistics where table_name=v_table and stattype_locked is not null;
  40. if v_is_lock is not null
  41. then
  42. dbms_output.put_line('table' || v_table || ' is locked :'||v_is_lock);
  43. --表处理锁定状态,需要先解锁
  44. DBMS_STATS.UNLOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  45. else
  46. dbms_output.put_line('table' || v_table || ' is not locked :'||v_is_lock);
  47. end if;
  48. DBMS_STATS.GATHER_index_STATS(OWNNAME => user,
  49. indname => v_index,
  50. estimate_percent => 10,
  51. DEGREE => 3);
  52. if v_is_lock is not null
  53. then
  54. --锁定表的统计信息,避免自动任务分析表
  55. DBMS_STATS.LOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  56. end if;
  57. elsif v_is_part='YES'
  58. then
  59. dbms_output.put_line('index' || v_index || ' is PARTITIONED');
  60. select max(stattype_locked) into v_is_lock from user_tab_statistics where table_name=v_table and stattype_locked is not null;
  61. if v_is_lock is not null
  62. then
  63. dbms_output.put_line('table' || v_table || ' is locked :'||v_is_lock);
  64. --表处理锁定状态,需要先解锁
  65. DBMS_STATS.UNLOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  66. else
  67. dbms_output.put_line('table' || v_table || ' is not locked :'||v_is_lock);
  68. end if;
  69. for cur_1 in (select partition_name from user_ind_partitions where index_name=v_index
  70. )
  71. loop
  72. DBMS_STATS.GATHER_index_STATS(OWNNAME => user,
  73. indname => v_index,
  74. PARTNAME => cur_1.partition_name,
  75. GRANULARITY => 'PARTITION',
  76. estimate_percent => 10,
  77. DEGREE => 3);
  78. end loop;
  79. if v_is_lock is not null
  80. then
  81. --锁定表的统计信息,避免自动任务分析表
  82. DBMS_STATS.LOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  83. end if;
  84. else
  85. dbms_output.put_line('table' || v_table || ' paritions gived is null');
  86. end if;
  87. end loop;
  88. END;
  89. /