예전 프로젝트에서 간단한 ETL 유틸리티 패키지를 작성했다.
패키지 사용법은 아래와 같다. 적재 전에 prc_begin_etl 프로시저를 수행하고, 적재 후에 prc_end_etl 프로시저를 수행하면 된다. 병렬도는 세그먼트 사이즈에 따라 자동으로 설정된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
-- 1 fnc_get_partition_name (i_owner, i_table_name, i_partition_key_value); fnc_get_subpartition_name (i_owner, i_table_name, i_subpartition_key_value); -- 2 prc_truncate_table (i_owner, i_table_name); prc_truncate_partition (i_owner, i_table_name, i_partition_name); prc_truncate_subpartition (i_owner, i_table_name, i_subpartition_name); -- 3 prc_begin_etl (i_owner, i_table_name, i_partition_name); - prc_nologging_table (i_owner, i_table_name); - prc_disable_constraint (i_owner, i_table_name); - prc_unusable_index (i_owner, i_table_name, i_partition_name); -- 4 INSERT -- 5 prc_end_etl (i_owner, i_table_name, i_partition_name); - prc_rebuild_index (i_owner, i_table_name, i_partition_name); - prc_enable_constraint (i_owner, i_table_name); - prc_logging_table (i_owner, i_table_name); -- 6 prc_gather_table_stats (i_owner, i_table_name, i_partition_name); |
패키지 소스는 아래와 같다. 테스트 환경에서 검증 후 사용해야 한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
CREATE OR REPLACE PACKAGE pkg_utl_etl IS -------------------------------------------------- -- pkg_utl_etl -------------------------------------------------- FUNCTION fnc_get_partition_name ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_key_value IN VARCHAR2 ) RETURN VARCHAR2; FUNCTION fnc_get_subpartition_name ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_subpartition_key_value IN VARCHAR2 ) RETURN VARCHAR2; PROCEDURE prc_begin_etl ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ); PROCEDURE prc_end_etl ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ); PROCEDURE prc_nologging_table ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ); PROCEDURE prc_logging_table ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ); PROCEDURE prc_disable_constraint ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ); PROCEDURE prc_enable_constraint ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ); PROCEDURE prc_unusable_index ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ); PROCEDURE prc_rebuild_index ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ); PROCEDURE prc_truncate_table ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ); PROCEDURE prc_truncate_partition ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 ); PROCEDURE prc_truncate_subpartition ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_subpartition_name IN VARCHAR2 ); PROCEDURE prc_gather_table_stats ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL , i_granularity IN VARCHAR2 DEFAULT DBMS_STATS.DEFAULT_GRANULARITY ); END pkg_utl_etl; / CREATE OR REPLACE PACKAGE BODY pkg_utl_etl IS -------------------------------------------------- -- fnc_get_partition_name -------------------------------------------------- FUNCTION fnc_get_partition_name ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_key_value IN VARCHAR2 ) RETURN VARCHAR2 IS l_sql_text VARCHAR2(32767); l_partition_position dba_tab_partitions.partition_position%TYPE; l_partition_name dba_tab_partitions.partition_name%TYPE; BEGIN l_sql_text := 'SELECT TBL$OR$IDX$PART$NUM ({OWNER}.{TABLE_NAME}, 0, 1, 0, {PARTITION_KEY_VALUE}) FROM DUAL' ; l_sql_text := REPLACE (l_sql_text, '{OWNER}' , i_owner); l_sql_text := REPLACE (l_sql_text, '{TABLE_NAME}' , i_table_name); l_sql_text := REPLACE (l_sql_text, '{PARTITION_KEY_VALUE}' , i_partition_key_value); EXECUTE IMMEDIATE l_sql_text INTO l_partition_position; SELECT partition_name INTO l_partition_name FROM dba_tab_partitions WHERE table_owner = i_owner AND table_name = i_table_name AND partition_position = l_partition_position; RETURN l_partition_name; EXCEPTION WHEN OTHERS THEN RETURN 'NONE' ; END fnc_get_partition_name; -------------------------------------------------- -- fnc_get_subpartition_name -------------------------------------------------- FUNCTION fnc_get_subpartition_name ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_subpartition_key_value IN VARCHAR2 ) RETURN VARCHAR2 IS l_sql_text VARCHAR2(32767); l_object_id dba_objects.object_id%TYPE; l_subobject_name dba_objects.subobject_name%TYPE; BEGIN l_sql_text := 'SELECT TBL$OR$IDX$PART$NUM ({OWNER}.{TABLE_NAME}, 0, 3, 0, {SUBPARTITION_KEY_VALUE}) FROM DUAL' ; l_sql_text := REPLACE (l_sql_text, '{OWNER}' , i_owner); l_sql_text := REPLACE (l_sql_text, '{TABLE_NAME}' , i_table_name); l_sql_text := REPLACE (l_sql_text, '{SUBPARTITION_KEY_VALUE}' , i_subpartition_key_value); EXECUTE IMMEDIATE l_sql_text INTO l_object_id; SELECT subobject_name INTO l_subobject_name FROM dba_objects WHERE object_id = l_object_id; RETURN l_subobject_name; EXCEPTION WHEN OTHERS THEN RETURN 'NONE' ; END fnc_get_subpartition_name; -------------------------------------------------- -- fnc_get_degree -------------------------------------------------- FUNCTION fnc_get_degree ( i_owner IN VARCHAR2 , i_segment_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ) RETURN NUMBER IS l_bytes dba_segments.bytes%TYPE; BEGIN IF i_partition_name IS NULL THEN SELECT SUM (bytes) INTO l_bytes FROM dba_segments WHERE owner = i_owner AND segment_name = i_segment_name; ELSE SELECT bytes INTO l_bytes FROM dba_segments WHERE owner = i_owner AND segment_name = i_segment_name AND partition_name = i_partition_name; END IF; RETURN GREATEST (LEAST (CEIL (l_bytes / POWER (1024, 3)), 32), 4); -- ! EXCEPTION WHEN OTHERS THEN RETURN 2; END fnc_get_degree; -------------------------------------------------- -- prc_begin_etl -------------------------------------------------- PROCEDURE prc_begin_etl ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ) IS BEGIN prc_nologging_table (i_owner, i_table_name); prc_disable_constraint (i_owner, i_table_name); prc_unusable_index (i_owner, i_table_name, i_partition_name); END prc_begin_etl; -------------------------------------------------- -- prc_end_etl -------------------------------------------------- PROCEDURE prc_end_etl ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ) IS BEGIN prc_rebuild_index (i_owner, i_table_name, i_partition_name); prc_enable_constraint (i_owner, i_table_name); prc_logging_table (i_owner, i_table_name); END prc_end_etl; -------------------------------------------------- -- prc_nologging_table -------------------------------------------------- PROCEDURE prc_nologging_table ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'ALTER TABLE ' || i_owner || '.' || i_table_name || ' NOLOGGING' ; END prc_nologging_table; -------------------------------------------------- -- prc_logging_table -------------------------------------------------- PROCEDURE prc_logging_table ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'ALTER TABLE ' || i_owner || '.' || i_table_name || ' LOGGING' ; END prc_logging_table; -------------------------------------------------- -- prc_disable_constraint -------------------------------------------------- PROCEDURE prc_disable_constraint ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ) IS BEGIN FOR f IN ( SELECT constraint_name FROM dba_constraints WHERE owner = i_owner AND table_name = i_table_name AND constraint_type IN ( 'P' , 'U' ) AND status = 'ENABLED' ) LOOP EXECUTE IMMEDIATE 'ALTER TABLE ' || i_owner || '.' || i_table_name || ' MODIFY CONSTRAINT ' || f.constraint_name || ' DISABLE KEEP INDEX' ; END LOOP; END prc_disable_constraint; -------------------------------------------------- -- prc_enable_constraint -------------------------------------------------- PROCEDURE prc_enable_constraint ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ) IS BEGIN FOR f IN ( SELECT constraint_name FROM dba_constraints WHERE owner = i_owner AND table_name = i_table_name AND constraint_type IN ( 'P' , 'U' ) AND status = 'DISABLED' ) LOOP EXECUTE IMMEDIATE 'ALTER TABLE ' || i_owner || '.' || i_table_name || ' MODIFY CONSTRAINT ' || f.constraint_name || ' ENABLE' ; END LOOP; END prc_enable_constraint; -------------------------------------------------- -- prc_unusable_index -------------------------------------------------- PROCEDURE prc_unusable_index ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ) IS BEGIN FOR f IN ( SELECT owner , index_name FROM dba_indexes WHERE table_owner = i_owner AND table_name = i_table_name AND status = 'VALID' ) LOOP EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' UNUSABLE' ; END LOOP; FOR f IN ( SELECT a.owner , a.index_name , b.partition_name FROM dba_part_indexes a , dba_ind_partitions b WHERE a.owner = i_owner AND a.table_name = i_table_name AND a.subpartitioning_type = 'NONE' AND a.locality = 'LOCAL' AND b.index_owner = a.owner AND b.index_name = a.index_name AND b.partition_name = NVL (i_partition_name, partition_name) AND b.status = 'USABLE' ) LOOP EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' MODIFY PARTITION ' || f.partition_name || ' UNUSABLE' ; END LOOP; FOR f IN ( SELECT a.owner , a.index_name , b.subpartition_name FROM dba_part_indexes a , dba_ind_subpartitions b WHERE a.owner = i_owner AND a.table_name = i_table_name AND a.subpartitioning_type <> 'NONE' AND a.locality = 'LOCAL' AND b.index_owner = a.owner AND b.index_name = a.index_name AND b.subpartition_name = NVL (i_partition_name, subpartition_name) AND b.status = 'USABLE' ) LOOP EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' MODIFY SUBPARTITION ' || f.subpartition_name || ' UNUSABLE' ; END LOOP; END prc_unusable_index; -------------------------------------------------- -- prc_rebuild_index -------------------------------------------------- PROCEDURE prc_rebuild_index ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL ) IS l_degree NUMBER; BEGIN l_degree := fnc_get_degree (i_owner, i_table_name); FOR f IN ( SELECT owner , index_name FROM dba_indexes WHERE table_owner = i_owner AND table_name = i_table_name AND status = 'UNUSABLE' ) LOOP EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' REBUILD NOLOGGING PARALLEL ' || l_degree; EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' LOGGING NOPARALLEL' ; END LOOP; FOR f IN ( SELECT a.owner , a.index_name , b.partition_name FROM dba_part_indexes a , dba_ind_partitions b WHERE a.owner = i_owner AND a.table_name = i_table_name AND a.subpartitioning_type = 'NONE' AND a.locality = 'LOCAL' AND b.index_owner = a.owner AND b.index_name = a.index_name AND b.partition_name = NVL (i_partition_name, partition_name) AND b.status = 'UNUSABLE' ) LOOP l_degree := fnc_get_degree (i_owner, i_table_name, f.partition_name); EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' REBUILD PARTITION ' || f.partition_name || ' NOLOGGING PARALLEL ' || l_degree; EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' MODIFY PARTITION ' || f.partition_name || ' LOGGING' ; END LOOP; FOR f IN ( SELECT a.owner , a.index_name , b.subpartition_name FROM dba_part_indexes a , dba_ind_subpartitions b WHERE a.owner = i_owner AND a.table_name = i_table_name AND a.subpartitioning_type <> 'NONE' AND a.locality = 'LOCAL' AND b.index_owner = a.owner AND b.index_name = a.index_name AND b.subpartition_name = NVL (i_partition_name, subpartition_name) AND b.status = 'UNUSABLE' ) LOOP l_degree := fnc_get_degree (i_owner, i_table_name, f.subpartition_name); EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' REBUILD SUBPARTITION ' || f.subpartition_name || ' NOLOGGING PARALLEL ' || l_degree; EXECUTE IMMEDIATE 'ALTER INDEX ' || f.owner || '.' || f.index_name || ' MODIFY SUBPARTITION ' || f.subpartition_name || ' LOGGING' ; END LOOP; END prc_rebuild_index; -------------------------------------------------- -- prc_truncate_table -------------------------------------------------- PROCEDURE prc_truncate_table ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || i_owner || '.' || i_table_name || ' DROP ALL STORAGE' ; END prc_truncate_table; -------------------------------------------------- -- prc_truncate_partition -------------------------------------------------- PROCEDURE prc_truncate_partition ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'ALTER TABLE ' || i_owner || '.' || i_table_name || ' TRUNCATE PARTITION ' || i_partition_name || ' DROP ALL STORAGE' ; END prc_truncate_partition; -------------------------------------------------- -- prc_truncate_subpartition -------------------------------------------------- PROCEDURE prc_truncate_subpartition ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_subpartition_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'ALTER TABLE ' || i_owner || '.' || i_table_name || ' TRUNCATE SUBPARTITION ' || i_subpartition_name || ' DROP ALL STORAGE' ; END prc_truncate_subpartition; -------------------------------------------------- -- prc_gather_table_stats -------------------------------------------------- PROCEDURE prc_gather_table_stats ( i_owner IN VARCHAR2 , i_table_name IN VARCHAR2 , i_partition_name IN VARCHAR2 DEFAULT NULL , i_granularity IN VARCHAR2 DEFAULT DBMS_STATS.DEFAULT_GRANULARITY ) IS l_degree NUMBER; BEGIN l_degree := fnc_get_degree (i_owner, i_table_name, i_partition_name); CASE WHEN i_partition_name IS NULL THEN DBMS_STATS.GATHER_TABLE_STATS (ownname => i_owner, tabname => i_table_name, degree => l_degree); ELSE DBMS_STATS.GATHER_TABLE_STATS (ownname => i_owner, tabname => i_table_name, partname => i_partition_name, degree => l_degree, granularity => i_granularity); END CASE ; END prc_gather_table_stats; END pkg_utl_etl; / |
'Oracle > PL/SQL' 카테고리의 다른 글
다형성 테이블 함수 (0) | 2019.01.04 |
---|---|
단위 변환 함수 (0) | 2018.12.31 |
에러 메시지 조회 함수 (0) | 2018.12.24 |
런타임 실행 계획 조회 함수 (0) | 2018.12.12 |
RAW 변환 함수 (0) | 2018.11.13 |