m4ly

DBMS_REDEFINITION_PARTITION

Apr 23rd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 6.46 KB | None | 0 0
  1. -- Dawid Mocek
  2. -- Reorganize single partition using new tablespace with compression
  3. -- Req: options_flag => DBMS_REDEFINITION.cons_use_pk
  4.  
  5. -- DB 11.2.0.3 has an error in dbms_redefinition package - it does not clear properly temporary data in dictionary/views,
  6. -- so trying to redefine next object will throw an exception
  7.  
  8. -- --
  9. -- AS SYS
  10. -- chg @part_name
  11. BEGIN
  12.   DBMS_REDEFINITION.CAN_REDEF_TABLE( uname => 'xxxx_xxx', tname => 'LOG_EVENT', options_flag => DBMS_REDEFINITION.cons_use_pk, part_name => 'P_12_2015');
  13. END;
  14. /
  15.  
  16. -- AS SYS
  17. -- chg @TBSPC_xxxx_xxx_INT_PAR_TMP filename
  18. -- CREATE TABLESPACE "TBSPC_xxxx_xxx_INT_TMP" DATAFILE '+DATA/DB/DATAFILE/xxxx_xxx_log/int_tmp.dbf' SIZE 4G REUSE AUTOEXTEND OFF LOGGING ONLINE PERMANENT BLOCKSIZE 8192 EXTENT MANAGEMENT  LOCAL AUTOALLOCATE  SEGMENT SPACE MANAGEMENT AUTO  DEFAULT COMPRESS ;
  19. CREATE TABLESPACE "TBSPC_xxxx_xxx_INT_PAR_TMP" DATAFILE '+DATA/DB/DATAFILE/xxxx_xxx_log/event122015c.dbf' SIZE 4G REUSE AUTOEXTEND OFF LOGGING ONLINE PERMANENT BLOCKSIZE 8192 EXTENT MANAGEMENT  LOCAL AUTOALLOCATE  SEGMENT SPACE MANAGEMENT AUTO DEFAULT COMPRESS ;
  20. -- CREATE TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" DATAFILE '+DATA/DB/DATAFILE/xxxx_xxx_log/idx_log_event.dbf' SIZE 4G REUSE AUTOEXTEND OFF LOGGING ONLINE PERMANENT BLOCKSIZE 8192 EXTENT MANAGEMENT  LOCAL AUTOALLOCATE  SEGMENT SPACE MANAGEMENT AUTO DEFAULT COMPRESS ;
  21.    
  22. -- AS SYS
  23. -- ALTER USER yyyy quota unlimited ON TBSPC_xxxx_xxx_INT_IDX_TMP;
  24. ALTER USER yyyy quota unlimited ON TBSPC_xxxx_xxx_INT_PAR_TMP;
  25. -- ALTER USER yyyy quota unlimited ON TBSPC_xxxx_xxx_INT_TMP;
  26.  
  27. -- ALTER USER zzzz quota unlimited ON TBSPC_xxxx_xxx_INT_IDX_TMP;
  28. ALTER USER zzzz quota unlimited ON TBSPC_xxxx_xxx_INT_PAR_TMP;
  29. -- ALTER USER zzzz quota unlimited ON TBSPC_xxxx_xxx_INT_TMP;
  30.  
  31. -- ALTER USER wwww quota unlimited ON TBSPC_xxxx_xxx_INT_IDX_TMP;
  32. ALTER USER wwww quota unlimited ON TBSPC_xxxx_xxx_INT_PAR_TMP;
  33. -- ALTER USER wwww quota unlimited ON TBSPC_xxxx_xxx_INT_TMP;
  34.  
  35.  
  36. -- AS USER
  37. CREATE TABLE "xxxx_xxx"."INT_LOG_EVENT"
  38.   (
  39.    [...]
  40.   )
  41.   TABLESPACE TBSPC_xxxx_xxx_INT_PAR_TMP;
  42.  
  43. -- AS SYS
  44. -- Start the redefinition process using /* rowid */ cons_use_pk
  45. -- @part_name
  46. BEGIN
  47.   DBMS_REDEFINITION.START_REDEF_TABLE( uname => 'xxxx_xxx', orig_table => 'oooo_oooo', int_table => 'INT_LOG_EVENT', col_mapping => NULL, options_flag => DBMS_REDEFINITION.cons_use_pk, part_name => 'P_12_2015');
  48. END;
  49. /
  50.  
  51.  
  52. -- AS USER
  53. -- Manually create any local indexes on the interim table
  54. CREATE INDEX "xxxx_xxx"."INT_LOG_EVENT_EVENT_CODE" ON "xxxx_xxx"."INT_LOG_EVENT"
  55.   (
  56.     "EVENT_CODE"
  57.   )
  58.   PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" ;
  59.  
  60. CREATE INDEX "xxxx_xxx"."INT_LOG_EVENT_EVENT_NAME" ON "xxxx_xxx"."INT_LOG_EVENT"
  61.   (
  62.     "EVENT_NAME"
  63.   )
  64.   PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" ;
  65.  
  66. CREATE INDEX "xxxx_xxx"."INT_LOG_EVENT_EVENT_TIME" ON "xxxx_xxx"."INT_LOG_EVENT"
  67.   (
  68.     "EVENT_TIME"
  69.   )
  70.   PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" ;
  71.  
  72. CREATE INDEX "xxxx_xxx"."INT_LOG_EVENT_E_SERVICE_CODE" ON "xxxx_xxx"."INT_LOG_EVENT"
  73.   (
  74.     "E_SERVICE_CODE"
  75.   )
  76.   PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" ;
  77.  
  78. CREATE INDEX "xxxx_xxx"."INT_LOG_EVENT_L_TIME" ON "xxxx_xxx"."INT_LOG_EVENT"
  79.   (
  80.     "L_TIME"
  81.   )
  82.   PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" ;
  83.  
  84. CREATE UNIQUE INDEX "xxxx_xxx"."INT_LOG_EVENT_PK" ON "xxxx_xxx"."INT_LOG_EVENT"
  85.   (
  86.     "L_ID"
  87.   )
  88.   PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "TBSPC_xxxx_xxx_INT_IDX_TMP" ;
  89.  
  90. -- AS SYS
  91. -- Optionally synchronize the interim table.
  92. -- @part_name
  93. BEGIN
  94.   DBMS_REDEFINITION.SYNC_INTERIM_TABLE( uname => 'xxxx_xxx', orig_table => 'LOG_EVENT', int_table => 'INT_LOG_EVENT', part_name => 'P_12_2015');
  95. END;
  96. /
  97.  
  98. -- AS SYS
  99. -- Complete the redefinition.
  100. -- @param part_name
  101. BEGIN
  102.   DBMS_REDEFINITION.FINISH_REDEF_TABLE( uname => 'xxxx_xxx', orig_table => 'LOG_EVENT', int_table => 'INT_LOG_EVENT', part_name => 'P_12_2015');
  103. END;
  104. /
  105.  
  106. -- AS SYS
  107. -- abort
  108. -- @param part_name
  109. BEGIN
  110.   DBMS_REDEFINITION.ABORT_REDEF_TABLE ( uname => 'xxxx_xxx', orig_table => 'LOG_EVENT', int_table => 'INT_LOG_EVENT', part_name => 'P_12_2015');
  111. END;
  112. /
  113.  
  114. -- AS SYS
  115. -- !!! Be sure that object has been redefined !!!
  116. SELECT partition_name,
  117.   tablespace_name
  118. FROM dba_tab_partitions
  119. WHERE table_name = 'LOG_EVENT';
  120.  
  121. -- AS SYS/USER
  122. -- optional rebuild indexes
  123. SELECT 'ALTER INDEX '
  124.   ||INDEX_NAME
  125.   ||' REBUILD'
  126. FROM user_indexes
  127. WHERE table_name='LOG_EVENT';
  128.  
  129.  
  130. -- AS SYS
  131. -- chg !!! tablespace name !!!
  132. -- DROP TABLESPACE TBSPC_xxxx_xxx_LOG_EVENT122015 INCLUDING CONTENTS AND DATAFILES;
  133.  
  134. -- AS SYS
  135. -- chg  !!! names !!!
  136. -- ALTER TABLESPACE TBSPC_xxxx_xxx_INT_PAR_TMP RENAME TO TBSPC_xxxx_xxx_LOG_EVENT122015;
  137.  
  138. -- AS SYS
  139. -- chg datafile name and size
  140. -- ALTER DATABASE DATAFILE '+DATA/db/datafile/xxxx_xxx/event122015c.dbf' RESIZE 137M;
  141.  
  142. -- AS SYS
  143. -- chg partiton name
  144. -- select count(l_id) from xxxx_xxx.log_event partition(P_12_2015);
  145.  
  146. -- AS SYS
  147. -- chg tablespace name
  148. --  ALTER TABLESPACE TBSPC_xxxx_xxx_LOG_EVENT122015 READ ONLY ;
  149.  
  150.  
  151. -- AS SYS
  152. -- sprawdz status wnętrzności bazy
  153.  
  154. -- select * from v$db_object_cache ;
  155. -- select OWNER,OBJECT_NAME,OBJECT_TYPE,STATUS from dba_objects where OWNER='xxxx_xxx';
  156. -- select * from sys.snap_loadertime$;
  157. -- select * from all_objects where object_id = ??;
  158. -- select * from all_objects ao, sys.snap_loadertime$ sl where ao.object_id = sl.TABLEOBJ#;
  159. -- select * from sys.sumpartlog$;
  160.  
  161. -- AS USER
  162. -- bypass ora-error
  163.  CREATE materialized VIEW mdummy TABLESPACE TBSPC_TEST NOLOGGING refresh complete
  164. AS
  165. SELECT * FROM xxxx_xxx.log_event PARTITION(P_12_2015);
  166.  
  167.  EXEC DBMS_MVIEW.refresh('mdummy', 'cf');
  168.  DROP MATERIALIZED VIEW mdummy;
  169.  
  170.  SELECT v.SQL_TEXT,
  171.            v.PARSING_SCHEMA_NAME,
  172.            v.FIRST_LOAD_TIME,
  173.            v.DISK_READS,
  174.            v.ROWS_PROCESSED,
  175.            v.ELAPSED_TIME,
  176.            v.service
  177.       FROM gv$sql v
  178. WHERE TO_DATE(v.FIRST_LOAD_TIME,'YYYY-MM-DD hh24:mi:ss')>ADD_MONTHS(TRUNC(SYSDATE,'MM'),-2) AND v.sql_text LIKE '%V_LOG_ALL%';
  179.  
  180.  
  181. SELECT * FROM xxxx_xxx.log_event PARTITION(P_12_2015);
  182.  
  183.  
  184. ALTER database datafile '+DATA/db/datafile/xxxx_xxx_log/event012016.dbf' resize 8G;
  185.  
  186.  
  187. SELECT * FROM xxxx_xxx.log_event PARTITION(p_01_2016) WHERE ROWNUM < 500 ORDER BY l_time DESC;
Advertisement
Add Comment
Please, Sign In to add comment