连接表变量与连接视图

发布于 2024-11-07 03:51:58 字数 1287 浏览 0 评论 0原文

我有一个运行速度相当慢的存储过程。因此我想在单独的视图中提取一些查询。

我的代码看起来像这样:

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 查询来检查)。

任何评论都会非常感激,因为我觉得自己对此很困惑。

编辑:

这是获取结果的完整查询(使用 @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

I have a stored procedure which is running quite slow. Therefore I want to extract some of the query in a separate view.

My code looks something like this:

DECLARE @tmpTable TABLE(..)

INSERT INTO @tmpTable (..) *query* (returns 3000 rows)

Select ... from table1
inner join table2
inner join table3
inner join @tmpTable
...

I then extract (copy-paste) the *query* and put it in a view - i.e. vView.

Doing this will then give me a different result:

Select ... from table1
    inner join table2
    inner join table3
    inner join vView
    ...

Why? I can see that the vView and the @tmpTable both returns 3000 rows, so they should match (also did a except query to check).

Any comments would be much appriciated as I feel quite stuck with this..

EDITED:

This is the full query for getting the result (using @tmpTable or vView gives me different results, although the appear the same):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2024-11-14 03:51:58

如果您更改为此,您可能不需要视图/表。

它连接到 client c 并且似乎只是为了连接到 logonstatistics

--remove inner join ***@tmpTable or vView*** b on b.sid = c.sid
--change JOIN
left outer join (select distinct clientsid from logonstatistics) as ls on c.sid=ls.clientsid

并将 COUNT(b.sid) 更改为 COUNT (c.sid) 在 SELECT 子句中

否则,如果得到不同的结果,我可以看到两个选项:

  1. 表和视图有不同的数据。你逐行比较过吗?
  2. 一个有 NULL,一个有值(特别是对于 sid 列,它会影响 JOIN)

最后,当您说“不同的结果”时,您的意思是您得到 x2 或 x3 行吗?不同的 COUNT?什么?

You maybe don't need the view/table if you change to this.

It joins on to client c and appears to be there only to JOIN onto logonstatistics

--remove inner join ***@tmpTable or vView*** b on b.sid = c.sid
--change JOIN
left outer join (select distinct clientsid from logonstatistics) as ls on c.sid=ls.clientsid

And change COUNT(b.sid) to COUNT(c.sid) in the SELECT clause

Otherwise, if you get different results you have two options I can see:

  1. Table and view have different data. Have you run a line by line comparsion?
  2. One has NULL, one has a value (especially for the sid column which will affect the JOIN)

Finally, when you says "different results" do you mean you get x2 or x3 rows? A different COUNT? What?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文