在这里,我在查询的海量数据的内部联接上遇到性能问题,如下所示:
例子:
/* Creating table */
create table xyz
(
colp1 nvarchar(10),
colp2 nvarchar(10),
coldt date,
coltm datetime,
coldr integer
);
/* Inserting records */
insert into xyz values('A','B','2014-08-02','10:00:00',50);
insert into xyz values('A','C','2014-08-02','11:08:08',120);
insert into xyz values('A','B','2014-08-02','11:08:55',160);
insert into xyz values('A','D','2014-08-03','09:00:15',180);
insert into xyz values('A','E','2014-08-04','11:00:10',600);
insert into xyz values('A','F','2014-08-04','11:05:50',320);
.
.
upto 50000
/* Query */
declare @testtable table(dt date,st time,et time)
insert into @testtable select coldt,coltm,DATEADD(ss,coldr,coltm) from xyz
select distinct colp1,colp2,coldt,
coltm from xyz as x
inner join
@testtable as t on convert(varchar,x.coltm,108) > t.st and
convert(varchar,x.coltm,108)< t.et;
获得大量时间来执行上述查询以获取海量数据。
尝试将查询更改为
create TABLE #testtable (dt date,st time,et time)
insert into #testtable select coldt,coltm,DATEADD(ss,coldr,coltm) from xyz
select distinct colp1,colp2,coldt,
coltm
INTO #tmp2
from xyz as x
inner join #testtable as t on convert(varchar,x.coltm,108) > t.st and
convert(varchar,x.coltm,108)< t.et;
这里的问题是使用表变量而不是临时表。这是用于比较表变量和临时表的文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句