Home >Database >Mysql Tutorial >Which one is more efficient to use union or in?
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!