今天有人问in一堆条件的sql如何优化。这个很天然就想到用union来代替in来提升效率,网上不少例子也是这么说的html
http://blog.csdn.net/adparking/article/details/6678911mysql
http://hi.baidu.com/dereky/blog/item/382c2df536c0532cbc310929.html ios
http://www.cnblogs.com/xwblog/archive/2012/04/09/2438737.htmlsql
但是我在本机作实验为何有相反地结果呢...数据库
EXPLAIN SELECT * from employees where employees.first_NAME ='Georgi' UNION ALL SELECT * from employees where employees.first_NAME ='Bezalel'
这条语句执行结果481条,执行时间为0.35s
1 PRIMARY employees ALL 300141 Using wheremysql优化
2 UNION employees ALL 300141 Using whereide
UNION RESULT <union1,2> ALL 性能
explain SELECT * FROM employees WHERE employees.first_name IN ('Georgi','Bezalel')
这条语句的执行结果时间为0.186s
explain SELECT * FROM employees WHERE employees.first_name ='Georgi' or employees.first_name='Bezalel'
这条语句的执行结果和in的结果差很少
EXPLAIN SELECT * FROM employees WHERE employees.EMP_NO=100001 UNION ALL SELECT * FROM employees WHERE employees.EMP_NO=101100
EXPLAIN SELECT * FROM employees WHERE employees.EMP_NO IN (100001 ,101100)
EXPLAIN SELECT * FROM employees WHERE employees.EMP_NO=100001 OR emp_no=101100
select * from employees where first_name ='Georgi' union select * from employees where first_name ='Bezalel' union select * from employees where first_name ='Parto' union select * from employees where first_name ='Chirstian' union select * from employees where first_name ='Kyoichi' union select * from employees where first_name ='Anneke' union select * from employees where first_name ='Tzvetan' union select * from employees where first_name ='Saniya' union select * from employees where first_name ='Sumant' union select * from employees where first_name ='Duangkaew' union select * from employees where first_name ='Mary' union select * from employees where first_name ='Patricio' union select * from employees where first_name ='Eberhardt' union select * from employees where first_name ='Berni' union select * from employees where first_name ='Guoxiang' union select * from employees where first_name ='Kazuhito' union select * from employees where first_name ='Cristinel' union select * from employees where first_name ='Kazuhide' union select * from employees where first_name ='Lillian' union select * from employees where first_name ='Mayuko' union select * from employees where first_name ='Ramzi' union select * from employees where first_name ='Shahaf' union select * from employees where first_name ='Bojan' union select * from employees where first_name ='Suzette' union select * from employees where first_name ='Prasadram' union select * from employees where first_name ='Yongqiao' union select * from employees where first_name ='Divier' union select * from employees where first_name ='Domenick' union select * from employees where first_name ='Otmar' union select * from employees where first_name ='Elvis' union select * from employees where first_name ='Karsten' union select * from employees where first_name ='Jeong' union select * from employees where first_name ='Arif' union select * from employees where first_name ='Bader' union select * from employees where first_name ='Alain' union select * from employees where first_name ='Adamantios' union select * from employees where first_name ='Pradeep' union select * from employees where first_name ='Huan' union select * from employees where first_name ='Alejandro' union select * from employees where first_name ='Weiyi' union select * from employees where first_name ='Uri' union select * from employees where first_name ='Magy' union select * from employees where first_name ='Yishay' union select * from employees where first_name ='Mingsen' union select * from employees where first_name ='Moss' union select * from employees where first_name ='Lucien' union select * from employees where first_name ='Zvonko' union select * from employees where first_name ='Florian' union select * from employees where first_name ='Basil' union select * from employees where first_name ='Yinghua' union select * from employees where first_name ='Hidefumi' union select * from employees where first_name ='Heping' union select * from employees where first_name ='Sanjiv' union select * from employees where first_name ='Mayumi' union select * from employees where first_name ='Georgy' union select * from employees where first_name ='Brendon' union select * from employees where first_name ='Ebbe' union select * from employees where first_name ='Berhard' union select * from employees where first_name ='Breannda' union select * from employees where first_name ='Tse' union select * from employees where first_name ='Anoosh' union select * from employees where first_name ='Gino' union select * from employees where first_name ='Udi' union select * from employees where first_name ='Satosi' union select * from employees where first_name ='Kwee' union select * from employees where first_name ='Claudi' union select * from employees where first_name ='Charlene' union select * from employees where first_name ='Margareta' union select * from employees where first_name ='Reuven' union select * from employees where first_name ='Hisao' union select * from employees where first_name ='Hironoby' union select * from employees where first_name ='Shir' union select * from employees where first_name ='Mokhtar' union select * from employees where first_name ='Gao' union select * from employees where first_name ='Erez' union select * from employees where first_name ='Mona' union select * from employees where first_name ='Danel' union select * from employees where first_name ='Kshitij' union select * from employees where first_name ='Premal' union select * from employees where first_name ='Zhongwei' union select * from employees where first_name ='Parviz' union select * from employees where first_name ='Vishv' union select * from employees where first_name ='Tuval' union select * from employees where first_name ='Kenroku' union select * from employees where first_name ='Somnath' union select * from employees where first_name ='Xinglin' union select * from employees where first_name ='Jungsoon' union select * from employees where first_name ='Sudharsan' union select * from employees where first_name ='Kendra' union select * from employees where first_name ='Amabile' union select * from employees where first_name ='Valdiodio' union select * from employees where first_name ='Sailaja' union select * from employees where first_name ='Arumugam' union select * from employees where first_name ='Hilari' union select * from employees where first_name ='Jayson' union select * from employees where first_name ='Remzi' union select * from employees where first_name ='Sreekrishna' union select * from employees where first_name ='Valter' union select * from employees where first_name ='Hironobu' union select * from employees where first_name ='Perla'
执行时间17.854s,共23527条数据
select * from employees where first_name in ('Georgi', 'Bezalel', 'Parto', 'Chirstian', 'Kyoichi', 'Anneke', 'Tzvetan', 'Saniya', 'Sumant', 'Duangkaew', 'Mary', 'Patricio', 'Eberhardt', 'Berni', 'Guoxiang', 'Kazuhito', 'Cristinel', 'Kazuhide', 'Lillian', 'Mayuko', 'Ramzi', 'Shahaf', 'Bojan', 'Suzette', 'Prasadram', 'Yongqiao', 'Divier', 'Domenick', 'Otmar', 'Elvis', 'Karsten', 'Jeong', 'Arif', 'Bader', 'Alain', 'Adamantios', 'Pradeep', 'Huan', 'Alejandro', 'Weiyi', 'Uri', 'Magy', 'Yishay', 'Mingsen', 'Moss', 'Lucien', 'Zvonko', 'Florian', 'Basil', 'Yinghua', 'Hidefumi', 'Heping', 'Sanjiv', 'Mayumi', 'Georgy', 'Brendon', 'Ebbe', 'Berhard', 'Breannda', 'Tse', 'Anoosh', 'Gino', 'Udi', 'Satosi', 'Kwee', 'Claudi', 'Charlene', 'Margareta', 'Reuven', 'Hisao', 'Hironoby', 'Shir', 'Mokhtar', 'Gao', 'Erez', 'Mona', 'Danel', 'Kshitij', 'Premal', 'Zhongwei', 'Parviz', 'Vishv', 'Tuval', 'Kenroku', 'Somnath', 'Xinglin', 'Jungsoon', 'Sudharsan', 'Kendra', 'Amabile', 'Valdiodio', 'Sailaja', 'Arumugam', 'Hilari', 'Jayson', 'Remzi', 'Sreekrishna', 'Valter', 'Hironobu', 'Perla')
很明显,执行时间是0.267s 执行结果23527条。因此union的效果然的很好么?