问:有一张大表,其中按时间字段(TIME_ID)进行表分区(按季度分区),但是如果业务人员做跨季度的大批量数据的查询时,未能走TIME_ID分区索引,导致全表扫描。此种情况该如何处理?
示例解析:
1、我们根据oracle中sh用户自带的SALES表创建一个新的分区表(TIME_ID上建季度分区;插入一个新的字段CREATE_ID,上面不建分区),步骤如下:
create table SALES_1998_part
(
PROD_ID NUMBER not null,
CUST_ID NUMBER not null,
TIME_ID DATE not null,
CREATE_ID DATE not null
)
partition by range (TIME_ID)
(
partition SALES_Q1_1998 values less than (TO_DATE(' 1998-04-01', 'SYYYY-MM-DD'))
tablespace EXAMPLE
pctfree 0
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
),
partition SALES_Q2_1998 values less than (TO_DATE(' 1998-07-01', 'SYYYY-MM-DD'))
tablespace EXAMPLE
pctfree 0
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
),
partition SALES_Q3_1998 values less than (TO_DATE(' 1998-10-01', 'SYYYY-MM-DD'))
tablespace EXAMPLE
pctfree 0
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
),
partition SALES_Q4_1998 values less than (TO_DATE(' 1999-01-01', 'SYYYY-MM-DD'))
tablespace EXAMPLE
pctfree 0
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
)
);
2、向sales_1998_part表中插入值,CREATE_ID的值来处于sales表的TIME_ID值。
insert into sales_1998_part(PROD_ID,CUST_ID,TIME_ID,CREATE_ID)
select PROD_ID,CUST_ID,TIME_ID,TIME_ID from sales
where time_id >= TO_DATE(' 1998-01-01', 'SYYYY-MM-DD')
and time_id < TO_DATE(' 1999-01-01', 'SYYYY-MM-DD');
commit;
select count(*) from sales_1998_part;
3、查询,查看执行计划
1) 使用分区键检索(使用分区键进行索引,会自动在数据存在的区进行检索。因为开始区为2,结束区为3。)
select * from sales_1998_part where TIME_ID >= TO_DATE(' 1998-04-01', 'SYYYY-MM-DD')
and TIME_ID <= TO_DATE(' 1998-10-01', 'SYYYY-MM-DD');
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Pstart| Pstop |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 83425 | | |
| 1 | PARTITION RANGE ITERATOR| | 83425 | 2 | 3 |
|* 2 | TABLE ACCESS FULL | SALES_1998_PART | 83425 | 2 | 3 |
----------------------------------------------------------------------------
2) 不使用分区键检索(没有使用分区键进行检索,那么则会查询全部的表分区。因为要查询的数据就在2和3分区上,其它的分区数据也被读取了,增大了数据库压力,效率低下。)
select * from sales_1998_part where CREATE_ID >= TO_DATE(' 1998-04-01', 'SYYYY-MM-DD')
and CREATE_ID <= TO_DATE(' 1998-10-01', 'SYYYY-MM-DD');
-----------------------------------------------------------------------
| Id | Operation | Name | Rows | Pstart| Pstop |
-----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 86662 | | |
| 1 | PARTITION RANGE ALL| | 86662 | 1 | 4 |
|* 2 | TABLE ACCESS FULL | SALES_1998_PART | 86662 | 1 | 4 |
-----------------------------------------------------------------------