<h2 id="t_13088c10a0102wq5c" class="titName SG_txta">mysql 中合并查询结果union用法 or、in与union all 的查询效率</h2>
<span class="img2">
<img width="15" height="15" align="absmiddle" title="此博文包含图片" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" class="SG_icon SG_icon18">
</span>
<span class="time SG_txtc">(2016-05-09 11:18:23)</span><div class="turnBoxzz"><a href="javascript:;" class="SG_aBtn SG_aBtn_ico SG_turn" action-type="reblog" action-data="{srcBlog:1, blogId:'13088c10a0102wq5c'}"><cite><img class="SG_icon SG_icon111" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" align="absmiddle">转载<em class="arrow">▼</em></cite></a></div> </div>
<div class="articalTag" id="sina_keyword_ad_area">
<table>
<tbody><tr>
<td class="blog_tag">
<script>
var $tag='mysql,union,or,in';
var $tag_code='dfc4c67d981830b93087c8132a78c03b';
var $r_quote_bligid='13088c10a0102wq5c';
var $worldcup='0';
var $worldcupball='0';
</script>
<span class="SG_txtb">标签:</span>
<h3><a href="http://search.sina.com.cn/?c=blog&q=mysql&by=tag" target="_blank">mysql</a></h3>
<h3><a href="http://search.sina.com.cn/?c=blog&q=union&by=tag" target="_blank">union</a></h3>
<h3><a href="http://search.sina.com.cn/?c=blog&q=or&by=tag" target="_blank">or</a></h3>
<h3><a href="http://search.sina.com.cn/?c=blog&q=in&by=tag" target="_blank">in</a></h3>
</td>
<td class="blog_class">
<span class="SG_txtb">分类:</span>
<a target="_blank" href="http://blog.sina.com.cn/s/articlelist_5109235978_5_1.html">mysql</a>
</td>
</tr>
</tbody></table>
</div>
<!-- 正文开始 -->
<div id="sina_keyword_ad_area2" class="articalContent newfont_family">
<p style="margin: 0px; line-height: 28px; padding: 0px 0px 15px; widows: 2; font-stretch: normal; font-family: 宋体, 'Arial narrow', arial, serif; orphans: 2; background-color: rgb(255, 255, 255);">
问题一
中合并查询结果union用法
今天来写写union的用法及一些需要注意的。
如果不想去掉重复的行,可以使用union all。
如:(select * from a order by id) union (select * from b order id);
在子句中,order by 需要配合limit使用才有意义。如果不配合limit使用,会被语法分析器优化分析时去除。
问题二
OR、in和union all 查询效率到底哪个快。
网上很多的声音都是说union all 快于 or、in,因为or、in会导致全表扫描,他们给出了很多的实例。
但真的union all真的快于or、in?本文就是采用实际的实例来探讨到底是它们之间的效率。
1:创建表,插入数据、数据量为1千万【要不效果不明显】。
-
drop
table if EXISTS BT; -
create
table BT( -
ID int(10) NOT NUll, -
VName varchar(20) DEFAULT '' NOT NULL, -
PRIMARY key( ID ) -
)ENGINE=INNODB;
向BT表中插入1千万条数据
这里我写了一个简单的存储过程【所以你的mysql版本至少大于5.0,俺的版本为5.1】,代码如下。
注意:最好
-
DROP
PROCEDURE IF EXISTS test_proc; -
CREATE
PROCEDURE test_proc() -
BEGIN
-
declare
i int default 0; -
set
autocommit = 0; -
while
i<10000000 do -
INSERT
INTO BT ( ID,VNAME ) VALUES( i, CONCAT( 'M', i ) ); -
set
i = i+1; -
if
i 00 = 0 then -
commit;
-
end
if; -
end
while; -
END;
存储过程是最好设置下innob的相关参数【主要和日志、写缓存相关这样能加快插入】,俺没有设置插入1千万条数据插了6分钟。
部分数据如下:1千万数据类似

2:实战

我使用其他的工具--EMS SQL Manager
查询显示时间为
93 ms, 94ms,93 ms,时间相差了多少几乎可以忽略。
然后我们在看看各自的执行计划

这里要注意的字段type 与ref字段
我们发现union all 的所用的 type【type为显示连接使用了何种类型】 为ref 而or和in为range【ref连接类型优于range,相差不了多少】,而查询行数都一样【看rows字段都是为3】。
从整个的过程来看,在索引列使用常数or及in和union all查询相差不了多少。
但为什么在有的复杂查询中,再索引列使用or及in 比union all 速度慢很多呢,这可能是你的查询写的不够合理,让mysql放弃索引而进行全表扫描。
2.2:在非索引列中使用 or、in及union all。

我们发现为啥union all查询时间几乎为 or 和in的三倍。
这是为什么呢,我们先不说,先看看三个的查询计划。

这里我们发现计划几乎一样。
但我们要注意扫描的此时对于 or及in 来说 只对表扫描一次即rows是列为9664782。
而对于union all 来说对表扫描了三次即rows的和为9664782*3。
这也是为什么我们看到union all 为几乎为三倍的原因。
备注:
3:总结
-
select
* from bt where bt.VName = 'M98' or bt.id ='9888589' -
-
select
* from bt where bt.VName = 'M98' -
UNION
ALL -
select
* from bt where bt.id = '9888589'
传入一张图,生成它的油画版!(python实现)(转 )
Python——画一棵漂亮的樱花树(不同种樱花+玫瑰+圣诞树喔)(转)
Python3.7实现自动刷博客访问量(只需要输入用户id)(转)
Python3 多线程的两种实现方式
图片生成字符
SqlServer性能优化 通过压缩与计算列提高性能(十一)
json与bson的区别
浅析Redis 和MongoDB
Solr DocValues详解
- 最新文章
-
黄聪:NaviCat通过Http方式连接服务器的MySQL数据库(转)
黄聪:wordpress后台导致fonts.googleapis.com、ajax.googleapis.com加载慢的解决方法
黄聪:禁止wordpress版本自动升级的解决方案
黄聪:wordpress更新失败‘C:WindowsTEMP/wordpress.tmp’,更换临时保存路径的解决办法
黄聪:Dsicuz x2.5、X3、X3.2如何去掉域名后面的/forum.php
黄聪:Discuz!的SEO优化策略二:如何去掉页脚多余的信息
黄聪:Discuz!的SEO优化策略一:如何设置标题 & 如何去掉Powered by Discuz!尾巴
黄聪:wordpress后台加载ajax.googleapis.com导致打开速度很慢的解决方案
黄聪:wordpress中remove_action、add_action、 do_action()的hook钩子都有哪些
黄聪:如何用代码设置控制自己网站的网页在360浏览器打开时强制优先使用极速模式,而非兼容模式
- 热门文章
-
黄聪:VPS实现自动定时备份网站数据以及Mysql数据库到百度云同步盘
黄聪:博客园的积分和排名算法探讨,积分是怎么计算的?(转)
黄聪:如何删除wordpress登录之后wp_footer、wp_head自行加载的Open Sans字体、fonts.googleapis.com连接导致卡死的问题
黄聪:wordpress如何使用wp_rewrite实现自定义伪静态,非301重定向。
《黄聪:手机移动站SEO优化教程》4、如何实现手机移动网站和PC站点的自主适配
《黄聪:手机移动站SEO优化教程》3、如何禁止百度对PC网站进行自动转码
python操作word
python3与Excel的完美结合
Python学习爬虫 requests库
用Python来做一个屏幕录制工具