<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'
基于复制的高可用
No orientation specified, and the default is
iOS 图标、图形尺寸? iPhone、iPad、 iPod touch
cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程)
loaded some nib but the view outlet was not set
IOS开发:UIAlertView使用
UIAlertView笔记
Xcode之外的文档浏览工具--Dash (在iOS代码库中浏览本帖)
iOS 开发者能用上的 10 个 Xcode 插件