Home > Database > Mysql Tutorial > body text

Which one is more efficient to use union or in?

一个新手
Release: 2017-09-30 10:17:31
Original
3111 people have browsed it

I have always thought that in is more efficient, but this time I was a bit confused.


SELECT * FROM runinfo WHERE status in (0,2,1,3,4,7,9,10);  
Copy after login

The efficiency of this query is often more than 1 second.

mysql> SELECT * FROM runinfo WHERE status in (0,2,1,3,4,7,9,10);
Copy after login

106 rows in set (1.20 sec)

After replacing it with the following writing method,


SELECT * FROM runinfo WHERE status = 0
union
SELECT * FROM runinfo WHERE status = 1
union
SELECT * FROM runinfo WHERE status = 2
union
SELECT * FROM runinfo WHERE status = 3
union
SELECT * FROM runinfo WHERE status = 4
union
SELECT * FROM runinfo WHERE status = 7
union
SELECT * FROM runinfo WHERE status = 9
union
SELECT * FROM runinfo WHERE status = 10  
Copy after login

is very efficient promote.

mysql>
108 rows in set (0.02 sec)
Copy after login

I can’t tell you the specific reason. The status field is indexed and there will be some updates to this field.

Make a record first, and then figure it out later.

The above is the detailed content of Which one is more efficient to use union or in?. For more information, please follow other related articles on the PHP Chinese website!

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