473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Should I create an index when there's already a composite index?

I have a table where fields 1 and 2 make up the primary key.
Because of this, I have a unique composite index on fields 1 and 2 (as
required by DB2). Now my question is: Fields 1 and 2 are also
separate foreign keys to two other tables. I have read that it is
always a good idea to create indexes on foreign keys. Should I create
single indexes on each of these fields? Or is that not necessary since
they are already part of a composite index?
Actually the same question applies (forgetting about the foreign key
stuff) if I decide that field 2 is very frequently going to be part of
the WHERE clause in my query. Normally I would make field 2 an index
because of this. Should I still do that, even though field 2 is
already part of the composite index?

May 15 '06 #1
2 5076

"bobby_b_" <bo******@hotmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
I have a table where fields 1 and 2 make up the primary key.
Because of this, I have a unique composite index on fields 1 and 2 (as
required by DB2). Now my question is: Fields 1 and 2 are also
separate foreign keys to two other tables. I have read that it is
always a good idea to create indexes on foreign keys. Should I create
single indexes on each of these fields? Or is that not necessary since
they are already part of a composite index?
Actually the same question applies (forgetting about the foreign key
stuff) if I decide that field 2 is very frequently going to be part of
the WHERE clause in my query. Normally I would make field 2 an index
because of this. Should I still do that, even though field 2 is
already part of the composite index?


Assuming that field 1 is to the left of field 2 in the index, i.e. the
definition is

Create index my_index on my_table(field1, field2)

not

Create index my_index on my_table(field2, field1)

you don't need a separate foreign key index on field1 because DB2 can use
the primary key index in its place. The performance should normally be the
same.

However, you will still want the separate foreign key index on field2.

--
Rhino
May 15 '06 #2
"bobby_b_" <bo******@hotmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
I have a table where fields 1 and 2 make up the primary key.
Because of this, I have a unique composite index on fields 1 and 2 (as
required by DB2). Now my question is: Fields 1 and 2 are also
separate foreign keys to two other tables. I have read that it is
always a good idea to create indexes on foreign keys. Should I create
single indexes on each of these fields? Or is that not necessary since
they are already part of a composite index?
Actually the same question applies (forgetting about the foreign key
stuff) if I decide that field 2 is very frequently going to be part of
the WHERE clause in my query. Normally I would make field 2 an index
because of this. Should I still do that, even though field 2 is
already part of the composite index?


The term "using an index" is a loaded term, because there are two ways for
DB2 to use an index. The first is via the B-Tree, which is the fastest
access, and is what most people expect when they think of using an index.

The other way is a complete index scan, in which DB2 reads the entire index
from top to bottom, ignoring the non-leaf pages (which are the "table of
contents" of the index). A complete index scan is faster than a table scan
(usually) but usually not nearly as fast as using the B-Tree.

In order to use the B-Tree of an index, the predicate of the SQL statement
must supply at least one of the leftmost columns of the columns of the
index. The WHERE clause does not have to supply all of the columns in index
for DB2 to use the B-tree of the index. So if your WHERE clause says:

WHERE Col2 = ?

and your index is on (Col1, Col2), then DB2 cannot use the B-Tree and will
either do a complete index scan or a table scan (depending on which it
thinks is faster to complete the processing of the SQL statement).

Therefore, you would probably want a separate index just on Col2 (plus the
unique index for the primary key on Col1, Col2) if you used the above
predicate with just Col2 fairly often.

However, your assumption that all Foreign keys need indexes is incorrect,
but that is another subject. You might try the Google Groups archives for
previous posts on this subject.
May 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ken | last post by:
Dear all, I have a beginner of oracle and recently I have a prob with using indexes. I have a table with 5 columns, with no primary key set in the table. An index has created for column 1. I...
0
by: Mike Coppinger | last post by:
I have a problem that has raised a couple of questions. SITUATION: I have a table called pr_persona that has a composite key comprising pr_persona_db CHAR(2) pr_persona_id INT(11) auto...
2
by: Miss Livvy | last post by:
Would it be OK to use varchar(5) instead of char(5) as the first field of a composite clustered index? My gut tells me that varchar would be a bad idea, but I am not finding much information on...
10
by: deko | last post by:
I understand it's possible to make a composite Primary Key by holding down the control key and selecting multiple fields, then right-clicking and selecting Primary Key. But I've heard that's not a...
12
by: Phill | last post by:
I'd like to have textboxes that only allow certain types of data. Like only integers, decimals, etc. Is it better to subclass TextBox for each type or to create only one new TextBox class that...
2
by: Peter Lindquist | last post by:
I had a fun issue this morning, and now I'm wondering if I violated a rule I didn't know about. Any insight would be much appreciated. All operations involving this particular table may be assumed...
13
by: Baihao Yuan | last post by:
Hi, I created a composite index (lastname, firstname). I know the following queries will use this index: WHERE lastname = ... WHERE lastname = ... AND firstname = ... Also this won't use...
6
by: Alvin SIU | last post by:
Hi all, I have a table in Db2 v8 like this: Team Name Role ------ -------- --------------------- A Superman Leader A Batman Member A WonderWoman Member B ...
6
by: Henry J. | last post by:
I have a composite index on two columns in a table. However, the index is not used in a query that restricts the 2nd column to a constant. If both columns are linked with columns in other join...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.