SyntaxStudy
Sign Up
MySQL Query Rewriting for Performance
MySQL Intermediate 4 min read

Query Rewriting for Performance

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.

Example
-- 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';
Pro Tip

Avoid wrapping indexed columns in functions — it defeats the index.