Partitioneren met EXCHANGE PARTITION

Dit document beschrijft een simpele methode om een bestaande tabel te partitioneren met de EXCHANGE PARTITION syntax. De inhoud geeft niet aan wanneer en hoe objecten te partitioneren maar alleen een methode om van A naar B te komen. Wat gaan we behandelen:

Het maken van een voorbeeld schema.
Het maken van een gepartitioneerde Destination tabel.
EXCHANGE PARTITION
SPLIT PARTITION

 

Het maken van een voorbeeld schema


Eerst maken we een voorbeeld schema dat als startpunt zal dienen.

Creëer en vul een kleine "lookup" tabel.

CREATE TABLE lookup (
id NUMBER(10),
description VARCHAR2(50)
);
ALTER TABLE lookup ADD (
CONSTRAINT lookup_pk PRIMARY KEY (id)
);
INSERT INTO lookup (id, description) VALUES (1, 'ONE');
INSERT INTO lookup (id, description) VALUES (2, 'TWO');
INSERT INTO lookup (id, description) VALUES (3, 'THREE');
COMMIT;

Creëer en vul een grotere tabel die we later zullen partitioneren.

 

CREATE TABLE big_table (
id NUMBER(10),
created_date DATE,
lookup_id NUMBER(10),
data VARCHAR2(50)
);
DECLARE
l_lookup_id lookup.id%TYPE;
l_create_date DATE;
BEGIN
FOR i IN 1 .. 1000000 LOOP
IF MOD(i, 3) = 0 THEN
l_create_date := ADD_MONTHS(SYSDATE, -24);
l_lookup_id := 2;
ELSIF MOD(i, 2) = 0 THEN
l_create_date := ADD_MONTHS(SYSDATE, -12);
l_lookup_id := 1;
ELSE
l_create_date := SYSDATE;
l_lookup_id := 3;
END IF;

INSERT INTO big_table (id, created_date, lookup_id, data)
VALUES (i, l_create_date, l_lookup_id, 'This is some data for ' || i);
END LOOP;
COMMIT;
END;
/

Voeg wat constraints aan de tabel toe.

ALTER TABLE big_table ADD (
CONSTRAINT big_table_pk PRIMARY KEY (id)
);
CREATE INDEX bita_created_date_i ON big_table(created_date);
CREATE INDEX bita_look_fk_i ON big_table(lookup_id);
ALTER TABLE big_table ADD (
CONSTRAINT bita_look_fk
FOREIGN KEY (lookup_id)
REFERENCES lookup(id)
);

Gather statistics van het schema


EXEC DBMS_STATS.gather_table_stats(USER, 'LOOKUP', cascade => TRUE);
EXEC DBMS_STATS.gather_table_stats(USER, 'BIG_TABLE', cascade = > TRUE);



Vervolgens maken we een nieuwe tabel met de juiste partitie structuur, die als "destination" tabel kan fungeren. De "destination" moet dezelfde constraints en indexen gedefinieerd hebben.


Creëer de gepartitioneerde tabel.

 
CREATE TABLE big_table2 (
id NUMBER(10),
created_date DATE,
lookup_id NUMBER(10),
data VARCHAR2(50)
)
PARTITION BY RANGE (created_date)
(PARTITION big_table_2007 VALUES LESS THAN (MAXVALUE));

Voeg nieuwe keys. FK's en triggers toe.

 

ALTER TABLE big_table2 ADD (
CONSTRAINT big_table_pk2 PRIMARY KEY (id)
);
CREATE INDEX bita_created_date_i2 ON big_table2(created_date) LOCAL;
CREATE INDEX bita_look_fk_i2 ON big_table2(lookup_id) LOCAL;
ALTER TABLE big_table2 ADD (
CONSTRAINT bita_look_fk2
FOREIGN KEY (lookup_id)
REFERENCES lookup(id)
);


Nu kunnen we beginnen met de conversie

 

EXCHANGE PARTITION


Nu kunnen we met de EXCHANGE PARTIOTION syntax de segmenten die overeenkomen met de bron tabel en de partitie, wisselen.


ALTER TABLE big_table2
EXCHANGE PARTITION big_table_2007
WITH TABLE big_table
WITHOUT VALIDATION
UPDATE GLOBAL INDEXES;


De wissel operatie zou niet beïnvloed moeten worden door de betrokken segmenten.

Als dit eenmaal klaar is kunnen we de oude tabel droppen en de nieuwe hernoemen , samen met al zijn constraints.

DROP TABLE big_table;
RENAME big_table2 TO big_table;
ALTER TABLE big_table RENAME CONSTRAINT big_table_pk2 TO big_table_pk;
ALTER TABLE big_table RENAME CONSTRAINT bita_look_fk2 TO bita_look_fk;
ALTER INDEX big_table_pk2 RENAME TO big_table_pk;
ALTER INDEX bita_look_fk_i2 RENAME TO bita_look_fk_i;
ALTER INDEX bita_created_date_i2 RENAME TO bita_created_date_i;

 

SPLIT PARTITION


Vervolgens splitten we de enkele grote partitie in kleinere partities.

ALTER TABLE big_table
SPLIT PARTITION big_table_2007 AT (TO_DATE('31-DEC-2005 23:59:59', 'DD-MON-YYYY HH24:MI:SS'))
INTO (PARTITION big_table_2005,
PARTITION big_table_2007)
UPDATE GLOBAL INDEXES;
EXEC DBMS_STATS.gather_table_stats(USER, 'BIG_TABLE', cascade => TRUE);

De volgende query laat zien dat het partitioneren succesvol was.

SELECT partitioned
FROM user_tables
WHERE table_name = 'BIG_TABLE';
PAR
---
YES
1 row selected.
SELECT partition_name, num_rows
FROM user_tab_partitions
WHERE table_name = 'BIG_TABLE';
PARTITION_NAME                   NUM_ROWS
------------------------------ ----------
BIG_TABLE_2005 335326
BIG_TABLE_2006 332730
BIG_TABLE_2007 334340
3 rows selected.
 

TOP 

zie ook

Gepartitioneerde Tabellen en Indexen in Oracle 10g
Partitioneren met DBMS_REDEFINITION

 

Advertentie

>

Poll

Voorkeur
 

Wie is er aanwezig

We hebben 277 gasten online