Home  >  Article  >  Database  >  Which one is more efficient to use union or in?

Which one is more efficient to use union or in?

一个新手
一个新手Original
2017-09-30 10:17:313107browse

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);  

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);

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  

is very efficient promote.

mysql>
108 rows in set (0.02 sec)

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!

Statement:
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