中Innerjoin和where的效率差异

天,手头上正在作的一个项目,在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在SQL语句上下功夫。(我这人比较懒,对简单的语句和查询都没有经过仔细优化的,一般只对姚使用leftjoin,outerjoin,groupby以及carsor的语句会仔细写并用理论考虑和检查---因为这种语句一般测试时如果发现错误,检查和调试很麻烦)先在网上Google搜索“Join与 where效率”以及察看SQLServer帮助文档,希望能获得“捷径”些的优化思路。搜索的结果是,各大论坛,包括MSDN上很多人提出了这个问题,但回答是众说纷纭。总体上总结出来时说:对小数据量(

语句(1)declare@operatorNamenvarchar(50)set@operatorName='%'selectdistinctitem.*fromitem,customer_item,customer_operator,operator whereitem.itemcode=customer_item.itemCodeandcustomer_item.customerCode=customer_operator.customerCodeandcustomer_operator.operatorId=customer_operator.operatorIdandoperator.operatorNamelike@operatorNameanditem.deleted=0andcustomer_item.deleted=0andcustomer_operator.deleted=0查询结果,74行,共时间0:00:04语句(2)declare@operatorNamenvarchar(50)set@operatorName='%'selectdistinctitem.*fromitem Innerjoincustomer_itemonitem.itemcode=customer_item.itemCode Innerjoincustomer_operatoroncustomer_item.customerCode=customer_operator.customerCode Innerjoinoperatoroncustomer_operator.operatorId=operator.operatorId [email protected]=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.*fromitem Innerjoincustomer_itemonitem.itemcode=customer_item.itemCode Innerjoincustomer_operatoroncustomer_item.customerCode=customer_operator.customerCode Innerjoinoperatoroncustomer_operator.operatorId=operator.operatorId [email protected]=0andcustomer_item.deleted=0andcustomer_operator.deleted=0andcustomer_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.itemCodeandcustomer_item.customerCode=customer_operator.customerCode--andcustomer_operator.operatorId=customer_operator.operatorIdandoperator.operatorNamelike@operatorNameanditem.deleted=0andcustomer_item.deleted=0andcustomer_operator.deleted=0时间和上一页 

你可能感兴趣的:(数据库,sqlserver,join,优化,sql,google,报表)