您的位置:首页 > 其它

关于分区索引与全局索引性能比较的示例

2012-12-31 13:47 225 查看
说明:之前使用range分区做出来的效果不明显,这次使用hash分区。

1、准备工作:

----创建两张一样的hash分区表,jacks_part和echos_part------------------
1 SQL> create table jacks_part (owner varchar2(30),object_id number,object_name varchar2(128))
2     partition by hash(object_id)
3  partitions 30;

Table created.

SQL> create table echos_part (owner varchar2(30),object_id number,object_name varchar2(128))
2     partition by hash(object_id)
3  partitions 30;

Table created.

----分别向两张表插入一些记录-----------------
13 SQL> insert into jacks_part select owner,object_id,object_name from dba_objects;

72196 rows created.

SQL> insert into echos_part select owner,object_id,object_name from jacks_part;

72196 rows created.

SQL> commit;

Commit complete.

----分别创建global索引和local索引---------------
25 SQL> create index globals_ind on jacks_part(object_id)
2   global partition by hash(object_id);

Index created.

SQL> create index locals_ind on echos_part(object_id) local;

Index created.

----查询索引是否正确--------------------------
34 SQL> select index_name,table_name,locality from user_part_indexes;

INDEX_NAME                 TABLE_NAME              LOCALI
------------------ ------------------------------ ------
LOCALS_IND                ECHOS_PART                LOCAL
GLOBALS_IND               JACKS_PART               GLOBAL


2、分区索引性能优于全局索引的例子:

SQL> set linesize 200;
SQL> set autotrace traceonly;
SQL> select /*+ index(echos_part,locals_ind) */ * from  echos_part where object_id>100;

72097 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 3092815211

-----------------------------------------------------------------------------------------------------------------
| Id  | Operation               | Name    | Rows    | Bytes | Cost (%CPU)| Time    | Pstart| Pstop |
-----------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |        |  4228 |   396K|    89   (0)| 00:00:02 |    |    |
|   1 |  PARTITION HASH ALL           |        |  4228 |   396K|    89   (0)| 00:00:02 |     1 |    30 |
|   2 |   TABLE ACCESS BY LOCAL INDEX ROWID| ECHOS_PART |  4228 |   396K|    89   (0)| 00:00:02 |     1 |    30 |
|*  3 |    INDEX RANGE SCAN           | LOCALS_IND |  4228 |    |    25   (0)| 00:00:01 |     1 |    30 |
-----------------------------------------------------------------------------------------------------------------

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

3 - access("OBJECT_ID">100)

Note
-----
- dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
0    recursive calls
0    db block gets
10562   consistent gets
0    physical reads
0    redo size
3128267  bytes sent via SQL*Net to client
53285   bytes received via SQL*Net from client
4808    SQL*Net roundtrips to/from client
0    sorts (memory)
0    sorts (disk)
72097   rows processed

SQL> select /*+ index(jacks_part,globals_ind) */ * from  jacks_part where object_id>100;

72097 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 2501448352

-------------------------------------------------------------------------------------------------------------------
| Id  | Operation                | Name      | Rows  | Bytes | Cost (%CPU)| Time      | Pstart| Pstop |
-------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |  2500 |   234K|  4639   (1)| 00:00:56 |      |      |
|   1 |  PARTITION HASH SINGLE            |          |  2500 |   234K|  4639   (1)| 00:00:56 |    1 |    1 |
|   2 |   TABLE ACCESS BY GLOBAL INDEX ROWID| JACKS_PART  |  2500 |   234K|  4639   (1)| 00:00:56 | ROWID | ROWID |
|*  3 |    INDEX RANGE SCAN            | GLOBALS_IND |  2500 |      |    15   (0)| 00:00:01 |    1 |    1 |
-------------------------------------------------------------------------------------------------------------------

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

3 - access("OBJECT_ID">100)

Note
-----
- dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
0    recursive calls
0    db block gets
74718   consistent gets
0    physical reads
0    redo size
3077218  bytes sent via SQL*Net to client
53285   bytes received via SQL*Net from client
4808   SQL*Net roundtrips to/from client
0    sorts (memory)
0    sorts (disk)
72097   rows processed


3、分区索引性能低于全局索引的例子1:

SQL> select /*+ index(echos_part,locals_ind) */ count(*) from  echos_part where object_id>100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2317569636

--------------------------------------------------------------------------------------------------
| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |         |     1 |    13 |    25   (0)| 00:00:01 |     |     |
|   1 |  SORT AGGREGATE     |         |     1 |    13 |          |      |     |     |
|   2 |   PARTITION HASH ALL|         |  4228 | 54964 |    25   (0)| 00:00:01 |     1 |    30 |
|*  3 |    INDEX RANGE SCAN | LOCALS_IND |  4228 | 54964 |    25   (0)| 00:00:01 |     1 |    30 |
--------------------------------------------------------------------------------------------------

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

3 - access("OBJECT_ID">100)

Note
-----
- dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
0  recursive calls
0  db block gets
205  consistent gets
0  physical reads
0  redo size
424  bytes sent via SQL*Net to client
419  bytes received via SQL*Net from client
2  SQL*Net roundtrips to/from client
0  sorts (memory)
0  sorts (disk)
1  rows processed

SQL> select /*+ index(jacks_part,globals_ind) */ count(*) from  jacks_part where object_id>100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2478129137

------------------------------------------------------------------------------------------------------
| Id  | Operation           | Name         | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |         |       1 |      13 |      15   (0)| 00:00:01 |         |         |
|   1 |  SORT AGGREGATE        |         |       1 |      13 |          |         |         |         |
|   2 |   PARTITION HASH SINGLE|         |    2500 | 32500 |      15   (0)| 00:00:01 |       1 |       1 |
|*  3 |    INDEX RANGE SCAN    | GLOBALS_IND |    2500 | 32500 |      15   (0)| 00:00:01 |       1 |       1 |
------------------------------------------------------------------------------------------------------

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

3 - access("OBJECT_ID">100)

Note
-----
- dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
0  recursive calls
0  db block gets
201  consistent gets
0  physical reads
0  redo size
424  bytes sent via SQL*Net to client
419  bytes received via SQL*Net from client
2  SQL*Net roundtrips to/from client
0  sorts (memory)
0  sorts (disk)
1  rows processed


分区索引性能低于全局索引的例子2:

SQL> drop index globals_ind;

Index dropped.

SQL> create index global_indexs on jacks_part(object_id) global;

Index created.

SQL> select /*+ index(echos_part,locals_ind) */ count(*) from  echos_part where object_id>100;

Execution Plan
----------------------------------------------------------
Plan hash value: 2317569636

--------------------------------------------------------------------------------------------------
| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |         |     1 |     5 |   175   (0)| 00:00:03 |     |     |
|   1 |  SORT AGGREGATE     |         |     1 |     5 |          |      |     |     |
|   2 |   PARTITION HASH ALL|         | 72101 |   352K|   175   (0)| 00:00:03 |     1 |    30 |
|*  3 |    INDEX RANGE SCAN | LOCALS_IND | 72101 |   352K|   175   (0)| 00:00:03 |     1 |    30 |
--------------------------------------------------------------------------------------------------

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

3 - access("OBJECT_ID">100)

Statistics
----------------------------------------------------------
1704  recursive calls
0  db block gets
437  consistent gets
206  physical reads
0  redo size

SQL> select /*+ index(jacks_part,global_indexs) */ count(*) from  jacks_part where object_id>100;

Execution Plan
----------------------------------------------------------
Plan hash value: 1016566238

-----------------------------------------------------------------------------------
| Id  | Operation      | Name      | Rows  | Bytes | Cost (%CPU)| Time      |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |          |    1 |    5 |   201   (0)| 00:00:03 |
|   1 |  SORT AGGREGATE   |          |    1 |    5 |           |      |
|*  2 |   INDEX RANGE SCAN| GLOBAL_INDEXS | 72101 |   352K|   201   (0)| 00:00:03 |
-----------------------------------------------------------------------------------

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

2 - access("OBJECT_ID">100)

Statistics
----------------------------------------------------------
1  recursive calls
0  db block gets
201  consistent gets
200  physical reads
0  redo size
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: