Home > Database > Mysql Tutorial > body text

How to use Union to optimize Like statement in MySQL

WBOY
Release: 2023-05-31 15:55:06
forward
895 people have browsed it

Use Union to optimize Like statements

1) Sometimes, you may need to use the or operator in the query for comparison. When the or keyword is used too frequently in the where clause, it may cause the MySQL optimizer to mistakenly choose a full table scan to retrieve records. The union clause can make queries execute faster, especially when one of the queries has an optimized index and the other query also has an optimized index.

For example, when there are indexes on first_name and last_name respectively, execute the following query statement:

mysql> select * from students where first_name like 'Ade%' or last_name like 'Ade%'
Copy after login

Compared with the above query and the following query that uses union to merge two query statements, Much slower.

mysql> select * from students where first_name like 'Ade%' union all select * from students wherelast_name like 'Ade%'
Copy after login

The above is the detailed content of How to use Union to optimize Like statement in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!