Oracle技术之和分区表相关的一点总结(四)

3.访问全部数据,我们发现访问heap表要比访问分区表是少了一些逻辑读:

88429<88573,其实这点差别对性能来说是无关紧要的,重要的是说明了一个问题,

那就是尽量让执行的sql少读、少些,这也是sql调整的最终目的,相差的这100

多逻辑读主要发生在查找metadata上...

SQL> select * from t1 ;

已选择1260672行。


已用时间: 00: 00: 26.70


执行计划

----------------------------------------------------------

Plan hash value: 3617692013


--------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

--------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1482K| 111M| 1064 (3)| 00:00:13 |

| 1 | TABLE ACCESS FULL| T1 | 1482K| 111M| 1064 (3)| 00:00:13 |

--------------------------------------------------------------------------


Note

-----

- dynamic sampling used for this statement



统计信息

----------------------------------------------------------

5 recursive calls

0 db block gets

88511 consistent gets

0 physical reads

0 redo size

44940044 bytes sent via SQL*Net to client

924869 bytes received via SQL*Net from client

84046 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1260672 rows processed


SQL> select * from t1 ;


已选择1260672行。


已用时间: 00: 00: 24.59


执行计划

----------------------------------------------------------

Plan hash value: 3617692013


--------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

--------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1482K| 111M| 1064 (3)| 00:00:13 |

| 1 | TABLE ACCESS FULL| T1 | 1482K| 111M| 1064 (3)| 00:00:13 |

--------------------------------------------------------------------------


Note

-----

- dynamic sampling used for this statement



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

88429 consistent gets

0 physical reads

0 redo size

44940044 bytes sent via SQL*Net to client

924869 bytes received via SQL*Net from client

84046 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1260672 rows processed


SQL> select * from t ;


已选择1260544行。


已用时间: 00: 00: 25.71


执行计划

----------------------------------------------------------

Plan hash value: 3557914527


--------------------------------------------------------------------------------


------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pst


art| Pstop |


--------------------------------------------------------------------------------


------------


| 0 | SELECT STATEMENT | | 1317K| 37M| 1094 (3)| 00:00:14 |

| |


| 1 | PARTITION RANGE ALL| | 1317K| 37M| 1094 (3)| 00:00:14 |

1 | 5 |


| 2 | TABLE ACCESS FULL | T | 1317K| 37M| 1094 (3)| 00:00:14 |

1 | 5 |


--------------------------------------------------------------------------------


------------



Note

-----

- dynamic sampling used for this statement



统计信息

----------------------------------------------------------

7 recursive calls

2 db block gets

90275 consistent gets

2133 physical reads

114820 redo size

39895221 bytes sent via SQL*Net to client

924781 bytes received via SQL*Net from client

84038 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1260544 rows processed


SQL> select * from t ;


已选择1260544行。


已用时间: 00: 00: 24.90


执行计划

----------------------------------------------------------

Plan hash value: 3557914527


--------------------------------------------------------------------------------


------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pst


art| Pstop |


--------------------------------------------------------------------------------


------------


| 0 | SELECT STATEMENT | | 1317K| 37M| 1094 (3)| 00:00:14 |

| |


| 1 | PARTITION RANGE ALL| | 1317K| 37M| 1094 (3)| 00:00:14 |

1 | 5 |


| 2 | TABLE ACCESS FULL | T | 1317K| 37M| 1094 (3)| 00:00:14 |

1 | 5 |


--------------------------------------------------------------------------------


------------



Note

-----

- dynamic sampling used for this statement



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

88573 consistent gets

0 physical reads

0 redo size

39895221 bytes sent via SQL*Net to client

924781 bytes received via SQL*Net from client

84038 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

1260544 rows processed


SQL>

--==========================

有index的情况,创建global index:

有index其实也是一样的,首先通过index找到rowid,之后最终通过rowid

再去访问表,如果访问的数据尽可能的可以集中在一些分区内,那么访问

分区表的性能肯定要优于heap表

SQL> create index idx_t on t(object_id) tablespace users;


索引已创建。


已用时间: 00: 00: 10.21

SQL> create index idx_t1 on t1(object_id) tablespace users;


索引已创建。


已用时间: 00: 00: 06.89

SQL>

SQL> exec dbms_stats.gather_table_stats('SYS','T',CASCADE=>TRUE);


PL/SQL 过程已成功完成。


已用时间: 00: 00: 06.00

SQL> exec dbms_stats.gather_table_stats('SYS','T1',CASCADE=>TRUE);


PL/SQL 过程已成功完成。


已用时间: 00: 00: 03.21

SQL>

--有global index跨分区访问

SQL> select /*+ index(t idx_t) */ * from t where object_id<3000;


已选择377984行。


已用时间: 00: 00: 07.75


执行计划

----------------------------------------------------------

Plan hash value: 3317799281


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 368K| 7922K| 363K (1)


| 01:12:41 | | |


| 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| T | 368K| 7922K| 363K (1)


| 01:12:41 | ROWID | ROWID |


|* 2 | INDEX RANGE SCAN | IDX_T | 368K| | 986 (1)


| 00:00:12 | | |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<3000)



统计信息

----------------------------------------------------------

1 recursive calls

0 db block gets

398707 consistent gets

0 physical reads

0 redo size

12757072 bytes sent via SQL*Net to client

277563 bytes received via SQL*Net from client

25200 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

377984 rows processed


SQL> select /*+ index(t idx_t) */ * from t where object_id<3000;


已选择377984行。


已用时间: 00: 00: 09.43


执行计划

----------------------------------------------------------

Plan hash value: 3317799281


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 368K| 7922K| 363K (1)


| 01:12:41 | | |


| 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| T | 368K| 7922K| 363K (1)


| 01:12:41 | ROWID | ROWID |


|* 2 | INDEX RANGE SCAN | IDX_T | 368K| | 986 (1)


| 00:00:12 | | |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<3000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

398707 consistent gets

0 physical reads

0 redo size

12757072 bytes sent via SQL*Net to client

277563 bytes received via SQL*Net from client

25200 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

377984 rows processed


SQL>

--======================

--有global index不垮分区访问:

SQL> select /*+ index(t1 idx_t1) */ * from t1 where object_id<2000;


已选择249984行。


已用时间: 00: 00: 05.57


执行计划

----------------------------------------------------------

Plan hash value: 50753647


--------------------------------------------------------------------------------


------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|


--------------------------------------------------------------------------------


------


| 0 | SELECT STATEMENT | | 245K| 5264K| 245K (1)| 00:4


9:12 |


| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 245K| 5264K| 245K (1)| 00:4


9:12 |


|* 2 | INDEX RANGE SCAN | IDX_T1 | 245K| | 522 (2)| 00:0


0:07 |


--------------------------------------------------------------------------------


------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

1 recursive calls

0 db block gets

266985 consistent gets

0 physical reads

0 redo size

8439590 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL> select /*+ index(t1 idx_t1) */ * from t1 where object_id<2000;


已选择249984行。


已用时间: 00: 00: 05.42


执行计划

----------------------------------------------------------

Plan hash value: 50753647


--------------------------------------------------------------------------------


------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|


--------------------------------------------------------------------------------


------


| 0 | SELECT STATEMENT | | 245K| 5264K| 245K (1)| 00:4


9:12 |


| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 245K| 5264K| 245K (1)| 00:4


9:12 |


|* 2 | INDEX RANGE SCAN | IDX_T1 | 245K| | 522 (2)| 00:0


0:07 |


--------------------------------------------------------------------------------


------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

266985 consistent gets

0 physical reads

0 redo size

8439590 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL> select /*+ index(t idx_t) */ * from t where object_id<2000;


已选择249984行。


已用时间: 00: 00: 04.93


执行计划

----------------------------------------------------------

Plan hash value: 2063514567


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 259K| 5073K| 1241K (1)


| 04:08:20 | | |


| 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| T | 259K| 5073K| 1241K (1)


| 04:08:20 | 1 | 1 |


|* 2 | INDEX RANGE SCAN | IDX_T | 1260K| | 3366 (1)


| 00:00:41 | | |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

44 recursive calls

0 db block gets

262733 consistent gets

0 physical reads

0 redo size

8439590 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL>

SQL> select /*+ index(t idx_t) */ * from t where object_id<2000;


已选择249984行。


已用时间: 00: 00: 04.62


执行计划

----------------------------------------------------------

Plan hash value: 2063514567


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 259K| 5073K| 1241K (1)


| 04:08:20 | | |


| 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| T | 259K| 5073K| 1241K (1)


| 04:08:20 | 1 | 1 |


|* 2 | INDEX RANGE SCAN | IDX_T | 1260K| | 3366 (1)


| 00:00:41 | | |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

262727 consistent gets

0 physical reads

0 redo size

8439590 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL>

--======================

SQL> exec dbms_stats.gather_table_stats('SYS','T',CASCADE=>TRUE);


PL/SQL 过程已成功完成。


已用时间: 00: 00: 05.82

SQL> exec dbms_stats.gather_table_stats('SYS','T1',CASCADE=>TRUE);


PL/SQL 过程已成功完成。


已用时间: 00: 00: 03.17

SQL>

--local index:

SQL> select /*+ index(t1 idx_t1) */ * from t1 where object_id<3000;


已选择377984行。


已用时间: 00: 00: 09.46


执行计划

----------------------------------------------------------

Plan hash value: 50753647


--------------------------------------------------------------------------------


------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|


--------------------------------------------------------------------------------


------


| 0 | SELECT STATEMENT | | 370K| 7963K| 371K (1)| 01:1


4:16 |


| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 370K| 7963K| 371K (1)| 01:1


4:16 |


|* 2 | INDEX RANGE SCAN | IDX_T1 | 370K| | 786 (2)| 00:0


0:10 |


--------------------------------------------------------------------------------


------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<3000)



统计信息

----------------------------------------------------------

150 recursive calls

0 db block gets

403726 consistent gets

0 physical reads

0 redo size

12757072 bytes sent via SQL*Net to client

277563 bytes received via SQL*Net from client

25200 SQL*Net roundtrips to/from client

3 sorts (memory)

0 sorts (disk)

377984 rows processed


SQL> select /*+ index(t1 idx_t1) */ * from t1 where object_id<3000;


已选择377984行。


已用时间: 00: 00: 08.28


执行计划

----------------------------------------------------------

Plan hash value: 50753647


--------------------------------------------------------------------------------


------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|


--------------------------------------------------------------------------------


------


| 0 | SELECT STATEMENT | | 370K| 7963K| 371K (1)| 01:1


4:16 |


| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 370K| 7963K| 371K (1)| 01:1


4:16 |


|* 2 | INDEX RANGE SCAN | IDX_T1 | 370K| | 786 (2)| 00:0


0:10 |


--------------------------------------------------------------------------------


------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<3000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

403706 consistent gets

0 physical reads

0 redo size

12757072 bytes sent via SQL*Net to client

277563 bytes received via SQL*Net from client

25200 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

377984 rows processed


SQL> select /*+ index(t idx_t) */ * from t where object_id<3000;


已选择377984行。


已用时间: 00: 00: 07.17


执行计划

----------------------------------------------------------

Plan hash value: 3038871768


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 368K| 7566K| 363K (1)


| 01:12:40 | | |


| 1 | PARTITION RANGE ITERATOR | | 368K| 7566K| 363K (1)


| 01:12:40 | 1 | 2 |


| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| T | 368K| 7566K| 363K (1)


| 01:12:40 | 1 | 2 |


|* 3 | INDEX RANGE SCAN | IDX_T | 368K| | 783 (2)


| 00:00:10 | 1 | 2 |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


3 - access("OBJECT_ID"<3000)



统计信息

----------------------------------------------------------

195 recursive calls

0 db block gets

398680 consistent gets

0 physical reads

0 redo size

5145251 bytes sent via SQL*Net to client

277563 bytes received via SQL*Net from client

25200 SQL*Net roundtrips to/from client

6 sorts (memory)

0 sorts (disk)

377984 rows processed


SQL> select /*+ index(t idx_t) */ * from t where object_id<3000;


已选择377984行。


已用时间: 00: 00: 07.42


执行计划

----------------------------------------------------------

Plan hash value: 3038871768


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 368K| 7566K| 363K (1)


| 01:12:40 | | |


| 1 | PARTITION RANGE ITERATOR | | 368K| 7566K| 363K (1)


| 01:12:40 | 1 | 2 |


| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| T | 368K| 7566K| 363K (1)


| 01:12:40 | 1 | 2 |


|* 3 | INDEX RANGE SCAN | IDX_T | 368K| | 783 (2)


| 00:00:10 | 1 | 2 |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


3 - access("OBJECT_ID"<3000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

398633 consistent gets

0 physical reads

0 redo size

5145251 bytes sent via SQL*Net to client

277563 bytes received via SQL*Net from client

25200 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

377984 rows processed


SQL>

--===============================

有local index不垮分区访问:

SQL> select /*+ index(t idx_t) */ * from t where object_id<2000;


已选择249984行。


已用时间: 00: 00: 04.93


执行计划

----------------------------------------------------------

Plan hash value: 3965956311


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 254K| 4962K| 245K (1)


| 00:49:11 | | |


| 1 | PARTITION RANGE SINGLE | | 254K| 4962K| 245K (1)


| 00:49:11 | 1 | 1 |


| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| T | 254K| 4962K| 245K (1)


| 00:49:11 | 1 | 1 |


|* 3 | INDEX RANGE SCAN | IDX_T | 249K| | 528 (2)


| 00:00:07 | 1 | 1 |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


3 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

24 recursive calls

0 db block gets

262679 consistent gets

0 physical reads

0 redo size

3406055 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL> select /*+ index(t idx_t) */ * from t where object_id<2000;


已选择249984行。


已用时间: 00: 00: 03.48


执行计划

----------------------------------------------------------

Plan hash value: 3965956311


--------------------------------------------------------------------------------


----------------------------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)


| Time | Pstart| Pstop |


--------------------------------------------------------------------------------


----------------------------


| 0 | SELECT STATEMENT | | 254K| 4962K| 245K (1)


| 00:49:11 | | |


| 1 | PARTITION RANGE SINGLE | | 254K| 4962K| 245K (1)


| 00:49:11 | 1 | 1 |


| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| T | 254K| 4962K| 245K (1)


| 00:49:11 | 1 | 1 |


|* 3 | INDEX RANGE SCAN | IDX_T | 249K| | 528 (2)


| 00:00:07 | 1 | 1 |


--------------------------------------------------------------------------------


----------------------------



Predicate Information (identified by operation id):

---------------------------------------------------


3 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

262676 consistent gets

0 physical reads

0 redo size

3406055 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL> select /*+ index(t1 idx_t1) */ * from t1 where object_id<2000;


已选择249984行。


已用时间: 00: 00: 05.40


执行计划

----------------------------------------------------------

Plan hash value: 50753647


--------------------------------------------------------------------------------


------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|


--------------------------------------------------------------------------------


------


| 0 | SELECT STATEMENT | | 247K| 5306K| 247K (1)| 00:4


9:29 |


| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 247K| 5306K| 247K (1)| 00:4


9:29 |


|* 2 | INDEX RANGE SCAN | IDX_T1 | 247K| | 525 (2)| 00:0


0:07 |


--------------------------------------------------------------------------------


------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

266985 consistent gets

0 physical reads

0 redo size

8439590 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL> select /*+ index(t1 idx_t1) */ * from t1 where object_id<2000;


已选择249984行。


已用时间: 00: 00: 05.70


执行计划

----------------------------------------------------------

Plan hash value: 50753647


--------------------------------------------------------------------------------


------


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time


|


--------------------------------------------------------------------------------


------


| 0 | SELECT STATEMENT | | 247K| 5306K| 247K (1)| 00:4


9:29 |


| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 247K| 5306K| 247K (1)| 00:4


9:29 |


|* 2 | INDEX RANGE SCAN | IDX_T1 | 247K| | 525 (2)| 00:0


0:07 |


--------------------------------------------------------------------------------


------



Predicate Information (identified by operation id):

---------------------------------------------------


2 - access("OBJECT_ID"<2000)



统计信息

----------------------------------------------------------

0 recursive calls

0 db block gets

266985 consistent gets

0 physical reads

0 redo size

8439590 bytes sent via SQL*Net to client

183700 bytes received via SQL*Net from client

16667 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

249984 rows processed


SQL>

--=========================

SQL> create index idx_t_name on t(object_name) tablespace users;


索引已创建。


SQL>

--=========================

SQL> select index_name,status from dba_indexes where index_name='IDX_T_NAME';


INDEX_NAME STATUS

------------------------------ --------

IDX_T_NAME VALID


SQL> alter table t truncate partition p4;


表被截断。

--在分区表上创建的global index(注意非分区),即使我们truncate partition

其实这个global index是不会自动维护的。

SQL> select index_name,status from dba_indexes where index_name='IDX_T_NAME';


INDEX_NAME STATUS

------------------------------ --------

IDX_T_NAME UNUSABLE


SQL> COL OBJECT_NAME FORMAT A20

SQL> select object_name,status from dba_objects where object_name='IDX_T_NAME';


OBJECT_NAME STATUS

-------------------- -------

IDX_T_NAME VALID


SQL>

SQL> select bytes/1024/1024 m from dba_segments where segment_name='IDX_T_NAME';



M

----------

47


SQL> alter index idx_t_name rebuild;


索引已更改。


SQL> select bytes/1024/1024 m from dba_segments where segment_name='IDX_T_NAME';



M

----------

37


SQL> select index_name,status from dba_indexes where index_name='IDX_T_NAME';


INDEX_NAME STATUS

------------------------------ --------

IDX_T_NAME VALID


SQL>

--==========================

验证一下dml对global index的影响,dml操作oracle会自动update global index(注意:并非global partition index),这个

比较好理解,也应该这样做...

SQL> select name,del_lf_rows,del_lf_rows_len from index_stats;


NAME DEL_LF_ROWS DEL_LF_ROWS_LEN

------------------------------ ----------- ---------------

IDX_T_NAME 256 10112


SQL>

SQL> delete from t where object_id=5002;


已删除128行。


SQL> commit;


提交完成。


SQL> analyze index idx_t_name validate structure;


索引已分析


SQL> select name,del_lf_rows,del_lf_rows_len from index_stats;


NAME DEL_LF_ROWS DEL_LF_ROWS_LEN

------------------------------ ----------- ---------------

IDX_T_NAME 384 15616


SQL>

SQL> select index_name,status from dba_indexes where index_name='IDX_T_NAME';


INDEX_NAME STATUS

------------------------------ --------

IDX_T_NAME VALID

--=========================

验证在分区表上创建主键和唯一索引的情况:

SQL> truncate table t ;


表被截断。


SQL> alter table t add pk number;


表已更改。


SQL> insert into t(pk,object_id,object_name) select rownum,object_id,object_name

from t1;


已创建1260672行。


SQL> commit;


提交完成。


SQL> alter table t add constraint pk_t primary key (pk);


表已更改。


SQL> alter table t drop constraint pk_t;


表已更改。


SQL> create unique index idx_t_pk on t(pk) tablespace users;


索引已创建。

SQL> create unique index idx_t_pk on t(pk) local tablespace users;

create unique index idx_t_pk on t(pk) local tablespace users

*

第 1 行出现错误:

ORA-14039: 分区列必须构成 UNIQUE 索引的关键字列子集



SQL> create unique index idx_t_pk on t(pk,object_id) local tablespace users;


索引已创建。


SQL> drop index idx_t_pk;


索引已删除。

SQL> alter table t add constraint pk_t primary key (pk) using index local;

alter table t add constraint pk_t primary key (pk) using index local

*

第 1 行出现错误:

ORA-14039: 分区列必须构成 UNIQUE 索引的关键字列子集



SQL> alter table t add constraint pk_t primary key (pk,object_id) using index l

ocal;


表已更改。


SQL> alter table t drop constraint pk_t;


表已更改。


SQL> create index idx_t_pk on t(pk) local tablespace users;


索引已创建。


SQL> drop index idx_t_pk;


索引已删除。


SQL>

结论:不管我们在创建unique index还是添加primary key时,只要我们

想把index创建成local分区index,那么就应该至少包含分区列...而不是象

网上有些人说的在分区表上创建primary key或者nuique index时必须包含分区列。


oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html


你可能感兴趣的:(oracle,分区表)