暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

【干货攻略】达梦数据库如何实现普通表转换为分区表

达梦E学 2022-03-16
1467

-----正文-----



在生产环境中,数据库中一开始用的是普通表,但随着时间推移,数据量越来越大,可以考虑将普通表转换为分区表,提升数据库的性能。
本章介绍在DM8数据库中,实现将普通表转换为分区表的方法。


环境说明

数据库版本:DM8
操作系统版本:麒麟V10
相关关键字:性能优化

 

一、逻辑导入导出方式


1.1、创建测试表:

    SQL> create table t1 (id int);
    begin
    for i in 1..10000 loop
    insert into t1 values (i);
    commit;
    end loop;
    end;
    /

    1.2、导出表t1:

      [dmdba@localhost bin]$ ./dexp SYSDBA/Dameng123 file=/dm8/backup/t1.dmp tables=t1

      1.3、删除表t1:

        SQL>drop table t1;

        1.4、创建分区表:

          SQL> create table t1 (id int)
          partition by range (id)
          interval(2000)
          (partition p1 values less than (2000),
          partition p2 values less than (4000),
          partition p3 values less than (6000),
          partition p4 values less than (8000),
          partition p5 values less than (10000),
          partition p6 values less than (12000)
          );

          1.5、导入数据至分区表中:

            [dmdba@localhost bin]$ ./dimp SYSDBA/Dameng123 file=/dm8/backup/t1.dmp tables=t1 rows=y ignore=y


            二、分区交换方式

            2.1、创建测试表

              SQL> create table t1 (id int);
              begin
              for i in 1..10000 loop
              insert into t1 values (i);
              commit;
              end loop;
              end;
              /

              2.2、创建分区表

                SQL> create table t2 (id int)
                partition by range (id)
                (
                partition p1 values less than (2000),
                partition p2 values less than (4000),
                partition pn values less than (maxvalue)
                );

                2.3、交换分区

                将普通表t1的数据交换到分区表t2的 pn分区,执行之前需要取消主键,删除索引,交换完成后进行重建。

                  SQL> alter table t2 exchange partition pn with table t1;

                  2.4、查询分区表的数据

                  交换分区后,数据并不进行校验,数据都会进入该分区。

                    SQL> select 'p1' partition_name, count(*) num from t2 partition(p1)
                    union all
                    select 'p2', count(*) from t2 partition(p2)
                    union all
                    select 'p3', count(*) from t2 partition(pn);

                    2.5、分区拆分

                    上述数据交换后,所有数据都在一个分区,可以使用分区拆分,分区拆分会对分区中的数据进行重组。

                      SQL> alter table t2 split partition pn at (10000) into (partition p3, partition pmax);

                      2.6、查询拆分后的分区

                        SQL> select 'p1' partition_name, count(*) num from t2 partition(p1)
                        union all
                        select 'p2', count(*) from t2 partition(p2)
                        union all
                        select 'p3', count(*) from t2 partition(p3)
                        union all
                        select 'p4', count(*) from t2 partition(pmax);

                        注意:

                        (1)分区交换仅支持范围和列表分区,不支持HASH分区。

                        (2)分区交换时不会进行数据校验,如果交换表的数据不符合分区范围,数据仍然会进入该分区。此时如果是范围分区可以使用split拆分分区,系统会自动对数据进行重组。

                        (3)在生产环境中,为保证数据安全,建议对源表的数据备份后再做分区交换。


                        >>> THE END <<<

                        好,以上是本期干货分享内容,希望能给大家带来帮助。关注本微信公众号,获取更多技术干货!




                         
                         


                        相关推荐

                        干货 达梦数据库如何实现基于操作系统的身份验证
                        干货 | DM8+Django+Python搭建网站
                        师资 | 达梦1+X证书高级师资培训开班通知
                        开班 | 达梦认证专家DCP在线认证课程招生
                        开班 | 达梦DCA(2022-3月)开始报名!

                        内容丨林夕
                        编辑丨Hh
                        文章转载自达梦E学,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                        评论