网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > 数据库 > SQL技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
数据库:数据库教程,数据库技巧,Oracle教程,MySQL教程,Sybase教程,Access教程,DB2教程,数据库安全,数据库文摘
本月文章推荐
.讲解数据库调优与程序员相关的几.
.简单三步走堵死SQLServer注入漏洞.
.讲解使用SQL Server升级顾问的详.
.使用正规表达式编写更好的SQL.
.Microsoft SQL Server数据库SA权.
.用SQLServer2000索引视图提高性能.
.SQLSERVER扩展存储过程XP_CMDSHE.
.log4net和SQL Server 2000.
.改善SQL Server内存管理.
.[SQL SERVER] 跨服务器查询.
.在SQL Server中用XQuery分解XML数.
.个人经验总结:SQL Server数据库.
.小結SQL Server連接失敗錯誤及解.
.比较一下看看自己掌握了多少SQL快.
.Sql Server实用操作小技巧集合.
.使用特殊数据.
.如何手动删除 SQL Server 2000 默.
.关于SQL Server 2000对XML支持的.
.SQL Server 2008的新压缩特性.
.怎么导出SQL所有用户表的字段信息.

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

发表日期:2007-3-7


今天,手头上正在作的一个项目,在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在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语句的优化方式 人气:5477
下一篇:SQL SERVER2000中订阅与发布的具体操作 人气:4693
浏览全部SQL Server的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐