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] "Viba Fima" <support@vibafima.com> wrote in message
news:%233T5My0tDHA.2236@TK2MSFTNGP10.phx.gbl...
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] "Mike John" <Mike.John@knowledgepool.spamtrap.com> wrote in message
news:OqLminmtDHA.1760@TK2MSFTNGP10.phx.gbl...
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] "Viba Fima" <support@vibafima.com> wrote in message
news:Of8%23hjitDHA.980@TK2MSFTNGP10.phx.gbl...
> 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
>
>