SQL Quest › Free SQL tools › SQL Query Optimizer
SQL query optimizer
Paste a query. It points out the patterns that make SQL slow — functions on indexed columns, leading wildcards, correlated subqueries, DISTINCT hiding a fan-out — and shows the faster shape. None of the suggestions change the result.
What it looks for
A function around a column in WHERE
YEAR(created_at) = 2024 cannot use an index on created_at. A date range can.
LIKE with a leading '%'
No ordinary index helps a pattern that can start anywhere.
A correlated subquery in SELECT
It runs once per outer row. A join to a pre-aggregated CTE, or a window function, runs once.
DISTINCT on top of a JOIN
Usually a fan-out being cleaned up after the fact. Fix the grain instead.
UNION where UNION ALL would do
UNION removes duplicates, which costs a sort or a hash over the whole result.
NOT IN (SELECT …)
NOT EXISTS is usually planned better, and it is correct when NULLs appear.
(SELECT COUNT(*) …) > 0
EXISTS stops at the first match; COUNT reads them all.
The same subquery written twice
Name it once in a WITH clause.
A row filter in HAVING
A condition that uses no aggregate can run in WHERE, before grouping.
SELECT *
Reads columns you do not use and breaks when the table changes.
It reads the SQL text — it does not connect to a database or see your data. A query can pass every check and still answer the wrong question, which is what a grader against real tables is for.
These are the mistakes interviews are built to catch.
SQL Quest grades your query against real tables and tells you which rows are wrong, then builds your practice around the skills you miss. Ten questions find the weakest one.
Questions
Does this optimizer read my execution plan?
No. It reads the query text and flags patterns that are slow on almost every database. For a specific table, run EXPLAIN (or EXPLAIN ANALYZE) on your own database — these suggestions tell you what to look for in it.
What does sargable mean?
A condition the database can answer with an index. created_at >= '2024-01-01' is sargable; YEAR(created_at) = 2024 is not, because the function has to be computed for every row first.
Is UNION slower than UNION ALL?
Usually, yes. UNION removes duplicate rows, which needs a sort or hash over the combined result. If the two halves cannot overlap, UNION ALL returns the same rows with less work.