ITPub博客

首页 > 数据库 > Oracle > [20220105]建立非唯一主键对性能有影响吗.txt

[20220105]建立非唯一主键对性能有影响吗.txt

原创 Oracle 作者:lfree 时间:2022-01-05 08:48:10 0 删除 编辑

[20220105]建立非唯一主键对性能有影响吗.txt

--//昨天优化项目,发现一个主键的索引非唯一,估计开始存在主键冲突,索引选择非唯一,解决后没有改正过来。
--//测试看看这样情况逻辑读是否存在变化。

1.环境:
SCOTT@book> @ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

2.测试
create table deptx as select * from dept;
create index pk_deptx on deptx (deptno);

SCOTT@book> select uniqueness from user_indexes where index_name = 'PK_DEPTX';
UNIQUENES
---------
NONUNIQUE

alter table deptx add constraint pk_deptx primary key (deptno) using index pk_deptx;

SCOTT@book> @gts deptx
Gather Table Statistics for table deptx...
PL/SQL procedure successfully completed.

--//这样建立非唯一索引作为主键。

3.测试性能:
SCOTT@book> @ sl all
alter session set statistics_level = all;
Session altered.

SCOTT@book> select * from deptx where deptno=10;
    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK

SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  cpn6gafrn12h6, child number 0
-------------------------------------
select * from deptx where deptno=10
Plan hash value: 296087899
----------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
----------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |      1 |        |       |     1 (100)|          |      1 |00:00:00.01 |       3 |
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPTX    |      1 |      1 |    20 |     1   (0)| 00:00:01 |      1 |00:00:00.01 |       3 |
|*  2 |   INDEX RANGE SCAN          | PK_DEPTX |      1 |      1 |       |     0   (0)|          |      1 |00:00:00.01 |       2 |
----------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / DEPTX@SEL$1
   2 - SEL$1 / DEPTX@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("DEPTNO"=10)

SCOTT@book> select * from dept where deptno=10;
    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK

SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4xamnunv51w9j, child number 0
-------------------------------------
select * from dept where deptno=10
Plan hash value: 2852011669
---------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name    | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
---------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |         |      1 |        |       |     1 (100)|          |      1 |00:00:00.01 |       2 |
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |      1 |    20 |     1   (0)| 00:00:01 |      1 |00:00:00.01 |       2 |
|*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |      1 |      1 |       |     0   (0)|          |      1 |00:00:00.01 |       1 |
---------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / DEPT@SEL$1
   2 - SEL$1 / DEPT@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("DEPTNO"=10)

--//可以看出逻辑读不同,前者是INDEX RANGE SCAN,后者是INDEX UNIQUE SCAN,逻辑读相差1个。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2850686/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7970383