使用union和in哪個效率高

一直都認為是in的效率要高,但是這次確有點蒙圈。

select?*?FROM?runinfo?WHERE?status?in?(0,2,1,3,4,7,9,10);  

這個查詢的效率是,經常是1秒多。

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

106 rows in set (1.20 sec)

?替換成下面這種寫法之后,

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  

效率有很大的提升。

mysql>  108?rows?in?set?(0.02?sec)

?具體的原因我也說不上來,其中status字段有建索引,這個字段會有一些更新。

先做記錄,慢慢再搞清楚這件事情吧。

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享