Query Optimization Basics
Writing correct SQL is step one. Writing FAST SQL is step two.
Use EXPLAIN to see how the database executes your query:
EXPLAIN QUERY PLAN
SELECT * FROM employees WHERE salary > 100000;Optimization tips:
- Avoid
SELECT* - select only columns you need - Use indexes on columns in
WHEREandJOINclauses - Filter early with
WHEREto reduce rows processed - Use
LIMITwhen you only need a few rows - Avoid functions on indexed columns in
WHERE
Bad: WHERE UPPER(first_name) = 'JOHN' Good: WHERE first_name = 'John'
Even small optimizations matter at scale with millions of rows.
Check your understanding
What is a key SQL optimization best practice?