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

Oracle性能优化:SQL优化之十七——索引分区表插入

oracleEDU 2017-11-12
1265

如果表没有索引,插入的速度一般都不会慢,只有在有索引的情况下,才要考虑插入速度的优化。

如果表有大量索引,一般来说,分区表的局部索引由于只需要更新局部分区的索引,所以索引的开销会比较小,插入性能比有着相同的记录数、列及索引的普通表更快。

下面以案例说明。

构建环境

创建分区表,插入数据:

SQL> drop table range_part_tab purge;

SQL> create table range_part_tab (id number,deal_date date,area_code number,nbr1 number,nbr2 number,nbr3 number,contents varchar2(4000)) partition by range (deal_date) (

partition p_201601 values less than (TO_DATE('2016-02-01', 'YYYY-MM-DD')),

partition p_201602 values less than (TO_DATE('2016-03-01', 'YYYY-MM-DD')),

partition p_201603 values less than (TO_DATE('2016-04-01', 'YYYY-MM-DD')),

partition p_201604 values less than (TO_DATE('2016-05-01', 'YYYY-MM-DD')),

partition p_201605 values less than (TO_DATE('2016-06-01', 'YYYY-MM-DD')),

partition p_201606 values less than (TO_DATE('2016-07-01', 'YYYY-MM-DD')),

partition p_201607 values less than (TO_DATE('2016-08-01', 'YYYY-MM-DD')),

partition p_201608 values less than (TO_DATE('2016-09-01', 'YYYY-MM-DD')),

partition p_201609 values less than (TO_DATE('2016-10-01', 'YYYY-MM-DD')),

partition p_201610 values less than (TO_DATE('2016-11-01', 'YYYY-MM-DD')),

partition p_201611 values less than (TO_DATE('2016-12-01', 'YYYY-MM-DD')),

partition p_201612 values less than (TO_DATE('2017-01-01', 'YYYY-MM-DD')),

partition p_201701 values less than (TO_DATE('2017-02-01', 'YYYY-MM-DD')),

partition p_201702 values less than (TO_DATE('2017-03-01', 'YYYY-MM-DD')),

partition p_201703 values less than (TO_DATE('2017-04-01', 'YYYY-MM-DD')),

partition p_201704 values less than (TO_DATE('2017-05-01', 'YYYY-MM-DD')),

partition p_max values less than (maxvalue)

);

以下是插入2016年一整年日期随机数和表示xx地区号含义(591到599)的随机数记录,共有100万条,如下:

SQL> insert into range_part_tab (id,deal_date,area_code,nbr1,nbr2,nbr3,contents) select rownum,to_date( to_char(sysdate-365,'J')+TRUNC(DBMS_RANDOM.VALUE(0,365)),'J'),ceil(dbms_random.value(591,599)),ceil(dbms_random.value(18900000001,18999999999)),ceil(dbms_random.value(18900000001,18999999999)),ceil(dbms_random.value(18900000001,18999999999)),rpad('*',400,'*')

from dual connect by rownum <= 2000000;

SQL>commit;

以下是插入2017年一整年日期随机数和表示xx地区号含义(591到599)的随机数记录,共有20万条,如下:

SQL> insert into range_part_tab (id,deal_date,area_code,nbr1,nbr2,nbr3,contents) select rownum,

to_date(to_char(sysdate,'J')+TRUNC(DBMS_RANDOM.VALUE(0,60)),'J'), ceil(dbms_random.value(591,599)),ceil(dbms_random.value(18900000001,18999999999)),ceil(dbms_random.value(18900000001,18999999999)),ceil(dbms_random.value(18900000001,18999999999)), rpad('*',400,'*') from dual connect by rownum <= 400000;

SQL> commit;

创建索引

SQL> create index idx_parttab_id on range_part_tab(id) local;

SQL> create index idx_parttab_nbr1 on range_part_tab(nbr1) local;

SQL> create index idx_parttab_nbr2 on range_part_tab(nbr2) local;

SQL> create index idx_parttab_nbr3 on range_part_tab(nbr3) local;

SQL> create index idx_parttab_area on range_part_tab(area_code) local;

创建普通表

SQL> drop table normal_tab purge;

SQL> create table normal_tab (id number,deal_date date,area_code number,nbr1 number,nbr2 number,nbr3 number,contents varchar2(4000));

SQL> insert into normal_tab select * from range_part_tab;

SQL> commit;

创建索引:

SQL> create index idx_tab_id on normal_tab(id) ;

SQL> create index idx_tab_nbr1 on normal_tab(nbr1) ;

SQL> create index idx_tab_nbr2 on normal_tab(nbr2) ;

SQL> create index idx_tab_nbr3 on normal_tab(nbr3) ;

SQL> create index idx_tab_area on normal_tab(area_code) ;

测试插入分区表:

SQL>set timing on

SQL>insert into range_part_tab 

     select rownum,

             to_date( to_char(sysdate+60,'J')+TRUNC(DBMS_RANDOM.VALUE(0,60)),'J'),

             ceil(dbms_random.value(591,599)),

             ceil(dbms_random.value(18900000001,18999999999)),

             ceil(dbms_random.value(18900000001,18999999999)),

             ceil(dbms_random.value(18900000001,18999999999)),

             rpad('*',400,'*')

        from dual

      connect by rownum <= 400000;

已创建400000行。

已用时间:  00: 00: 51.20

SQL> commit;

测试插入普通表:

SQL>insert into normal_tab 

     select rownum,

             to_date( to_char(sysdate+60,'J')+TRUNC(DBMS_RANDOM.VALUE(0,60)),'J'),

             ceil(dbms_random.value(591,599)),

             ceil(dbms_random.value(18900000001,18999999999)),

             ceil(dbms_random.value(18900000001,18999999999)),

             ceil(dbms_random.value(18900000001,18999999999)),

             rpad('*',400,'*')

        from dual

      connect by rownum <= 400000;

已创建400000行。

已用时间:  00: 01: 20.04

SQL> commit;

最后修改时间:2021-04-28 20:27:26
文章转载自oracleEDU,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论