In my application I have two queries which will be quite frequently used. The Where clauses of these queries are the following:
WHERE FieldA = @P1 AND (FieldB = @P2 OR FieldC = @P2)
and
WHERE FieldA = @P1 AND FieldB = @P2
P1
and P2
are parameters entered in the UI or coming from external datasources.
int
and highly non-unique, means: only two, three, four different values in a table with say 20000 rowsvarchar(20)
and is "almost" unique, there will be only very few rows where FieldB might have the same valuevarchar(15)
and also highly distinct, but not as much as FieldBI'm wondering now what's the best way to define an index to speed up specifically these two queries. Shall I define one index with...
FieldB (or better FieldC here?)
FieldC (or better FieldB here?)
FieldA
... or better two indices:
FieldB
FieldA
and
FieldC
FieldA
Or are there even other and better options? What's the best way and why?
Thank you for suggestions in advance!
Edit:
Just as an info to other readers: Here was another answer which has been deleted now. Actually the answer seemed very useful to me. The recommendation was to create two indices (according to my second option above) and to reformulate the first query by using a UNION
of two select statements (one with WHERE FieldA = @P1 AND FieldB = @P2
and one with WHERE FieldA = @P1 AND FieldC = @P2
) instead of OR
to benefit from both indices (which wouldn't be the case with the OR-operator).
Edit2:
The statement that with OR the indexes are not used and that a UNION is preferable seems to be wrong - at least according to my own tests (see my own answer below).
Extending Remus' (edit: now deleted) answer...
I wouldn't bother with indexes until you resolve this datatype precedence issue: this is on top of the OR clause issue.
Finally, a column is unique or non-unique: there is no in between. Statistics help here with selectivity, but it's irrelevant.
I would reverse the indexes from Remus' answer to be FieldB, FieldA
(and unique) and FieldC, FieldA
because of FieldA's selectivity
Edit, after comments: you can't compare the use of @p2 against the use of constant strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With