GATHER_TABLE_STATS_PRD.sql 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. --/
  2. CREATE PROCEDURE GATHER_TABLE_STATS_PRD
  3. (in_table IN VARCHAR2, in_partitions IN VARCHAR2)
  4. -- create by dufs 2019-01-10 用于表的统计信息收集
  5. AS
  6. v_is_part varchar2(32);
  7. v_is_lock varchar2(32);
  8. v_table varchar2(32);
  9. v_partitions varchar2(4000);
  10. BEGIN
  11. v_table:=upper(in_table);
  12. v_partitions:=upper(in_partitions);
  13. select PARTITIONED into v_is_part from user_tables where table_name=v_table;
  14. if v_is_part is null
  15. then
  16. dbms_output.put_line('not found table' || v_table);
  17. return;
  18. elsif v_is_part='NO'
  19. then
  20. dbms_output.put_line('table' || v_table || ' is not PARTITIONED');
  21. select stattype_locked into v_is_lock from user_tab_statistics where table_name=v_table;
  22. if v_is_lock is not null
  23. then
  24. dbms_output.put_line('table' || v_table || ' is locked :'||v_is_lock);
  25. --表处理锁定状态,需要先解锁
  26. DBMS_STATS.UNLOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  27. else
  28. dbms_output.put_line('table' || v_table || ' is not locked :'||v_is_lock);
  29. end if;
  30. DBMS_STATS.GATHER_TABLE_STATS(OWNNAME => user,
  31. TABNAME => v_table,
  32. estimate_percent => 10,
  33. DEGREE => 3);
  34. if v_is_lock is not null
  35. then
  36. --锁定表的统计信息,避免自动任务分析表
  37. DBMS_STATS.LOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  38. end if;
  39. elsif v_is_part='YES'
  40. then
  41. dbms_output.put_line('table' || v_table || ' is PARTITIONED');
  42. if v_partitions is not null
  43. then
  44. select max(stattype_locked) into v_is_lock from user_tab_statistics where table_name=v_table and stattype_locked is not null;
  45. if v_is_lock is not null
  46. then
  47. dbms_output.put_line('table' || v_table || ' is locked :'||v_is_lock);
  48. --表处理锁定状态,需要先解锁
  49. DBMS_STATS.UNLOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  50. else
  51. dbms_output.put_line('table' || v_table || ' is not locked :'||v_is_lock);
  52. end if;
  53. for cur in ( select partition_name from user_tab_partitions where table_name=v_table and partition_name in (select COLUMN_VALUE from table(str2varlist_new(v_partitions)))
  54. )loop
  55. DBMS_STATS.GATHER_TABLE_STATS(OWNNAME => user,
  56. TABNAME => v_table,
  57. PARTNAME => cur.partition_name,
  58. GRANULARITY => 'PARTITION',
  59. DEGREE => 3,
  60. CASCADE => TRUE);
  61. end loop;
  62. if v_is_lock is not null
  63. then
  64. --锁定表的统计信息,避免自动任务分析表
  65. DBMS_STATS.LOCK_TABLE_STATS(OWNNAME => user, TABNAME => v_table);
  66. end if;
  67. else
  68. dbms_output.put_line('table' || v_table || ' paritions gived is null');
  69. end if;
  70. end if;
  71. END;
  72. /