TLDR Web Dev · ARTICLES & TUTORIALS

A SQL HEURISTIC: ORS ARE EXPENSIVE

ethanseal.com8 min read

Summary from the TLDR Web Dev newsletter

OR clauses in SQL queries can be much slower than AND clauses due to how query planners use indexes. When using OR, the database might resort to less efficient strategies like merging separate filtered results or performing a full table scan. Rewriting queries to use AND clauses, often by using unions or restructuring the schema, can improve performance. Common schema issues that lead to needing OR clauses can be addressed by creating secondary tables or using an extension table pattern.