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
468 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL Select unique values from two columns 4 79
use lov values 2 71
Help on model clause 5 47
update statement in oracle 9 37
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
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

713 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