【发布时间】:2011-05-13 08:05:05
【问题描述】:
我有一个运行速度很慢的存储过程。因此,我想在单独的视图中提取一些查询。
我的代码如下所示:
DECLARE @tmpTable TABLE(..)
INSERT INTO @tmpTable (..) *query* (returns 3000 rows)
Select ... from table1
inner join table2
inner join table3
inner join @tmpTable
...
然后我提取(复制粘贴)*查询*并将其放入视图中 - 即 vView。
这样做会给我一个不同的结果:
Select ... from table1
inner join table2
inner join table3
inner join vView
...
为什么?我可以看到 vView 和 @tmpTable 都返回 3000 行,所以它们应该匹配(也做了一个 except 查询来检查)。
任何 cmets 都会非常感激,因为我对此感到非常困惑..
编辑:
这是获取结果的完整查询(使用@tmpTable 或 vView 会给我不同的结果,尽管看起来相同):
select dep.sid as depsid, dep.[name], COUNT(b.sid) as possiblelogins, count(ls.clientsid) as logins
from department dep
inner join relationship r on dep.sid=r.primarysid and r.relationshiptypeid=27 and r.validto is null
inner join [user] u on r.secondarysid=u.sid
inner join relationship r2 on u.sid=r2.secondarysid and r2.validto is null and r2.relationshiptypeid in (1,37)
inner join client c on r2.primarysid=c.sid
inner join ***@tmpTable or vView*** b on b.sid = c.sid
left outer join (select distinct clientsid from logonstatistics) as ls on b.sid=ls.clientsid
GROUP BY dep.sid, dep.[name],dep.isdepartment
HAVING dep.isdepartment=1
【问题讨论】:
-
你能提供更多的代码和信息吗? @tmpTable 查询上有 WHERE 吗? JOIN 有什么不同吗?是“更多行”还是“重复”?
-
我已经添加了选择本身,但是用于构建@tmpTable 和vView 的查询是一个沉重的查询,其中包含内部连接、联合等,但结果集是不同的。
-
请插入带有定义的@tmpTable 代码?