sql / expert
Snippet
SARGable Predicates for Index Optimization
Expert performance tuning requires 'Search Argumentable' (SARGable) queries. Instead of applying functions like YEAR(order_date), which prevent index usage, we use a range comparison. This allows the database engine to perform an index seek rather than a full table scan.
snippet.sql
1
2
3
4
5
SELECT order_id, customer_idFROM ordersWHERE order_date >= '2023-01-01'AND order_date < '2024-01-01'AND status = 'COMPLETED';
Breakdown
1
WHERE order_date >= '2023-01-01'
Provides a lower bound that the query optimizer can use for index traversal.
2
AND order_date < '2024-01-01'
Provides an upper bound without wrapping the column in a function, maintaining SARGability.