473,396 Members | 1,707 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.

Index question

I have a one to many relationship between two tables. The "one" table has a
5 field PK (total 7 fields, 1400 records) and the "many" table has a 7
field PK (total 15 fields, 1500 records). I followed the general rule of
indexing each field in the PKs in both tables. I was doing a query on both
tables with a "group by" to get the sum of a field in the "many" table. The
query was taking about 5 seconds to run which I thought was horrendous. I
deleted the individual indexes on the PK fields of the "many" table and that
made the query instantaneous. My question is: When creating a PK, does
that automatically index the individual fields? If so, then does my
re-indexing them slow things down?

Thanks.

Matthew Wells
MW****@NumberCruncher.com
Nov 12 '05 #1
1 1519
Matthew Wells wrote:
I have a one to many relationship between two tables. The "one" table has a
5 field PK (total 7 fields, 1400 records) and the "many" table has a 7
field PK (total 15 fields, 1500 records). I followed the general rule of
indexing each field in the PKs in both tables. I was doing a query on both
tables with a "group by" to get the sum of a field in the "many" table. The
query was taking about 5 seconds to run which I thought was horrendous. I
deleted the individual indexes on the PK fields of the "many" table and that
made the query instantaneous. My question is: When creating a PK, does
that automatically index the individual fields? If so, then does my
re-indexing them slow things down?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Creating a multi-column PK creates one index for the combined columns.
E.g. (a simplification):

PK = Col1, Col2

Col1 Col2 PK value
- ------- ------- -------------
AAA BBB AAABBB
CCC DDD CCCDDD

The index is on the PK value column.

Therefore, if you "know" you are going to be using one column of the PK
more often than other PK columns in JOINs or WHERE clauses, that column
should be declared first in the PK declaration. E.g., I want the EmpID
to be the important column in the Sales table PK.

ALTER TABLE Sales
ADD CONSTRAINT PK_Sales PRIMARY KEY (EmpID, SalesDate)

When a query optimization scheme is created it tries to calculate the
best index(es) to use. If there are many indexes refering to the same
column(s) the optimization may be "confused" and not select the best
index(es) to scan during the query run. Simplifying indexes (few, if
any, overlapping indexes) can simplify the optimization scheme
generation and speed up the query.

- --
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQGxjpoechKqOuFEgEQKfzQCg7YaeOcl1b3nYa8a7gMbSBn WWUGcAoONB
5APh/Tz270D3ItvClSvjaeD8
=sQ32
-----END PGP SIGNATURE-----

Nov 12 '05 #2

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

Similar topics

6
by: Anita | last post by:
I have just tested 3 queries using QA. The complete test information : ------ CREATE TABLE agls1 ( fyear char(4) NULL , fprefix char(3) NULL , fvcno char(20) NULL , fdate datetime NULL ,...
9
by: Robert Brown | last post by:
If I use _reverse_ wildcard search will it always result in a table scan? Is it possible to get the DB (Oracle or SQL server) to use indexes when doing reverse wildcard match? let's say I have:...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
9
by: WalterR | last post by:
This is my first time here, so there may be earlier relevant threads of which I am unaware. Though my experience with DB2 is not extensive, such as it is was under OS/390 or equ. My main...
3
by: cmc | last post by:
I look at the table indexes of the system tables to see what kind of indexes in UDB V8. Here is my command: SELECT DISTINCT INDEXTYPE FROM SYSCAT.INDEXES I got indextype "REG" returned. My...
10
by: hilz | last post by:
Hi all. I have a table that i create in MsAccess, using ado connection as follows: create table PAYITEM ( PAYITEM_ID COUNTER PRIMARY KEY, PAYITEM_NAME ...
2
by: Thomas F.O'Connell | last post by:
I'm interested to know a little bit more about the postgres implementation of indexes. I'm specifically wondering what it means in the output of EXPLAIN when a filter is applied. I'm trying to...
2
by: bobby_b_ | last post by:
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...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.