capypad
0 day streak
sql / expert
Snippet

SARGable Predicates for Index Optimization

SARGable (Search ARGumentable) queries allow the engine to perform index seeks rather than full scans. By comparing the column directly to a range instead of wrapping it in a function like EXTRACT, the database can utilize the B-Tree index structure efficiently.

snippet.sql
sql
1
2
3
4
5
SELECT order_id
FROM Orders
WHERE order_date >= '2023-01-01'
AND order_date < '2023-02-01';
-- AVOID: WHERE EXTRACT(MONTH FROM order_date) = 1
Breakdown
1
WHERE order_date >= '2023-01-01'
Provides a clear starting point for an index seek operation.
2
AND order_date < '2023-02-01'
Defines the upper bound without transforming the column data, maintaining SARGability.