Explore Library
Code Quiz

Non-Sargable Predicate Kills Index Usage

A wrapped function on an indexed column forces a full scan despite a perfect index.

Codesql
-- Index exists: CREATE INDEX idx_orders_date ON orders(order_date);
-- Goal: fetch all orders placed in 2023, expecting an Index Range Scan.

SELECT order_id, customer_id, total_amount
FROM orders
WHERE YEAR(order_date) = 2023
ORDER BY order_date;

-- EXPLAIN shows a full table scan on 'orders' instead of using idx_orders_date.

Why does this query ignore idx_orders_date and perform a full table scan, and how should it be fixed?