SQL
Optimization
#1
Use‘regexp_like’ to replace ‘LIKE’ clauses
#2
Use‘regexp_extract’ to replace ‘Case-when Like’
#3
Convert long list of IN clause into a temporary table
#4
Always order your JOINs from largest tables to smallest
tables
#5
Usesimple equi-joins
Two tables with date string e.g., ‘2020-09-01’, but one of the tables only has columns for year, month, day
values
#6
Always "GROUP BY" by the attribute/column with the
largest number of unique entities/values
#7
Avoid subqueries in WHERE clause
#8
Use Max instead of Rank
#9
Selecting Unnecessary Columns
#10
Using Functions on Indexed Columns
#11
OR Conditions Killing Indexes
#12
OR Conditions Killing Indexes
#13
Window Function Optimization
#14
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
#15
🎯 Golden Rules of SQL Optimization
✔ Avoid SELECT *
✔ Filter early
✔ Use proper indexes
✔ Avoid functions on indexed columns
✔ Use execution plan (EXPLAIN)
✔ Understand data distribution
✔ Reduce data movement
If You Found This
Helpful?
Follow for more amazing content
like this !