Query Performance enhancements


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
Who is Participating?
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.
If you provide me with scripts for "another table" and complete query you are talking about, I'll be able to add more hints.
smetechitAuthor Commented:
Can PK be non-clustered?
Yes, but by default it is clustered.
smetechitAuthor Commented:
Dear RGBDart,

Attached are the table definitions and query asked for.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.