Query Rewriting
Small rewrites can have big impacts: replace correlated subqueries with JOINs, avoid functions on indexed columns, and use EXISTS instead of COUNT for existence checks.
Small rewrites can have big impacts: replace correlated subqueries with JOINs, avoid functions on indexed columns, and use EXISTS instead of COUNT for existence checks.
-- Slow: function prevents index use
SELECT * FROM users WHERE YEAR(created_at) = 2024;
-- Fast: range condition uses index
SELECT * FROM users
WHERE created_at >= '2024-01-01'
AND created_at < '2025-01-01';
Avoid wrapping indexed columns in functions — it defeats the index.