I like to think I know enought theory, but I have little experience optimizing DB in real world.
I would like to know points of view, thoughts or experiences.
Let’s imagine a scenario like:
Table A
Key: c1, c2, c3, c4
Index: c7, c3, c2
Table B
Key: c1, c2, c3, c4
Index: c1, c5
All are non-clustered.
The tables have 40+ fields.
They are feeded daily by night and have some updates during the day.
Table A, if more queries benefit from Key than Index, could the index impact negatively?
Because insert/delete has to update 2 indexes instead of 1.
Table B, has an extra field at the index, not present in key.
Could a query using c1, c5
Benefit from this Key?:
Key: c1, c2, c3, c4, c5
So that Index could be droped.
What impact does the order of the fields has?
Key: c1, c2, c3
Key: c3, c1, c2
A typical scenario for me is process_date, client_number, operation.
And it feeds with a bunch of data each day (process_date).
A non-clustered index has a negative impact on insert/update/delete performance. The negative impact is usually outweighed by the increased performance of selects.
Yes, if only a few rows share the same c1, then the index will be very effective.
Order is important, both for filtering and ordering. An index on (c1,c2) can be used for
where c1 = 1andwhere c1 = 1 and c2 = 1, but not forwhere c2 = 1. Likewise, it helps withorder by c1, but not withorder by c2.