优化 InterBase 查询

发布于 2024-10-07 01:44:34 字数 846 浏览 10 评论 0原文

SELECT
  AI_636.PARENT_ID AS PART,
  MAX(b.AP_1036) AS ESTEND,
  MAX(a.AP_3222) AS ACTEND
FROM
  AI_636
  LEFT OUTER JOIN AI_665 a
    ON
    (
      a.AP_1033 = AI_636.PARENT_ID
      AND SUBSTR(a.AP_1028, 1, 4) >= '2000'
      AND a.AP_1030 NOT IN ('994')
      AND
      (
        a.AP_1033 NOT IN
        (
          SELECT AI_665.AP_1033 FROM AI_665 WHERE AI_665.AP_3222 IS NULL
        )
      )
    )
  JOIN AI_665 b
    ON
    (
      b.AP_1033 = AI_636.PARENT_ID
      AND SUBSTR(b.AP_1028, 1, 4) >= '2000'
    )
  GROUP BY AI_636.PARENT_ID

该查询只是较大查询的一小部分,它会导致整个调用执行速度非常慢。

基本上,有一个父操作,然后在其下面有几个子操作。操作的估计结束日期与实际结束日期仅存储在子级别,因此为了导出父级别的结束日期,我试图找到子级别的最大日期。我遇到的问题是,当子操作未完成时,它的结束日期为 NULL,而 MAX() 函数会忽略这些。我通过将子操作表连接到自身并将其缩小到仅包含其同级都具有非 NULL 结束日期的子操作来解决此问题。

有什么方法可以优化对具有非 NULL 结束日期的子项的父操作的搜索吗?

SELECT
  AI_636.PARENT_ID AS PART,
  MAX(b.AP_1036) AS ESTEND,
  MAX(a.AP_3222) AS ACTEND
FROM
  AI_636
  LEFT OUTER JOIN AI_665 a
    ON
    (
      a.AP_1033 = AI_636.PARENT_ID
      AND SUBSTR(a.AP_1028, 1, 4) >= '2000'
      AND a.AP_1030 NOT IN ('994')
      AND
      (
        a.AP_1033 NOT IN
        (
          SELECT AI_665.AP_1033 FROM AI_665 WHERE AI_665.AP_3222 IS NULL
        )
      )
    )
  JOIN AI_665 b
    ON
    (
      b.AP_1033 = AI_636.PARENT_ID
      AND SUBSTR(b.AP_1028, 1, 4) >= '2000'
    )
  GROUP BY AI_636.PARENT_ID

This query is a small part of a larger one and it causes the entire call to execute very slowly.

Basically, there is a parent operation and then several child operations underneath it. The estimated vs actual end dates of the operations are only stored at the child level, so to derive one for the parent level, I am trying to find the largest dates at the child level. The problem I run into is when a child operation is not completed, it has a NULL end date, and the MAX() function ignores these. I am getting around this by joining the child operations table to itself and narrowing it down to only include child operations whose siblings all have non-NULL end dates.

Is there any way I can optimize the search for parent operations with children with non-NULL end dates?

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

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

发布评论

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

评论(2

滥情空心 2024-10-14 01:44:34

我上次的回答有一个错误,但这也许会有所帮助:

AND NOT EXISTS (SELECT NULL
                FROM   AI_665 a2
                WHERE  a2.AP_1033 = a.AP_1033
                AND    a2.AP_3222 IS NULL)

My last answer had a mistake, but maybe this will help:

AND NOT EXISTS (SELECT NULL
                FROM   AI_665 a2
                WHERE  a2.AP_1033 = a.AP_1033
                AND    a2.AP_3222 IS NULL)
一紙繁鸢 2024-10-14 01:44:34

您必须创建降序索引以加速 MAX()

You must create an descending index to speed up the MAX()

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