Skip to main content
Incorporated comment
Source Link
Paul White
  • 95.7k
  • 30
  • 440
  • 691

It is in the case if you just use just first in the predicate.

It will do scan if you use first columns of composite key and non-key column of composite key.

To trick it you can just dummy predicates like this and then non-key column:

[A,B] is your index, [C] - another column

To utilize index you write as:

SELECT A,B,C,D,E FROM test WHERE A=1 AND B=B AND C=3 

...why does it select the composite index by default, if the single A index is available?

It will use index only in the case if there are one or two predicates [A] Or [A],[B]. It will not use it in the order [B],[A] or [A],[C]. To be able to utilize index with additional column [C], you need to enforce index by ordering predicates as [A],[B] and [C].

It is in the case if you just use just first in the predicate.

It will do scan if you use first columns of composite key and non-key column of composite key.

To trick it you can just dummy predicates like this and then non-key column:

[A,B] is your index, [C] - another column

To utilize index you write as:

SELECT A,B,C,D,E FROM test WHERE A=1 AND B=B AND C=3 

It is in the case if you just use just first in the predicate.

It will do scan if you use first columns of composite key and non-key column of composite key.

To trick it you can just dummy predicates like this and then non-key column:

[A,B] is your index, [C] - another column

To utilize index you write as:

SELECT A,B,C,D,E FROM test WHERE A=1 AND B=B AND C=3 

...why does it select the composite index by default, if the single A index is available?

It will use index only in the case if there are one or two predicates [A] Or [A],[B]. It will not use it in the order [B],[A] or [A],[C]. To be able to utilize index with additional column [C], you need to enforce index by ordering predicates as [A],[B] and [C].

Source Link

It is in the case if you just use just first in the predicate.

It will do scan if you use first columns of composite key and non-key column of composite key.

To trick it you can just dummy predicates like this and then non-key column:

[A,B] is your index, [C] - another column

To utilize index you write as:

SELECT A,B,C,D,E FROM test WHERE A=1 AND B=B AND C=3