Oracle:单个多列索引或两个单列索引

use*_*874 7 sql indexing performance oracle11g

我有桌子

create table1(
  column1 number(10,
  column2 number(10),
  column3 number(10)
);
Run Code Online (Sandbox Code Playgroud)

column1是主键 column2,column3是外键

我在2列上创建了唯一约束

alter table table1 
        add constraint table1_contr1 unique(column1,column2) 
      using index tablespace tbs1;
Run Code Online (Sandbox Code Playgroud)

当我在两个列上创建索引时

create index table1_idx1 on table1(column1,coulmn2);

ERROR at line 1:
ORA-01408: such column list already indexed
Run Code Online (Sandbox Code Playgroud)

因此Oracle在创建唯一约束时已经创建了索引.但如果我单独创建索引,它就会接受这些索引

create index table1_idx1 on table1(column1);
create index table2_idx2 on table2(column2);
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,在对两列都有唯一约束后,我还需要担心在每列上创建索引吗?在访问对象时,每列上没有索引会对性能产生影响吗?

这是在oracle 11R2上.

Jus*_*ave 9

这取决于...

column1如果你已经有一个复合索引,那么一个索引就不太可能是有益的column1, column2.由于column1是前导索引,因此针对仅column1作为谓词的表的查询将能够使用复合索引.如果您经常运行需要对索引进行全面扫描的查询,并且存在column2大大增加索引的大小,那么索引就可能column1更有效,因为完整索引扫描需要做的更少I/O.但这是一个非常不寻常的情况.

column2如果针对该表的某些查询仅指定谓词,则对just 的索引可能是有益的column2.如果相对较少的不同值column1,Oracle可能会使用复合索引执行索引跳过扫描,以满足仅指定column2为谓词的查询.但跳过扫描的效率可能远远低于范围扫描,因此有可能索引column2就会使这些查询受益.如果存在大量不同的值column1,则跳过扫描的效率会更低,并且索引column2就会更有益.当然,如果您从未在column2不指定谓词的情况下使用查询表,则column1不需要仅使用索引column2.