Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# What's the best way to create indexes (single/composite) on tables so that query will run much faster?

Posted on 2013-10-25
Medium Priority
481 Views
Let's say in below two tables,

1) table_1 has unique fields col_a, col_b, col_c and
2) table_2 has unique fields col_a, col_b,col_c, col_d

col_a, col_b, and col_c are same fields on both tables.

I will create a primary key constraint on both tables (table_1 and table_2). I will create indexes on both table for performance reason as table_1 will be joined with table_2 on col_a, col_b, col_c

table_2 has huge number of records 400 plus millions, table_1 has less than 8 millions of records.

Questions:

1) To create a primary key constraint on both tables, should I do,

create unique index idx_table_1_01 on table_1 (col_a, col_b, col_c)
then do
alter table table_1 add constraint pk_table_1 primary key (col_a, col_b, col_c)

or just run alter table statement,

alter table table_1 add constraint pk_table_1 primary key (col_a, col_b, col_c)

create unique index idx_table_2_01 on table_2 (col_a, col_b, col_c, col_d)
then do
alter table table_2 add constraint pk_table_2 primary key (col_a, col_b, col_c, col_d)

or just run alter table statement,

alter table table_2 add constraint pk_table_2 primary key (col_a, col_b, col_c, col_d)

2) Since I am creating unique indexes on above two tables (table_1 and table_2). Do I still need to create normal indexes on same fields on these tables as well. For example,

create index idx_table_1_02  on table_1 (col_a, col_b, col_c)
create index idx_table_2_02 on table_2 (col_a, col_b, col_c, col_d)

or there is nop need since unique indexes on same fields exist on both tables.

3) Since I will be joining table_1 with table_2, to increase performances should I create a normal index on just a single field on both tables. For example,

create index idx_table_1_03 on table_1 (col_a)
create index idx_table_2_03 on table_2 (col_a)

or should I create composite indexes to on both tables so that query will run much faster. For example,

create index idx_table_1_03 on table_1 (col_a, col_b, col_c)
create index idx_table_2_03 on table_2 (col_a, col_b, col_c)

Just a note, col_a, col_b, and col_c are same on both tables and all three columns will be used in the WHERE clause.

create table table_1
(
col_a   varchar2(10 byte),
col_b   varchar2(12 byte),
col_c   varchar2(50 byte),
col_d   varchar2(10 byte),
col_i    number,
col_n   date
)

create table table_2
(
col_a   varchar2(10 byte),
col_b   varchar2(12 byte),
col_c   varchar2(50 byte),
col_d   varchar2(10 byte),
col_e   varchar2(25 byte),
col_f    number,
col_g   number,
col_s   date
)
0
Question by:IT_ETL
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 77

Expert Comment

ID: 39601300
Try creating an index on the same columns as the primary key.  See what happens.

You index fields based on how they are used in the queries that access the tables.

If all three columns will be used in all queries in '=' joins, I see no need for additional indexes.

It's next to impossible to predict 'good' indexes without knowing the applications that will be using the data.

I would start with just the PK's and possibly indexes on any foreign keys you have.

Then monitor after the app has performed some processing and add indexes on poor performing queries.
0

LVL 35

Accepted Solution

johnsone earned 2000 total points
ID: 39601620
To address your questions 1 & 2 specifically, there is no need to create a unique index on the primary key fields.  Just create a regular index and then the constraint like this:

create index idx_table_2_01 on table_2 (col_a, col_b, col_c, col_d)
alter table table_2 add constraint pk_table_2 primary key (col_a, col_b, col_c, col_d);

The constraint will ensure the uniqueness, having a unique index is redundant.

The advantage to doing it this way is that you can disable the primary key constraint (for maintenance if needed), and the index will not be dropped.  This way to enable the constraint again, it only has to scan the existing index, not rebuild the index from scratch.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
###### Suggested Courses
Course of the Month5 days, 11 hours left to enroll