I have a composite table called TeacherClass with two columns, TeacherID and ClassID. The primary key is both the TeacherID and ClassID. Even though these are also considered foreign keys, should I add an index to each column separately even though they have one on both of them combined from the primary key?
I have a composite table called TeacherClass with two columns, TeacherID and ClassID. The
Share
If they are both part of the PK, then most likely are already in a clustered index, but index will have, say
(TeacherID, ClassID), not the other way around (ClassID, TeacherID). This means the table will be fast when running something like:But slow when running
If you are planning on running similar select statements, add a new index including ClassID and TeacherID, in that order. You won’t need any separate indexes then (having an index that includes Col1 and Col2, makes an index that includes just Col1 redundant).