论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Sql Server教程
Tag:注入,存储过程,分页,安全,优化,加密,索引,日志,压缩,base64,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,触发器,socket,安装,sqlserver2000,sqlserver2005,sqlserver2008,视频教程

SQL Server 中Inner join 和where的效率差异

文章类别:Sql Server | 发表日期:2008-10-5 21:37:53

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

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

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

还是自己来做试验吧。

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

语句(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可以帮助检查语句中的无效或者误写的关联条件

视频教程列表
文章教程搜索
 
Sql Server推荐教程
Sql Server热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058