分区索引Oracle

时间:2017-01-31 09:40:56

标签: sql oracle indexing

我可以在列上使用TRUNC创建全局分区索引吗? Smth喜欢:

    CREATE INDEX "SCHEMA"."IND_DATA_TEST" ON "SCHEMA"."TEST_TABLE" (TRUNC("DATA"), "TEST_COLUMN") 
  GLOBAL PARTITION BY RANGE (TRUNC("DATA"))
 (PARTITION "PRT_06_06"  VALUES LESS THAN (TO_DATE(' 2006-06-01', 'SYYYY-MM-DD', 'NLS_CALENDAR=GREGORIAN')) TABLESPACE "TEST_A_IND" , 
  PARTITION "PRT_06_07"  VALUES LESS THAN (TO_DATE(' 2006-07-01', 'SYYYY-MM-DD', 'NLS_CALENDAR=GREGORIAN')) TABLESPACE "TEST_A_IND" , 
 PARTITION "PRT_OTH"  VALUES LESS THAN (MAXVALUE) TABLESPACE "TEST_A_IND" ) 
  PARALLEL 8 ;

1 个答案:

答案 0 :(得分:0)

您可以定义虚拟列代替" trunc(数据)"。这是可能的,因为oracle支持

  • 虚拟列上的索引
  • 虚拟列上的分区

但请查看您的Oracle版本,因为您的版本可能不支持其中一个或两个功能。

按以下方式编写代码:

drop table xxx_demo;

create table xxx_demo (
  date_field date,
  sample_field number,
  truncated_field as (trunc(date_field)) --this is the virtual column
)
;

create index partitioned_index
on xxx_demo(truncated_field, sample_field)
GLOBAL
partition by range(truncated_field)(
PARTITION "PRT_06_06"  VALUES LESS THAN (TO_DATE('2006-06-01', 'YYYY-MM-DD')), 
PARTITION "PRT_OTH"  VALUES LESS THAN (MAXVALUE) 
)
;

通过正确的查询

访问表时,您可以看到实际使用了索引
insert into xxx_demo(date_field, sample_field) values(sysdate, 1);
insert into xxx_demo(date_field, sample_field) values(sysdate-365*20, 2);

explain plan for
select * from xxx_demo where truncated_field < sysdate-365;

select * from table(dbms_xplan.display);


Plan hash value: 2789338220

------------------------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |     1 |    31 |     0   (0)| 00:00:01 |       |       |
|   1 |  PARTITION RANGE ITERATOR    |                   |     1 |    31 |     0   (0)| 00:00:01 |     1 |   KEY |
|   2 |   TABLE ACCESS BY INDEX ROWID| XXX_DEMO          |     1 |    31 |     0   (0)| 00:00:01 |       |       |
|*  3 |    INDEX RANGE SCAN          | PARTITIONED_INDEX |     1 |       |     0   (0)| 00:00:01 |     1 |   KEY |
------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("TRUNCATED_FIELD"<SYSDATE@!-365)

Note
-----
   - dynamic sampling used for this statement (level=2)
   - automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold