If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Create index on joining tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-07-10, 12:36
jin007 jin007 is offline
Registered User
 
Join Date: Jan 2004
Location: Chicago
Posts: 54
Create index on joining tables

I need to select rows from 2 tables using SQL:

SELECT A.column1, A.column2, B.column1, B.column2 FROM table1 A join table2 B on A.column1 = B.column1 ORDER BY A.column1, B.column1

How should I create an index to make the selection faster?
Maybe I should create more than 1 indexes?

Thanks!

Bruce
Reply With Quote
  #2 (permalink)  
Old 05-07-10, 13:17
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
For that query, there should be an index on a.column1 and another on b.column1. You "order by" is redundant, remove the b.column1 since it is the same as a.column1.

Andy
Reply With Quote
  #3 (permalink)  
Old 05-07-10, 13:43
jin007 jin007 is offline
Registered User
 
Join Date: Jan 2004
Location: Chicago
Posts: 54
Thanks Andy.

Good point. Now suppose I change the ORDER BY to ORDER BY A.column1, B.column2

SELECT A.column1, A.column2, B.column1, B.column2 FROM table1 A join table2 B on A.column1 = B.column1 ORDER BY A.column1, B.column2

Then should I create 3 indexes?

1. on A.column1
2. on B.column1
3. on B.column2

Or should I create some sort of composite index to handle
"ORDER BY A.column1, B.column2"

Thanks.
Bruce
Reply With Quote
  #4 (permalink)  
Old 05-07-10, 13:47
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
No, you still only need 2 indexes. Just make the one on B column1,column2.

Andy
Reply With Quote
  #5 (permalink)  
Old 05-07-10, 14:04
jin007 jin007 is offline
Registered User
 
Join Date: Jan 2004
Location: Chicago
Posts: 54
Cool, Thanks Andy.

What would be the general rules of making indexes for query like this: (A, B, C are 3 tables)

SELECT ... FROM ... ORDER BY A.c1, B.c1, C.c1, A.c2, B.c2, C.c2

Thanks
Bruce
Reply With Quote
  #6 (permalink)  
Old 05-07-10, 16:37
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Adding indexes just to satisfy an order by clause may not be the best idea. You get the most bang for the buck with indexes when they cover foreign key relationships and the predicates in a where clause.

Andy
Reply With Quote
Reply

Tags
index, join table

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On