Top 20 Query Optimization Techniques
Some tips on query performance.
- Create an index on huge tables (>1.000.000 rows)
- Use EXISTS() instead of COUNT() to find an element in the table
- SELECT specific fields instead of using SELECT *
- Avoid subqueries in WHERE clause
- Avoid SELECT DISTINCT where possible
- Use WHERE clause instead of HAVING
- Create joins with INNER JOIN (not WHERE)
- Use LIMIT to sample query results
- Use UNION ALL instead of UNION wherever possible
- Use UNION where instead of WHERE ... OR ... query
- Run your query during off-peak hours
- Avoid using OR in join queries
- Choose GROUP BY over window functions
- Use derived and temporary tables
- Drop the index before loading bulk data
- Use materialized views instead of views
- Avoid != or <> (not equal) operator
- Minimize the number of subqueries
- Use INNER JOIN as little as possible when you can get the same output using LEFT/RIGHT JOIN
- Frequently try to use temporary sources to retrieve the same dataset