all groups > sql server programming > november 2003 >
You're in the

sql server programming

group:

Is an additional index needed if I have a double primary key?


Is an additional index needed if I have a double primary key? Viba Fima
11/28/2003 9:34:50 PM
sql server programming:
I have a table with a primary key:
PRIMARY KEY (USERID, ORDERID)

Now I need to search for records with a certain ORDERID. Will the
double-field PRIMARY KEY above support this search, or should I create a new
index for just ORDERID (in addition to keeping the above primary key)?

Thanks,
Krup Nugent


Re: Is an additional index needed if I have a double primary key? Ray Higdon
11/29/2003 7:19:38 AM
Just to add to what Mike has already said, assuming you didn't specify
anything else besides your syntax "PRIMARY KEY (USERID, ORDERID)", SQL would
make this a clustered key. It is typically not recommended to use composite
clustered indexes as they bloat your non-clustered indexes and it's not easy
to proactively prepare for pagesplits. If this will act as a covering index,
it would benefit you more to have this as a non-clustered index., a covered
index could be something like "select orderid from table where userid =
'blah'"

HTH

--
Ray Higdon MCSE, MCDBA, CCNA
---
[quoted text, click to view]

Re: Is an additional index needed if I have a double primary key? Aaron Bertrand [MVP]
11/29/2003 8:18:30 AM
[quoted text, click to view]

I assume you mean try each independently, not together. :-)

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/

Re: Is an additional index needed if I have a double primary key? Mike John
11/29/2003 11:14:02 AM
Viba,

The index created to support the primary key can only be used to provide =
keyed access if the query includes the leftmost columns in the WHERE =
clause, soin this case if you included USERID. If you are only speciying =
selection on ORDERID then all else being equal you should hget a table =
scan which is probably not what you want. So in this case adding an =
additional index on ORFDERID sounds sensible.

I would also experiment with making the table clustered on ORDERID, as =
well as clustered (as I guess it is at the moment) on USERID and =
ORDERID. Without knowing what you are doiung with table it is impossible =
to say which would be best, but try both, measure the performance =
difference and should see which is best.

Mike John


[quoted text, click to view]
Re: Is an additional index needed if I have a double primary key? Mike John
11/29/2003 4:26:43 PM
Thanks Aaron- Yes I did mean independently, but probably did not explain =
that too well!

Mike John

[quoted text, click to view]
Re: Is an additional index needed if I have a double primary key? Vlad Vissoultchev
11/29/2003 4:46:39 PM
[quoted text, click to view]
you should hget a table scan which is probably not what you want <<

not necessary a table scan. more often he will see a clustered index scan
with an additional "WHERE OrderID ..." or if UserID selectivity is low (20-
unique values on a 100k rows table) and joining with users table he will see
a clustered index seek executed 20 times for each value of UserID (coming
from the selection of users table).

HTH,
</wqw>

Re: Is an additional index needed if I have a double primary key? Ray Higdon
11/30/2003 8:06:29 AM
When dealing with index "what if" questions, I find it best to test it
for myself. Looking at sample queries and their exection plans help and
also working with the command "set statistics IO on" to see your logical
IO (how many types you hit a page) helps to determine your best index
strategy.

HTH

Ray Higdon MCSE, MCDBA, CCNA

*** Sent via Developersdex http://www.developersdex.com ***
Re: Is an additional index needed if I have a double primary key? Viba Fima
11/30/2003 8:22:38 AM
Thanks for all contributions,
What happens if instead of PRIMARY KEY (USERID, ORDERID), I have PRIMARY KEY
(ORDERID, USERID) -- I just reverse the order of the columns. If I need a
SELECT on ORDERID only (no USERID specified), do I need a separate index on
ORDERID or will the PRIMARY KEY (ORDERID, USERID) index help me avoid a
table scan?

Thanks,
Krup Nugent



[quoted text, click to view]
Viba,

The index created to support the primary key can only be used to provide
keyed access if the query includes the leftmost columns in the WHERE clause,
soin this case if you included USERID. If you are only speciying selection
on ORDERID then all else being equal you should hget a table scan which is
probably not what you want. So in this case adding an additional index on
ORFDERID sounds sensible.

I would also experiment with making the table clustered on ORDERID, as well
as clustered (as I guess it is at the moment) on USERID and ORDERID. Without
knowing what you are doiung with table it is impossible to say which would
be best, but try both, measure the performance difference and should see
which is best.

Mike John


[quoted text, click to view]

Re: Is an additional index needed if I have a double primary key? Tom Moreau
11/30/2003 10:18:39 AM
As always, it depends. This will be great if what you want to get is a
particular order. However, if what you are looking for is all orders for a
particular UserID, then you'd be best to have a nonclustered index on
UserID. In the end, you have to analyze all queries going to that table to
settle on the best candidate for the clustered index.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
..
[quoted text, click to view]
Thanks for all contributions,
What happens if instead of PRIMARY KEY (USERID, ORDERID), I have PRIMARY KEY
(ORDERID, USERID) -- I just reverse the order of the columns. If I need a
SELECT on ORDERID only (no USERID specified), do I need a separate index on
ORDERID or will the PRIMARY KEY (ORDERID, USERID) index help me avoid a
table scan?

Thanks,
Krup Nugent



[quoted text, click to view]
Viba,

The index created to support the primary key can only be used to provide
keyed access if the query includes the leftmost columns in the WHERE clause,
soin this case if you included USERID. If you are only speciying selection
on ORDERID then all else being equal you should hget a table scan which is
probably not what you want. So in this case adding an additional index on
ORFDERID sounds sensible.

I would also experiment with making the table clustered on ORDERID, as well
as clustered (as I guess it is at the moment) on USERID and ORDERID. Without
knowing what you are doiung with table it is impossible to say which would
be best, but try both, measure the performance difference and should see
which is best.

Mike John


[quoted text, click to view]
AddThis Social Bookmark Button