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
2
471 Views
Last Modified: 2013-10-29
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.

Please advise.


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
Comment
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
  • Learn & ask questions
2 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
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

by:
johnsone earned 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
capture vmstat info and insert it into an oracle table 31 93
Error in creating a view. 8 52
Updating a temp table inside a PL/SQL block 3 65
Sorting a SQL script 5 42
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to recover a database from a user managed backup

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question