Home > Database > Mysql Tutorial > SQL Performance: When Is UNION Better Than OR?

SQL Performance: When Is UNION Better Than OR?

DDD
Release: 2025-01-14 11:45:43
Original
296 people have browsed it

SQL Performance: When Is UNION Better Than OR?

SQL performance: The controversy between UNION and OR

Simply thinking that replacing OR statements with UNIONs will improve SQL performance is an oversimplification. However, understanding the nuances of how they are used can lead to significant performance improvements.

Understand the differences

OR executes a single query and retrieves rows that satisfy either condition. UNION, on the other hand, executes two separate queries and combines their results. This distinction becomes critical when the OR involves multiple columns.

Consider the following hypothetical query:

<code class="language-sql">select username from users where company = 'bbc' or city = 'London';</code>
Copy after login

If both company and city columns have indexes, MySQL needs to choose an index. This may result in a full table scan of another column.

UNION as the solution

UNION solves this problem by detaching the query:

<code class="language-sql">select username from users where company = 'bbc'
union
select username from users where city = 'London';</code>
Copy after login

Each subquery effectively utilizes its respective index, and the final result is the union of the two sets.

Notes on sorting

UNION does require sorting to eliminate duplicates in the result set. However, since UNION focuses on a smaller set of rows, the overhead is usually negligible. In most cases where a WHERE clause affects a table, the cost of a full table scan can exceed the cost of a UNION sort.

Conclusion

UNION is not absolutely better than OR. However, it can optimize performance when OR involves predicates on columns with conflicting indexes. Understanding these nuances is critical to writing efficient SQL queries.

The above is the detailed content of SQL Performance: When Is UNION Better Than OR?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template