在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在SQL 语句上下功夫。 .:!*/s
z]mNAPRWM:
在生成报表时,客户感觉太慢,于是,各处检查,看可否提示效率。界面上的都改进了,提升不大。如是在SQL 语句上下功夫。 .:!*/s
z]mNAPRWM:
先在网上Google搜索“Join 与 Where 效率”以及察看SQL Server 帮助文档,希望能获得“捷径”些的优化思路。 L J >g?*
0B35
搜索的结果是,各大论坛。总体上总结出来时说:对小数据量(<N万)的来说效率几乎无差异,更有说法说Inner join 和Where只是SQL标准不同,在查询分析器中SQL Server查询分析器是将Where直接转换为Join后查询的。 wFt ^0
ol1;,UyggV
ZMAI lv~I
如是有了如下比较结果(均在查询分析器中查询和计时): Ja"d<
s=W$^|&[qQ
语句(1) 7\0GAm%
IgXS_we
declare @operatorName nvarchar(50) $; ,W]W]
zxJa4~DfH
set @operatorName = '%' VauhiD
+e?.3
select distinct item.* from item , customer_item , customer_operator ,operator >y&A^z'
X-6duzp%
where item.itemcode = customer_item.itemCode @g" ~KW
U WXq{B_
and customer_item.customerCode = customer_operator.customerCode Oy[M*;5jo
N7i04iPY
and customer_operator.operatorId = customer_operator.operatorId ]uR_` k,
L[wJ.3hU
and operator.operatorName like @operatorName oN>RHu {Q
ZYm";K^
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0 ZITxwD6z9
jc!!ot[#_
查询结果,74行,共时间0:00:04 |(/#J "
Gp%5P}`
语句(2) #u3i.]lr="
&X3U Y*p
declare @operatorName nvarchar(50) \n7(a4n<_
|lkj`yB6
set @operatorName = '%' 4PjzV2cg I
FmKj%7^
select distinct item.* from item inner join customer_item B-A( #
#037]BI
on item.itemcode = customer_item.itemCode xpLus
|(D3c 0}PT
inner join customer_operator on customer_item.customerCode = customer_operator.customerCode GnyK<^![A
N,>':@\
inner join operator on customer_operator.operatorId = operator.operatorId jbIM:
Q7WVF{,5
where operator.operatorName like @operatorName `Vl`$` )
OP^,h_sK
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0 "Bbt:b
n,= v4BGM
共74行,时间0:00:01 V?gJaemA[*
Von,yp
ikdpv|&:`
后检查发现语句(1)中有一个重复自查询条件 :customer_operator.operatorId = customer_operator.operatorId nm}}8g~
SZ sEXv7
将其叶加到语句(2)中。 zD-iUPle
x+}?W)GoB