{"id":401,"date":"2015-11-16T13:24:39","date_gmt":"2015-11-16T13:24:39","guid":{"rendered":"http:\/\/onlinelab.info\/?p=401"},"modified":"2015-11-16T13:24:39","modified_gmt":"2015-11-16T13:24:39","slug":"dba-huong-dan-thao-tac-voi-partition-trong-oracle-db","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/11\/16\/dba-huong-dan-thao-tac-voi-partition-trong-oracle-db\/","title":{"rendered":"[DBA] H\u01b0\u1edbng d\u1eabn thao t\u00e1c v\u1edbi Partition trong Oracle DB"},"content":{"rendered":"<h2>H\u01b0\u1edbng d\u1eabn thao t\u00e1c v\u1edbi Partition trong Oracle DB<\/h2>\n<h2>Drop partition<\/h2>\n<p>Drop m\u1ed9t partition s\u1ebd x\u00f3a to\u00e0n b\u1ed9 d\u1eef li\u1ec7u tr\u00ean partition \u0111\u00f3<\/p>\n<blockquote><p>\u2013drop a partition<\/p>\n<p>alter table thetest drop partition p201510;<\/p><\/blockquote>\n<p>\u2013&gt;drop a partition + its data + drop the corresponding partitions in each local index<\/p>\n<h2>Insert d\u1eef li\u1ec7u v\u00e0o m\u1ed9t b\u1ea3ng \u0111\u00e3 c\u00f3 partition<\/h2>\n<p>N\u1ebfu d\u1eef li\u1ec7u kh\u00f4ng thu\u1ed9c partition n\u00e0o c\u00f3 s\u1eb5n th\u00ec s\u1ebd kh\u00f4ng them \u0111\u01b0\u1ee3c tr\u1eeb khi cho th\u00eam partition v\u00e0o b\u1ea3ng. C\u00e2u l\u1ec7nh INSERT kh\u00f4ng kh\u00e1c g\u00ec v\u1edbi c\u00e2u INSERT th\u00f4ng th\u01b0\u1eddng.<\/p>\n<h2>Th\u00eam partition v\u00e0o m\u1ed9t b\u1ea3ng \u0111\u00e3 c\u00f3 s\u1eb5n partition (k\u1ec3 c\u1ea3 composite partition)<\/h2>\n<p>B\u00e0i n\u00e0y d\u1ef1a tr\u00ean ki\u1ebfn th\u1ee9c c\u01a1 b\u1ea3n v\u1ec1 partition nh\u01b0 c\u00e1c lo\u1ea1i partition, c\u00e1ch \u0111\u00e1nh partition, \u2026 n\u00ean s\u1ebd kh\u00f4ng n\u00f3i l\u1ea1i c\u00e1c ph\u1ea7n c\u0103n b\u1ea3n n\u00e0y.<\/p>\n<p>S\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh sau:<\/p>\n<blockquote><p>ALTER TABLE thetest2<\/p>\n<p>ADD PARTITION P201511 VALUES LESS THAN (to_date(\u201820151201\u2032,\u2019YYYYMMDD\u2019));<\/p><\/blockquote>\n<h2>Th\u00eam subpartition v\u00e0o m\u1ed9t b\u1ea3ng c\u00f3 s\u1eb5n partition (*-list partition)<\/h2>\n<blockquote><p>ALTER TABLE THETESt2 MODIFY PARTITION P201509<\/p>\n<p>add subpartition P201509_PC values (\u2018C\u2019, \u2018c\u2019);<\/p><\/blockquote>\n<p>Nh\u1eadn th\u1ea5y, ta c\u1ea7n ph\u1ea3i ch\u1ec9 \u0111\u1ecbnh r\u00f5 rang partition tr\u01b0\u1edbc khi th\u00eam m\u1ed9t subpartition v\u00e0 c\u1ea5u tr\u00fac c\u1ee7a vi\u1ec7c insert n\u00e0y ph\u1ea3i l\u00e0 ALTER TABLE \u2026 MODIFY PARTITION \u2026 ch\u1ee9 kh\u00f4ng gi\u1ed1ng nh\u01b0 th\u00eam partition.<\/p>\n<p>Tuy v\u1eady ta c\u00f3 th\u1ec3 th\u00eam subpartition v\u00e0o c\u00f9ng v\u1edbi partition trong m\u1ed9t c\u00e2u l\u1ec7nh nh\u01b0 sau:<\/p>\n<blockquote><p>ALTER TABLE thetest2<\/p>\n<p>ADD PARTITION P201512 VALUES LESS THAN (to_date(\u201820160101\u2032,\u2019YYYYMMDD\u2019))<\/p>\n<p>(<\/p>\n<p>SUBPARTITION P201512_PA values (\u2018A\u2019, \u2018a\u2019),<\/p>\n<p>SUBPARTITION P201512_PB values (\u2018B\u2019, \u2018b\u2019),<\/p>\n<p>SUBPARTITION P201512_PC values (\u2018C\u2019, \u2018c\u2019)<\/p>\n<p>)<\/p><\/blockquote>\n<p>Chi ti\u1ebft h\u01a1n c\u00f3 th\u1ec3 tham kh\u1ea3o t\u1ea1i:<em><a href=\"https:\/\/docs.oracle.com\/cd\/E18283_01\/server.112\/e16541\/part_admin002.htm#i1007318\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.oracle.com\/cd\/E18283_01\/server.112\/e16541\/part_admin002.htm#i1007318<\/a><\/em><\/p>\n<h2>T\u1ea1o m\u1edbi partition v\u00e0o m\u1ed9t table c\u00f3 s\u1eb5n d\u1eef li\u1ec7u nh\u01b0ng ch\u01b0a c\u00f3 partition<\/h2>\n<p>\u0110\u1ec3 t\u1ea1o m\u1edbi partition v\u00e0o m\u1ed9t table c\u00f3 s\u1eb5n d\u1eef li\u1ec7u khi \u1edf khai b\u00e1o m\u1eb7c \u0111\u1ecbnh c\u1ee7a table l\u1ea1i kh\u00f4ng c\u00f3 partition th\u00ec c\u1ea7n s\u1eed d\u1ee5ng<strong>DBMS_REDEFINITION<\/strong> ho\u1eb7c <strong>ALTER TABLE \u2026 EXCHANGE PARTITION<\/strong>.<\/p>\n<p>C\u1ea3 hai ph\u01b0\u01a1ng ph\u00e1p n\u00e0y \u0111\u1ec3 d\u1ef1a tr\u00ean c\u00e1ch th\u1ee9c l\u00e0 s\u1ebd t\u1ea1o m\u1ed9t b\u1ea3ng m\u1edbi v\u1edbi c\u1ea5u tr\u00fac gi\u1ed1ng h\u1ec7t nh\u01b0ng \u0111\u00e3 khai b\u00e1o partition khi t\u1ea1o m\u1edbi v\u00e0 swap gi\u1eefa hai b\u1ea3ng n\u00e0y. Cu\u1ed1i c\u00f9ng sau khi \u0111\u00e3 swap xong th\u00ec s\u1ebd th\u1ef1c hi\u1ec7n vi\u1ec7c drop b\u1ea3ng t\u1ea1m \u0111i. C\u1ee5 th\u1ec3 nh\u01b0 sau:<\/p>\n<h3>B\u01b0\u1edbc 1: Kh\u1edfi t\u1ea1o d\u1eef li\u1ec7u sample<\/h3>\n<p>Gi\u1ea3 s\u1eed ta c\u00f3 m\u1ed9t b\u1ea3ng v\u1edbi d\u1eef li\u1ec7u \u0111\u00e3 c\u00f3 s\u1eb5n nh\u01b0 sau:<\/p>\n<blockquote><p>create table thetest(<\/p>\n<p>logid number,<\/p>\n<p>transdate date,<\/p>\n<p>status varchar2(50),<\/p>\n<p>pid varchar2(50),<\/p>\n<p>constraint pk_logid PRIMARY KEY(logid)<\/p>\n<p>);<\/p>\n<p>insert into thetest values(1,to_date(\u201820151101\u2019, \u2018YYYYMMDD\u2019), \u20181\u2019, \u2018A\u2019);<\/p>\n<p>insert into thetest values(2,to_date(\u201820151001\u2019, \u2018YYYYMMDD\u2019), \u20181\u2019, \u2018A\u2019);<\/p>\n<p>insert into thetest values(3,to_date(\u201820151201\u2019, \u2018YYYYMMDD\u2019), \u20181\u2019, \u2018A\u2019);<\/p><\/blockquote>\n<h3>B\u01b0\u1edbc 2: T\u1ea1o m\u1ed9t b\u1ea3ng t\u01b0\u01a1ng t\u1ef1 \u0111\u1ec3 swap<\/h3>\n<p>T\u1ea1o m\u1edbi m\u1ed9t b\u1ea3ng s\u1ea1ch s\u1ebd v\u1edbi c\u1ea5u tr\u00fac gi\u1ed1ng h\u1ec7t nh\u01b0ng \u0111\u00e3 khai b\u00e1o partition. \u1ede \u0111\u00e2y t\u00f4i dung composite partition:<\/p>\n<blockquote><p>create table thetest2(<\/p>\n<p>logid number,<\/p>\n<p>transdate date,<\/p>\n<p>status varchar2(50),<\/p>\n<p>pid varchar2(50),<\/p>\n<p>constraint pk_logid2 PRIMARY KEY(logid))<\/p>\n<p>partition by range(transdate) subpartition by list(pid)<\/p>\n<p>subpartition template(<\/p>\n<p>subpartition pa values(\u2018A\u2019, \u2018a\u2019),<\/p>\n<p>subpartition pb values(\u2018B\u2019, \u2018b\u2019)<\/p>\n<p>)<\/p>\n<p>(<\/p>\n<p>partition p201509 values less than (to_date(\u201820151001\u2032,\u2019YYYYMMDD\u2019)),<\/p>\n<p>partition p201510 values less than (to_date(\u201820151101\u2032,\u2019YYYYMMDD\u2019))<\/p>\n<p>)<\/p><\/blockquote>\n<h3>B\u01b0\u1edbc 3: Ki\u1ec3m tra xem c\u00f3 redefinition \u0111\u01b0\u1ee3c b\u1ea3ng ban \u0111\u1ea7u kh\u00f4ng<\/h3>\n<p>\u0110\u0103ng nh\u1eadp b\u1eb1ng user <strong>SYS<\/strong> ho\u1eb7c m\u1ed9t user n\u00e0o \u0111\u01b0\u1ee3c g\u00e1n quy\u1ec1n <strong>EXEC DBMS_REDEFINITION<\/strong><\/p>\n<blockquote><p>EXEC <strong><em>DBMS_REDEFINITION.can_redef_table<\/em><\/strong>(\u2018SCHEMA\u2019, \u2018THETEST\u2019);<\/p><\/blockquote>\n<p>N\u1ebfu c\u00e2u l\u1ec7nh tr\u00ean kh\u00f4ng g\u00e2y l\u1ed7i t\u1ee9c l\u00e0 c\u00f3 th\u1ec3 redefine l\u1ea1i b\u1ea3ng \u0111\u01b0\u1ee3c v\u1edbi user \u0111\u00e3 khai b\u00e1o<\/p>\n<h3>B\u01b0\u1edbc 4: Ghi nh\u1edb c\u00e2u l\u1ec7nh stop\/abort<\/h3>\n<p>L\u00e0 m\u1ed9t DBA, b\u1ea1n n\u00ean ghi nh\u1edb c\u00e2u stop tr\u01b0\u1edbc khi h\u1ecdc c\u00e2u start.!<\/p>\n<blockquote><p>EXEC DBMS_REDEFINITION.abort_redef_table(\u2018SCHEMA\u2019, \u2018THETEST\u2019, \u2018THETEST2\u2019);<\/p><\/blockquote>\n<p>N\u1ebfu qu\u00e1 tr\u00ecnh organize table c\u00f3 v\u1ea5n \u0111\u1ec1 ch\u00fang ta s\u1ebd ph\u1ea3i th\u1ef1c hi\u1ec7n vi\u1ec7c restart l\u1ea1i qu\u00e1 tr\u00ecnh n\u00e0y nh\u01b0ng b\u1edfi v\u00ec redefinition s\u1ebd y\u00eau c\u1ea7u t\u1ea1o snapshot n\u00ean ph\u1ea3i ch\u1ea1y c\u00e2u l\u00eanh <strong>abort_redef_table()<\/strong> \u1edf tr\u00ean \u0111\u1ec3 release snapshot tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u l\u1ea1i.<\/p>\n<p>C\u00f3 th\u1ec3 th\u1eed b\u1eb1ng c\u00e1ch t\u1ea1o \u00edt partition \u1edf b\u1ea3ng t\u1ea1m h\u01a1n so v\u1edbi c\u00e1c gi\u00e1 tr\u1ecb c\u1ee7a b\u1ea3ng ch\u00ednh. Khi n\u00e0y sau khi b\u1eaft \u0111\u1ea7u ti\u1ebfn tr\u00ecnh th\u00ec Oracle s\u1ebd b\u00e1o l\u1ed7i do m\u1ed9t s\u1ed1 gi\u00e1 tr\u1ecb s\u1ebd kh\u00f4ng bi\u1ebft cho v\u00e0o partition n\u00e0o. L\u00fac n\u00e0y gi\u1ea3 s\u1eed r\u1eb1ng ta \u0111\u00e3 t\u1ea1o th\u00eam partition \u1edf b\u1ea3ng t\u1ea1m cho chu\u1ea9n v\u1edbi d\u1eef li\u1ec7u (c\u00e1ch th\u00eam partition v\u00e0o b\u1ea3ng c\u00f3 s\u1eb5n d\u1eef li\u1ec7u \u0111\u00e3 n\u00eau \u1edf tr\u00ean) ch\u1ea1y l\u1ea1i c\u00e2u l\u1ec7nh<strong>start_redef_table()<\/strong> s\u1ebd b\u1ecb l\u1ed7i:<\/p>\n<blockquote><p>ORA-23539: table \u201cSCHEMA\u201d.\u201dTHETEST\u201d currently being redefined<\/p><\/blockquote>\n<p>Nh\u01b0 v\u1eady Oracle v\u1eabn ghi nh\u1eadn b\u1ea3ng THETSET \u0111ang \u0111\u01b0\u1ee3c redefined n\u00ean t\u1ea5t nhi\u00ean s\u1ebd ng\u0103n c\u1ea5m kh\u00f4ng cho th\u1ef1c hi\u1ec7n c\u00e2u l\u1ec7nh.<\/p>\n<h3>B\u01b0\u1edbc 5: Th\u1ef1c hi\u1ec7n ti\u1ebfn tr\u00ecnh<\/h3>\n<blockquote><p>EXEC <strong><em>DBMS_REDEFINITION.start_redef_table<\/em><\/strong>(\u2018SCHEMA\u2019, \u2018THETEST\u2019, \u2018THETEST2\u2019);<\/p>\n<p>\u2014<\/p><\/blockquote>\n<p>L\u01b0u \u00fd: sau c\u00e2u l\u1ec7nh n\u00e0y, d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c chuy\u1ec3n sang b\u1ea3ng THETEST2 nh\u01b0ng d\u1eef li\u1ec7u tr\u00ean b\u1ea3ng THETEST v\u1eabn c\u00f2n. Th\u1eddi gian th\u1ef1c hi\u1ec7n ti\u1ebfn tr\u00ecnh n\u00e0y kh\u00f4ng nhanh v\u00e0 ph\u1ee5 thu\u1ed9c v\u00e0o d\u1eef li\u1ec7u trong DB.<\/p>\n<blockquote><p>exec <strong><em>DBMS_REDEFINITION.sync_interim_table<\/em><\/strong>(\u2018SCHEMA\u2019, \u2018THETEST\u2019, \u2018THETEST2\u2019);<\/p><\/blockquote>\n<p>Cu\u1ed1i c\u00f9ng l\u00e0 k\u1ebft th\u00fac qu\u00e1 tr\u00ecnh redefinition<\/p>\n<blockquote><p>exec <strong><em>DBMS_REDEFINITION.finish_redef_table<\/em><\/strong>(\u2018SCHEMA\u2019, \u2018THETEST\u2019, \u2018THETEST2\u2019);<\/p><\/blockquote>\n<p>Sau c\u00e2u l\u1ec7nh n\u00e0y, ph\u1ea7n b\u1ea3ng THETEST \u0111\u00e3 c\u00f3 partition \u0111\u1ed3ng th\u1eddi c\u00f3 d\u1eef li\u1ec7u \u0111\u1ea7y \u0111\u1ee7. B\u00e2y gi\u1edd c\u00f3 th\u1ec3 drop b\u1ea3ng t\u1ea1m THETEST2 \u0111i do b\u1ea3ng n\u00e0y v\u1eabn c\u00f2n d\u1eef li\u1ec7u nh\u01b0ng kh\u00f4ng c\u00f2n c\u1ea7n thi\u1ebft n\u1eefa.<\/p>\n<p>Th\u1eddi gian th\u1ef1c hi\u1ec7n ti\u1ebfn tr\u00ecnh finish n\u00e0y kh\u00e1 d\u00e0i n\u00ean \u0111\u00f2i h\u1ecfi t\u00ednh to\u00e1n h\u1ee3p l\u00fd v\u00e0 c\u00f3 s\u1ef1 ki\u00ean nh\u1eabn. Th\u00eam m\u1ed9t v\u1ea5n \u0111\u1ec1 khi l\u00e0m DBA l\u00e0 t\u00ecnh ki\u00ean tr\u00ec quy\u1ebft kh\u00f4ng b\u1ea5m Cancel. Thay v\u00ec \u0111\u00f3 h\u00e3y gi\u00e0nh th\u1eddi gian chu\u1ea9n b\u1ecb k\u1ef9 c\u00e0ng v\u00e0 h\u1ecdc c\u00e1ch stop ti\u1ebfn tr\u00ecnh.<\/p>\n<h3>B\u01b0\u1edbc 6: D\u1ecdn d\u1eb9p ti\u1ebfn tr\u00ecnh<\/h3>\n<blockquote><p>drop table thetest2;<\/p>\n<p>alter table thetest rename constraint pk_logid2 to pk_logid;<\/p>\n<p>alter index pk_logid2 rename to pk_logid;<\/p><\/blockquote>\n<p>\u0110o\u1ea1n tr\u00ean \u0111\u00e3 th\u1ef1c hi\u1ec7n x\u00f3a b\u1ea3ng t\u1ea1m, \u0111\u1ed5i t\u00ean l\u1ea1i c\u00e1c constraint v\u00e0 c\u00e1c index cho \u0111\u00fang v\u1edbi b\u1ea3ng c\u0169. Nghi\u00ean c\u1ee9u v\u00e0 ki\u1ec3m tra ch\u00ednh x\u00e1c b\u1ea3ng c\u0169 l\u00e0 r\u1ea5t quan tr\u1ecdng.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>H\u01b0\u1edbng d\u1eabn thao t\u00e1c v\u1edbi Partition trong Oracle DB Drop partition Drop m\u1ed9t partition s\u1ebd x\u00f3a to\u00e0n b\u1ed9 d\u1eef li\u1ec7u tr\u00ean partition \u0111\u00f3 \u2013drop a partition alter table thetest drop partition p201510; \u2013&gt;drop a partition&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}