1. 问题1,列数对数据的影响,下面两个查询生成的数据大小是一样的,但列数不一样,但效率差别却很大:--测试数据
create table tb(a nchar)
insert tb select '0'
union all select '1'
union all select '3'
go--查询1
select a=a.a+b.a+c.a+d.a+e.a+f.a
+g.a+h.a+i.a+j.a+k.a+l.a
+m.a
from tb a,tb b,tb c,tb d,tb e,tb f
,tb g,tb h,tb i,tb j,tb k,tb l
,tb m--查询2
select a1=a.a,a2=b.a,a3=c.a,a4=d.a,a5=e.a,a6=f.a
,a7=g.a,a8=h.a,a9=i.a,a10=j.a,a11=k.a,a12=l.a
,a13=m.a
from tb a,tb b,tb c,tb d,tb e,tb f
,tb g,tb h,tb i,tb j,tb k,tb l
,tb m所以一般来说,要避免将不关联的数据组织在同一个表中,或者尽量避免将不同时处理的数据放在同一个表中.
create table tb(a nchar)
insert tb select '0'
union all select '1'
union all select '3'
go--查询1
select a=a.a+b.a+c.a+d.a+e.a+f.a
+g.a+h.a+i.a+j.a+k.a+l.a
+m.a
from tb a,tb b,tb c,tb d,tb e,tb f
,tb g,tb h,tb i,tb j,tb k,tb l
,tb m--查询2
select a1=a.a,a2=b.a,a3=c.a,a4=d.a,a5=e.a,a6=f.a
,a7=g.a,a8=h.a,a9=i.a,a10=j.a,a11=k.a,a12=l.a
,a13=m.a
from tb a,tb b,tb c,tb d,tb e,tb f
,tb g,tb h,tb i,tb j,tb k,tb l
,tb m所以一般来说,要避免将不关联的数据组织在同一个表中,或者尽量避免将不同时处理的数据放在同一个表中.
这就是为什么很多书上建议把经常使用的查询做成存储过程或者视图(因为这两个会保存执行计划)
因此,第一次执行查询的时候包含了编译和数据检索两个时间,第二次及以后的查询仅包含数据检索时间,测试查询执行时间以那个为标准,那就看你自己的了.
declare @ti datetime
select @ti = getdate()
select a=a.a+b.a+c.a+d.a+e.a+f.a
+g.a+h.a+i.a+j.a+k.a+l.a
+m.a
from tb a,tb b,tb c,tb d,tb e,tb f
,tb g,tb h,tb i,tb j,tb k,tb l
,tb m
select datediff (ms,@ti,getdate()),@@rowcountselect @ti= getdate()
select a1=a.a,a2=b.a,a3=c.a,a4=d.a,a5=e.a,a6=f.a
,a7=g.a,a8=h.a,a9=i.a,a10=j.a,a11=k.a,a12=l.a
,a13=m.a
from tb a,tb b,tb c,tb d,tb e,tb f
,tb g,tb h,tb i,tb j,tb k,tb l
,tb m
select datediff (ms,@ti,getdate()),@@rowcount第一条的使用时间10043
第二条的使用时间21703
真没有想到相差这么大!