Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I index primary key column(s) in Oracle

Tags:

I've recently stopped to think that Primary Keys are not indexes, they're a combination of Unique and Null constraints. And till now, I've never created index for PK columns. My question is if I should create index for PK columns if this column is going to be used in the WHERE part of many queries.

like image 243
Mikayil Abdullayev Avatar asked Mar 29 '13 12:03

Mikayil Abdullayev


People also ask

Do we need index on primary key Oracle?

There is no such thing as a "primary key index". A primary key under the covers will use either a UNIQUE or NON-UNIQUE index. If the primary key is deferrable it'll use a non-unique index. If the column(s) are already indexed with a non-unique index, the primary key constraint will rely on that index.

Should you index a primary key?

But in the database world, it's actually not necessary to create an index on the primary key column — the primary index can be created on any non primary key column as well.

Which column should be indexed in Oracle?

Columns with one or more of the following characteristics are good candidates for indexing: Values are unique in the column, or there are few duplicates. There is a wide range of values (good for regular indexes). There is a small range of values (good for bitmap indexes).

Can we index a primary key column?

We can apply a Primary Key constraint and a Clustered Index constraint to different columns in the same table or to the same column. It's a common practice to apply a Clustered Index to a Primary Key. Since the Primary Key is often used to connect data, it's frequently used in searches.


1 Answers

Oracle will create an index for you, or can use an existing one. Whether a unique or non-unique index is used is up to you.

http://docs.oracle.com/cd/B28359_01/server.111/b28310/indexes003.htm#i1006566

A primary key itself is not an index, and nor is a unique constraint -- they are both constraints. However an index is used to support them.

A unique index is rather different as it can exist in the absence of a unique or primary key constraint, and neither constraint type require that the index supporting it be unique.

like image 74
David Aldridge Avatar answered Oct 08 '22 22:10

David Aldridge