SQL normally assumes that terms in the
WHEREclause that cannot be used by indices are usually true. If this assumption is incorrect, it could lead to a suboptimal query plan. Use the
UNLIKELY(X)SQL functions to provide hints to the query planner about clause terms that are probably not true, which helps the query planner to select the best possible plan.
UNLIKELYto optimize evaluation of
UNLIKELYcauses the left side of an expression to be evaluated first. This allows the right side of the query to be skipped when possible. For example, in the clause
UNLIKELY(A) AND B, if
Bdoes not need to be evaluated.
Consider the following:
SELECT COUNT(*) FROM test WHERE UNLIKELY(x IN (7, 8, 9, 10)) AND y > 42;
xis one of the values
10, the filter
y > 42is applied. If
xis not one of those values, the filter
y > 42is not applied.