Here are some SQL optimization Tips.
- Use ‘regexp_like‘ to replace ‘LIKE‘ clauses
- Use ‘regexp_extract‘ to replace ‘Case-when Like’
- Convert a long list of In clauses into a temporary table.
- Always order your JOINs from the largest tables to the smallest tables.
- Use simple equi-joins.
- Always “GROUP BY” the attributes/column with the largest number of unique entities/values.
- Avoid subqueries in the WHERE clause.
- Use Max instead of Rank.
- Other Tips
- Use approx_distinct() instead of count(distinct) for very large datasets.
- Use approx_percentile(metric, 0.5) for median
- Avoid UNIONs where possible
- Use With statements vs nested subqueries