Query Performance enhancements

Hello,

I have a SQL 2008 R2 database table with below structure (FK means that a column is a Forign Key)

MachineId (int)
ProductId (nvarchar(20), FK)
ProductChangeDateTime (datetime)
ShiftId (int, FK)
Column 5 (real)
Column 6 (real)
Column 7 (real)
Column 8 (int, FK)
Column 9 (int, FK)
Column 10 (int, FK)
Column 11 (nvarchar(4), FK)
Column 12 (datetime)
Column 13 (nvarchar(4), FK)
Column 14 (datetime)

Primary Key is compsed of MachineId and ProductChangeDateTime

I have a Query on this table that inner join with another table on ProductId and the search criteria are MachineId and ShiftId

My question is what is the best for this query performance and why:
 - to add index on ShiftId
 - or to add shiftId to table PK
smetechitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RGBDartCommented:
If your PK is a clustered index, than adding ShiftID to PK is better (but remeber about unique constraint!).
You must place columns in PK in correct order to have positive effect - (MachineId, ShiftId, ProductChangeDateTime) or (ShiftId, MachineId, ProductChangeDateTime).
If you do this, query you described will perform "clustered index seek" - fastest operation possible for a table (check the query plan to see it youself).
Simply speaking, records in a table "physically" sorted by clustered key columns values, and can be easily found / retrieved.

If that "other table" have index on ProductId - this will speed up the query too.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RGBDartCommented:
P.S.
If you provide me with scripts for "another table" and complete query you are talking about, I'll be able to add more hints.
0
smetechitAuthor Commented:
Can PK be non-clustered?
0
RGBDartCommented:
Yes, but by default it is clustered.
0
smetechitAuthor Commented:
Dear RGBDart,

Attached are the table definitions and query asked for.


tbl2-ProductsDef.sql
tbl1-MachineProductsTrx.sql
sp-GetMachineProducts.sql
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.