普通单表上建立分区索引测试
创建语句如下:
--range类型--
create index idx_emp3_empno on emp3(empno) global
partition by range(empno)
(partition p1 values less than (25),
partition p2 values less than (50),
partition p3 values less than (75),
partition p4 values less than (maxvalue));
--hash类型--
create index idx_emp3_mgr on emp3(mgr) global
partition by hash(mgr)
partitions 4;
下面是测试记录
SQL> select * from emp3;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1 2 18-JUN-15
2 3 18-JUN-15
3 4 18-JUN-15
4 5 18-JUN-15
5 6 18-JUN-15
6 7 18-JUN-15
7 8 18-JUN-15
8 9 18-JUN-15
9 10 18-JUN-15
10 11 18-JUN-15
10 rows selected.
SQL> col OBJECT_NAME for a28
SQL> select OBJECT_NAME,SUBOBJECT_NAME,OBJECT_TYPE from user_objects where OBJECT_NAME='EMP3';
OBJECT_NAME SUBOBJECT_NAME OBJECT_TYPE
---------------------------- ------------------------------ -------------------
EMP3 TABLE
SQL>
SQL> desc emp3
Name Null? Type
---------- -------- ------------
EMPNO NUMBER(10)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(10)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL>
SQL> create index idx_emp3_empno on emp3(empno) global
2 partition by range(empno)
3 (partition p1 values less than (25),
4 partition p2 values less than (50),
5 partition p3 values less than (75),
6 partition p4 values less than (maxvalue));
Index created.
SQL>
SQL> create index idx_emp3_mgr on emp3(mgr) global
2 partition by hash(mgr)
3 partitions 4;
Index created.
SQL> col OBJECT_NAME for a28
SQL> select OBJECT_NAME,SUBOBJECT_NAME,OBJECT_TYPE from user_objects where OBJECT_NAME like '%EMP3%' order by 2,3;
OBJECT_NAME SUBOBJECT_NAME OBJECT_TYPE
---------------------------- ------------------------------ -------------------
IDX_EMP3_EMPNO P1 INDEX PARTITION
IDX_EMP3_EMPNO P2 INDEX PARTITION
IDX_EMP3_EMPNO P3 INDEX PARTITION
IDX_EMP3_EMPNO P4 INDEX PARTITION
IDX_EMP3_MGR SYS_P41 INDEX PARTITION
IDX_EMP3_MGR SYS_P42 INDEX PARTITION
IDX_EMP3_MGR SYS_P43 INDEX PARTITION
IDX_EMP3_MGR SYS_P44 INDEX PARTITION
IDX_EMP3_MGR INDEX
IDX_EMP3_EMPNO INDEX
EMP3 TABLE
最近查阅资料,了解到单表也可以创建分区索引。
于是做了如下测试。
emp3是一张未分区的单表,在该表上新建了两个分区索引,一个是range的分区索引,一个是hash的分区索引。创建语句如下:
--range类型--
create index idx_emp3_empno on emp3(empno) global
partition by range(empno)
(partition p1 values less than (25),
partition p2 values less than (50),
partition p3 values less than (75),
partition p4 values less than (maxvalue));
--hash类型--
create index idx_emp3_mgr on emp3(mgr) global
partition by hash(mgr)
partitions 4;
--说明,分区表创建分区索引很常见,这里没有做测试。
=======================================================================================下面是测试记录
SQL> select * from emp3;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1 2 18-JUN-15
2 3 18-JUN-15
3 4 18-JUN-15
4 5 18-JUN-15
5 6 18-JUN-15
6 7 18-JUN-15
7 8 18-JUN-15
8 9 18-JUN-15
9 10 18-JUN-15
10 11 18-JUN-15
10 rows selected.
SQL> col OBJECT_NAME for a28
SQL> select OBJECT_NAME,SUBOBJECT_NAME,OBJECT_TYPE from user_objects where OBJECT_NAME='EMP3';
OBJECT_NAME SUBOBJECT_NAME OBJECT_TYPE
---------------------------- ------------------------------ -------------------
EMP3 TABLE
SQL>
SQL> desc emp3
Name Null? Type
---------- -------- ------------
EMPNO NUMBER(10)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(10)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL>
SQL> create index idx_emp3_empno on emp3(empno) global
2 partition by range(empno)
3 (partition p1 values less than (25),
4 partition p2 values less than (50),
5 partition p3 values less than (75),
6 partition p4 values less than (maxvalue));
Index created.
SQL>
SQL> create index idx_emp3_mgr on emp3(mgr) global
2 partition by hash(mgr)
3 partitions 4;
Index created.
SQL> col OBJECT_NAME for a28
SQL> select OBJECT_NAME,SUBOBJECT_NAME,OBJECT_TYPE from user_objects where OBJECT_NAME like '%EMP3%' order by 2,3;
OBJECT_NAME SUBOBJECT_NAME OBJECT_TYPE
---------------------------- ------------------------------ -------------------
IDX_EMP3_EMPNO P1 INDEX PARTITION
IDX_EMP3_EMPNO P2 INDEX PARTITION
IDX_EMP3_EMPNO P3 INDEX PARTITION
IDX_EMP3_EMPNO P4 INDEX PARTITION
IDX_EMP3_MGR SYS_P41 INDEX PARTITION
IDX_EMP3_MGR SYS_P42 INDEX PARTITION
IDX_EMP3_MGR SYS_P43 INDEX PARTITION
IDX_EMP3_MGR SYS_P44 INDEX PARTITION
IDX_EMP3_MGR INDEX
IDX_EMP3_EMPNO INDEX
EMP3 TABLE
11 rows selected.
从上面红色和绿色部分的对象类型看,对象类型为INDEX PARTITION,可以知道创建的索引是分区索引。
于是可以得出结论,不论是普通单表还是分区表,都可以创建分区索引。