我有一个具有间隔分区的表。
CREATE TABLE interval_tab (
id NUMBER,
code VARCHAR2(10),
description VARCHAR2(50),
created_date DATE
)
PARTITION BY RANGE (created_date)
INTERVAL (NUMTODSINTERVAL(1, 'day'))
(
PARTITION part_01 values LESS THAN (TO_DATE('01-NOV-2017','DD-MON-YYYY'))
);
每天都会在新分区中将数据加载到该表中,我需要从最新分区中选择数据。如何查询以获取最新分区中的数据?
谢谢!
答案 0 :(得分:3)
Oracle应该能够根据您的查询谓词确定要使用的分区。如果您仅选择当天的数据,Oracle应该执行以下操作:
create table test_data (
id NUMBER,
code VARCHAR2(10),
description VARCHAR2(50),
created_date DATE default sysdate not null
)
PARTITION BY RANGE (created_date)
INTERVAL(NUMTODSINTERVAL(1, 'day'))
(
PARTITION part_01 values LESS THAN (TO_DATE('20180627','YYYYMMDD'))
);
insert into test_data values (1,'A','A code',sysdate-3);
insert into test_data values (1,'B','B code',sysdate-2);
insert into test_data values (1,'C','C code',sysdate-1);
insert into test_data values (1,'D','D code',sysdate);
insert into test_data values (1,'E','E code',sysdate);
commit;
select * from test_data
where created_date >= trunc(sysdate);
输出:
ID CODE DESCRIPTION CREATED_DATE
1 D D code 6/27/2018 9:11:07 AM
1 E E code 6/27/2018 9:11:07 AM
说明计划如下:
Plan
SELECT STATEMENT ALL_ROWS
Cost: 4 Bytes: 168 Cardinality: 3
2 PARTITION RANGE ITERATOR
Cost: 4 Bytes: 168 Cardinality: 3 Partition #: 1 Partitions determined by Key Values
1 TABLE ACCESS FULL TABLE TEST_DATA
Cost: 4 Bytes: 168 Cardinality: 3 Partition #: 1 Partitions determined by Key Values
您还可以根据情况和需求添加索引(本地或全局)。
注意:您还可以使用“ partition for”子句从分区中选择数据而无需知道其名称(特别是在间隔分区中很有用)。例如:
select * from test_data partition for (to_date('20180627','YYYYMMDD'));