中Innerjoin和where的效率差异

来源:互联网 发布:江汉大学网络缴费系统 编辑:程序博客网 时间:2024/05/16 16:56
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
天,手头上正在作的一个项目,在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在SQL语句上下功夫。(我这人比较懒,对简单的语句和查询都没有经过仔细优化的,一般只对姚使用leftjoin,outerjoin,groupby以及carsor的语句会仔细写并用理论考虑和检查---因为这种语句一般测试时如果发现错误,检查和调试很麻烦)

先在网上Google搜索“Join与where效率”以及察看SQLServer帮助文档,希望能获得“捷径”些的优化思路。

搜索的结果是,各大论坛,包括MSDN上很多人提出了这个问题,但回答是众说纷纭。总体上总结出来时说:对小数据量(<N万)的来说效率几乎无差异,更有说法说Innerjoinwhere只是SQL标准不同,在查询分析器中SQLServer查询分析器是将where直接转换为Join后查询的。

还是自己来做试验吧。

如是有了如下比较结果(均在查询分析器中查询和计时):

语句(1)

declare@operatorNamenvarchar(50)

set@operatorName='%'

selectdistinctitem.*fromitem,customer_item,customer_operator,operator

whereitem.itemcode=customer_item.itemCode

andcustomer_item.customerCode=customer_operator.customerCode

andcustomer_operator.operatorId=customer_operator.operatorId

andoperator.operatorNamelike@operatorName

anditem.deleted=0andcustomer_item.deleted=0andcustomer_operator.deleted=0

查询结果,74行,共时间0:00:04

语句(2)

declare@operatorNamenvarchar(50)

set@operatorName='%'

selectdistinctitem.*fromitemInnerjoincustomer_item

onitem.itemcode=customer_item.itemCode

Innerjoincustomer_operatoroncustomer_item.customerCode=customer_operator.customerCode

Innerjoinoperatoroncustomer_operator.operatorId=operator.operatorId

whereoperator.operatorNamelike@operatorName

anditem.deleted=0andcustomer_item.deleted=0andcustomer_operator.deleted=0

共74行,时间0:00:01

后检查发现语句(1)中有一个重复自查询条件:customer_operator.operatorId=customer_operator.operatorId

将其叶加到语句2中,语句(3)

declare@operatorNamenvarchar(50)

set@operatorName='%'

selectdistinctitem.*fromitemInnerjoincustomer_item

onitem.itemcode=customer_item.itemCode

Innerjoincustomer_operatoroncustomer_item.customerCode=customer_operator.customerCode

Innerjoinoperatoroncustomer_operator.operatorId=operator.operatorId

whereoperator.operatorNamelike@operatorName

anditem.deleted=0andcustomer_item.deleted=0andcustomer_operator.deleted=0

andcustomer_operator.operatorId=customer_operator.operatorId

所用时间和结果都为74行,时间0:00:01。

将语句(1)中的去掉该条件后成为语句(4)

declare@operatorNamenvarchar(50)

set@operatorName='%'

selectdistinctitem.*fromitem,customer_item,customer_operator,operator

whereitem.itemcode=customer_item.itemCode

andcustomer_item.customerCode=customer_operator.customerCode

--andcustomer_operator.operatorId=customer_operator.operatorId

andoperator.operatorNamelike@operatorName

anditem.deleted=0andcustomer_item.deleted=0andcustomer_operator.deleted=0

时间和上一页 
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击