All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP教程实例:用PHP打造动态聊天室系统_[PHP教程]
GridView每页添加不重新开始序号列_[Asp.Net教程]
如何使用Ajax技术开发Web应用程序_[AJAX教程]
MS SQL 2005的新功能创建数据库快照_[SQL Server教程]
技术畅谈:开发大型PHP项目的技巧与方法_php资料_编程技术
ASP.NET中多国语言的实现_[Asp.Net教程]
ASP.NET,DataGrid,控件深入研究_[Asp.Net教程]
PHP实例:将IP最后一位替换为星号_[PHP教程]
SQL中的全文检索_[SQL,Server教程]
Asp.Net2.0中实现多任务异步页的一点提示_[Asp.Net教程]
Reading number is top 10 pictures
BingBingFan apple dew point photo gallery1
9.3阅兵全景图5-地空反舰导弹梯队
The money of more than 100 countries and regions7
不知名的美女素颜照1
Sora aoi calligraphy show
Beauty shocked Japan Tokyo motor show model
007 James. bond's new lover
Valentine's day comes, send some cartoon
Beauty is thus produced
China's zhejiang university to create the world's most light material
Download software ranking
Tram sex maniac 2 (H) rar bag16
WebService在.NET中的实战应用教学视频 → 第2集
Unix video tutorial2
SP3 for SQL2000
美女游泳记
DreamWeaver8
matrix1
C#与.NET技术平台实战演练
Dance with duck(male prostitution)
Photoshop 8.0图象编辑软件
归海一刀 published in(发表于) 2014/2/3 6:35:18 Edit(编辑)
SQL Server 中Inner join 和where的效率差异_[SQL Server教程]

SQL Server 中Inner join 和where的效率差异_[SQL Server教程]

SQL Server 中Inner join 和where的效率差异_[SQL Server教程]


今天,手头上正在作的一个项目,在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在SQL 语句上下功夫。(我这人比较懒,对简单的语句和查询都没有经过仔细优化的,一般只对姚使用left join,outer join,group by 以及carsor的语句会仔细写并用数据库理论考虑和检查---因为这种语句一般测试时如果发现错误,检查和调试很麻烦)



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



搜索的结果是,各大论坛,包括MSDN上很多人提出了这个问题,但回答是众说纷纭。总体上总结出来时说:对小数据量(


还是自己来做试验吧。



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



语句(1)
declare @operatorName nvarchar(50)
set @operatorName = '%'


select distinct item.* from item , customer_item , customer_operator ,operator
where item.itemcode = customer_item.itemCode
and customer_item.customerCode = customer_operator.customerCode
and customer_operator.operatorId = customer_operator.operatorId
and operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
查询结果,74行,共时间0:00:04



语句(2)
declare @operatorName nvarchar(50)


set @operatorName = '%'


select distinct item.* from item inner join customer_item
on item.itemcode = customer_item.itemCode
inner join customer_operator on customer_item.customerCode = customer_operator.customerCode
inner join operator on customer_operator.operatorId = operator.operatorId
where operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
共74行,时间0:00:01



后检查发现语句(1)中有一个重复自查询条件 :customer_operator.operatorId = customer_operator.operatorId
将其叶加到语句2中,语句(3)
declare @operatorName nvarchar(50)


set @operatorName = '%'


select distinct item.* from item inner join customer_item
on item.itemcode = customer_item.itemCode
inner join customer_operator on customer_item.customerCode = customer_operator.customerCode
inner join operator on customer_operator.operatorId = operator.operatorId
where operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
and customer_operator.operatorId = customer_operator.operatorId


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



将语句(1)中的去掉该条件后成为语句(4)
declare @operatorName nvarchar(50)
set @operatorName = '%'


select distinct item.* from item , customer_item , customer_operator ,operator
where item.itemcode = customer_item.itemCode
and customer_item.customerCode = customer_operator.customerCode
--and customer_operator.operatorId = customer_operator.operatorId
and operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0


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


终于发现了些他们的差异。


结论:
尽量使用Join 而不是Where来列出关联条件,特别是多个表联合的时候。
原因是:
(1)在效率上,Where可能具有和Inner join一样的效率。但基本可以肯定的(通过SQLServer帮助和其它资料,以及本测试)是Join的效率不比Where差。
(2)使用Join可以帮助检查语句中的无效或者误写的关联条件


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.