手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Mssql>列表

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

来源:互联网 作者:west263.com 时间:2008-04-02
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

天,手头上正在作的一个项目,在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在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

[1] [2] 下一页


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!