视图运行缓慢,我必须使用 select into 临时表吗?

发布于 2024-11-02 09:48:35 字数 1212 浏览 3 评论 0原文

所以我遇到了一个严重的麻烦,因为我的最后一个查询执行时间是 8 分钟才能获取 2500 个节点。它只是其他两种视图的组合。

第一个视图是:

SELECT
   RecTime, SQL AS str, ID, 
   ROW_NUMBER() OVER(ORDER BY RecTime,ID) AS rwnb 
FROM         
   (SELECT
       A.RecTime, X.SQL, X.ID
    FROM 
       dbo.EventView AS A 
    CROSS JOIN
       dbo.Incident AS X
    WHERE      
       (PATINDEX('%' + A.Col + '%', X.SQL) > 0)) AS INC

1 秒和 1600 个节点

第二个视图是:

SELECT     
   D.RecTime, D.Event, D.ID, CAST(CASE WHEN X.[Value] IS NULL THEN 0 ELSE X.[Value] END AS bit) AS Value
FROM         
   dbo.XDependencedEvents AS D 
INNER JOIN
    dbo.EventView AS X ON X.Col = D.Event 
                          AND D.RecTime BETWEEN X.RecTime AND X.ChangedDate

3 秒执行时间和 2100 个节点

,最终视图是

SELECT 
    X.[Rectime], X.[ID], X.[str], D.[Event], D.[Value],X.[rwnb] 
FROM 
    [XDependencedIncidents] AS X
INNER JOIN [XEventStates] AS D ON X.[Rectime] = D.[Rectime]
                                  AND X.[ID] = D.[ID]

8 分钟和 2500 个节点。

我什至使用 RowNumber AS rwnb 来加快视图的处理速度,但它仍然很慢。我必须对前 2 个视图使用 select into 临时表吗?或者我在这里做错了什么?

最终目的是每次“事件”发生变化时获取某些“事件”“组”的值

So I've got a serious trouble because my last query execute time is 8 minutes to get 2500 nodes. And it just combination of 2 other views.

First view is :

SELECT
   RecTime, SQL AS str, ID, 
   ROW_NUMBER() OVER(ORDER BY RecTime,ID) AS rwnb 
FROM         
   (SELECT
       A.RecTime, X.SQL, X.ID
    FROM 
       dbo.EventView AS A 
    CROSS JOIN
       dbo.Incident AS X
    WHERE      
       (PATINDEX('%' + A.Col + '%', X.SQL) > 0)) AS INC

1 second and 1600 nodes

Second view is :

SELECT     
   D.RecTime, D.Event, D.ID, CAST(CASE WHEN X.[Value] IS NULL THEN 0 ELSE X.[Value] END AS bit) AS Value
FROM         
   dbo.XDependencedEvents AS D 
INNER JOIN
    dbo.EventView AS X ON X.Col = D.Event 
                          AND D.RecTime BETWEEN X.RecTime AND X.ChangedDate

3 seconds execute time and 2100 nodes

and final view is

SELECT 
    X.[Rectime], X.[ID], X.[str], D.[Event], D.[Value],X.[rwnb] 
FROM 
    [XDependencedIncidents] AS X
INNER JOIN [XEventStates] AS D ON X.[Rectime] = D.[Rectime]
                                  AND X.[ID] = D.[ID]

8 minutes and 2500 nodes.

I even use RowNumber AS rwnb to speed up working with views but it still being really slow. Must I use select into temp tables for first 2 views ? Or I making wrong something here ?

The final purpose is to getting values for some "groups" of "events" for each time when "events" changing

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

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

发布评论

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

评论(2

狠疯拽 2024-11-09 09:48:35

试试这个:

SELECT X.[Rectime] , X.[ID] , X.[str], D.[Event], D.[Value],X.[rwnb] 
FROM [XDependencedIncidents] AS X
INNER JOIN [XEventStates] AS D ON X.[ID] = D.[ID]
WHERE X.[Rectime] = D.[Rectime]

如果它也很慢。检查XEventStates 索引

try this:

SELECT X.[Rectime] , X.[ID] , X.[str], D.[Event], D.[Value],X.[rwnb] 
FROM [XDependencedIncidents] AS X
INNER JOIN [XEventStates] AS D ON X.[ID] = D.[ID]
WHERE X.[Rectime] = D.[Rectime]

If its also slow. Check the XEventStates Indexing.

淤浪 2024-11-09 09:48:35

我奇怪的解决方案是使用 RowNumber 来修改第二个和最后一个视图,例如:

SELECT     D.RecTime, D.Event, D.ID
,CAST(CASE WHEN X.[Value] IS NULL THEN 0 ELSE X.[Value] END AS bit) AS Value
,ROW_NUMBER() OVER(ORDER BY D.RecTime,D.ID) AS rwnb 
FROM         dbo.XDependencedEvents AS D 
    INNER JOIN
    dbo.EventView AS X 
        ON X.Col = D.Event 
        AND D.RecTime BETWEEN X.RecTime AND X.ChangedDate

SELECT X.[Rectime] , X.[ID] , X.[str], D.[Event], D.[Value],X.[rwnb], D.[rwnb]
FROM [XDependencedIncidents] AS X
INNER JOIN [XEventStates] AS D ON X.[ID] = D.[ID]
AND X.[Rectime] = D.[Rectime]

对这种奇怪的优化方法有何看法?如果有的话,我真的想要一个更好的解决方案。

顺便说一句,是的,添加行号使得 8 分钟 -> 5秒。

My weird solution is using RowNumber like modify 2nd and last Views like :

SELECT     D.RecTime, D.Event, D.ID
,CAST(CASE WHEN X.[Value] IS NULL THEN 0 ELSE X.[Value] END AS bit) AS Value
,ROW_NUMBER() OVER(ORDER BY D.RecTime,D.ID) AS rwnb 
FROM         dbo.XDependencedEvents AS D 
    INNER JOIN
    dbo.EventView AS X 
        ON X.Col = D.Event 
        AND D.RecTime BETWEEN X.RecTime AND X.ChangedDate

and

SELECT X.[Rectime] , X.[ID] , X.[str], D.[Event], D.[Value],X.[rwnb], D.[rwnb]
FROM [XDependencedIncidents] AS X
INNER JOIN [XEventStates] AS D ON X.[ID] = D.[ID]
AND X.[Rectime] = D.[Rectime]

What do you think about this weird method of optimization ? I really want a better solution if there some.

BTW yes, adding row number makes from 8 minutes -> 5 seconds.

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