优化选择查询(内部选择+组)

发布于 2024-11-08 11:59:17 字数 617 浏览 2 评论 0原文

我当前的版本是:

SELECT     DT, AVG(DP_H2O) AS Tx,
           (SELECT    AVG(Abs_P) / 1000000 AS expr1
           FROM       dbo.BACS_MinuteFlow_1
           WHERE      (DT =
                                     (SELECT     MAX(DT) AS Expr1
                                       FROM      dbo.BACS_MinuteFlow_1
                                       WHERE     DT <= dbo.BACS_KongPrima.DT ))
          GROUP BY DT) AS Px
  FROM    dbo.BACS_KongPrima
  GROUP BY DT

但运行速度非常慢。

基本上在内部选择中,我选择最接近我的时间的时间,然后按最近的时间分组。

有可能的优化吗?也许我可以以某种方式加入它,但麻烦的是我不知道如何按最近的日期进行分组。

谢谢

My current version is :

SELECT     DT, AVG(DP_H2O) AS Tx,
           (SELECT    AVG(Abs_P) / 1000000 AS expr1
           FROM       dbo.BACS_MinuteFlow_1
           WHERE      (DT =
                                     (SELECT     MAX(DT) AS Expr1
                                       FROM      dbo.BACS_MinuteFlow_1
                                       WHERE     DT <= dbo.BACS_KongPrima.DT ))
          GROUP BY DT) AS Px
  FROM    dbo.BACS_KongPrima
  GROUP BY DT

but it works very slow.

basically in inner select I'm selecting maximum near time to my time, then group by this nearest time.

Is there possible optimizations ? Maybe I can join it somehow , but the trouble I'm not sure how to group by this nearest date.

Thank you

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

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

发布评论

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

评论(1

哥,最终变帅啦 2024-11-15 11:59:17

您可以尝试通过交叉应用重新排列它以使用下面的代码。我不确定这是否会提高性能,但通常我会不惜一切代价避免在特定列上使用查询,并且 SQL Server 非常擅长优化 Apply 语句。

WITH  Bacs_MinuteFlow_1  (Abs_P ,DT ) AS
(SELECT 5.3,'2011/10/10'
    UNION SELECT 6.2,'2011/10/10'
    UNION SELECT 7.8,'2011/10/10'
    UNION SELECT 5.0,'2011/03/10'
    UNION SELECT 4.3,'2011/03/10'),
BACS_KongPrima (DP_H2O ,DT)AS
(SELECT 2.3,'2011/10/15'
    UNION SELECT 2.6,'2011/10/15'
    UNION SELECT 10.2,'2011/03/15')

SELECT DT, AVG(DP_H2O) AS Tx,
       a.Px

FROM    BACS_KongPrima
CROSS APPLY  
(
SELECT AVG(Abs_P) / 1000000 AS Px 
FROM BACS_MinuteFlow_1 
WHERE DT =
    (SELECT     MAX(DT) AS maxdt 
     FROM      BACS_MinuteFlow_1
     WHERE     DT <= BACS_KongPrima.DT
    )
) a
GROUP BY DT,a.Px

干杯

You could try to rearrange it to use the code below using a cross apply. Am not sure if this will improve performance but generally I try to avoid at all costs using a query on a specific column and SQL Server is pretty good at optimising the Apply statement.

WITH  Bacs_MinuteFlow_1  (Abs_P ,DT ) AS
(SELECT 5.3,'2011/10/10'
    UNION SELECT 6.2,'2011/10/10'
    UNION SELECT 7.8,'2011/10/10'
    UNION SELECT 5.0,'2011/03/10'
    UNION SELECT 4.3,'2011/03/10'),
BACS_KongPrima (DP_H2O ,DT)AS
(SELECT 2.3,'2011/10/15'
    UNION SELECT 2.6,'2011/10/15'
    UNION SELECT 10.2,'2011/03/15')

SELECT DT, AVG(DP_H2O) AS Tx,
       a.Px

FROM    BACS_KongPrima
CROSS APPLY  
(
SELECT AVG(Abs_P) / 1000000 AS Px 
FROM BACS_MinuteFlow_1 
WHERE DT =
    (SELECT     MAX(DT) AS maxdt 
     FROM      BACS_MinuteFlow_1
     WHERE     DT <= BACS_KongPrima.DT
    )
) a
GROUP BY DT,a.Px

Cheers

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