多个 SQL 计入多个条件

发布于 2024-10-28 09:12:02 字数 482 浏览 0 评论 0原文

我一直在尝试优化从数据库检索数据的方式,以便在软件开发的“仪表板”类型页面上显示

我的数据库结构如下:

  • 需求表,其中包含各个字段的所有各种需求,但重要的是REQ_ID 作为键。
  • 包含的任务表可以包含多个任务,其中包含 TASK_ID、TASK_NAME(DEV、TEST 或 RELEASE)、TASK_STATUS(未开始、完成、阻止)、TASK_WINDOW(任务完成时的 Week1、Week2、.. 等)和返回链接满足 REQ_I 的要求。例如,一个需求可能有多个开发任务,测试任务和发布任务,但只有当与需求相关的所有开发任务都完成时才能开发完成,否则它是不完整的

我想查询这两个表以提供我一个结果集,其中单独包含单个查询中每个 DEV 任务窗口的“DEV 完成”、“测试完成”和“发布完成”要求的数量。我目前正在执行多个查询,每个查询都包含子查询,然后使用 PHP 聚合结果,但是这总共需要 15 秒来执行,任何人都可以帮我将其合并到单个查询中吗?

I've been trying to optimise the way I retrieve data from my database for display on a "dashboard" type of page for software development

My database structure is as follows:

  • Requirements Table that contains all the various requirements with various fields, but importantly a REQ_ID as key.
  • Tasks Table that contains can contain multiple tasks with a TASK_ID, TASK_NAME (DEV, TEST OR RELEASE), TASK_STATUS (Not Started, Complete, Blocked), TASK_WINDOW (Week1, Week2, .. etc. when task was completed) and a link back to a requirement with REQ_I. For example, a requirement may have multiple dev tasks, test tasks and release tasks but for can only be dev complete if all the dev tasks related to a requirement is complete, otherwise it is incomplete

I would like to query these two tables to provide me a results set that contains individually the number DEV Complete, Test Complete and Release Complete requirements per DEV task window in a single query. I'm currently performing multiple query each containing subqueries and then aggregating the results with PHP, however this in total takes 15 sec to exec, Can anybody please help me in consolidating this into a single query>

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

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

发布评论

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

评论(2

ι不睡觉的鱼゛ 2024-11-04 09:12:02
SELECT r.REQ_ID, 
       SUM(CASE WHEN t.TASK_NAME = 'DEV' THEN 1 ELSE 0 END) AS DevComplete,
       SUM(CASE WHEN t.TASK_NAME = 'TEST' THEN 1 ELSE 0 END) AS TestComplete,
       SUM(CASE WHEN t.TASK_NAME = 'RELEASE' THEN 1 ELSE 0 END) AS ReleaseComplete
    FROM Requirements r
        INNER JOIN Tasks t
            ON r.REQ_ID = t.REQ_ID
    WHERE t.TASK_STATUS = 'Complete'
    GROUP BY r.REQ_ID
SELECT r.REQ_ID, 
       SUM(CASE WHEN t.TASK_NAME = 'DEV' THEN 1 ELSE 0 END) AS DevComplete,
       SUM(CASE WHEN t.TASK_NAME = 'TEST' THEN 1 ELSE 0 END) AS TestComplete,
       SUM(CASE WHEN t.TASK_NAME = 'RELEASE' THEN 1 ELSE 0 END) AS ReleaseComplete
    FROM Requirements r
        INNER JOIN Tasks t
            ON r.REQ_ID = t.REQ_ID
    WHERE t.TASK_STATUS = 'Complete'
    GROUP BY r.REQ_ID
听不够的曲调 2024-11-04 09:12:02

我意识到这是一个老问题,但我使用以下模式进行了测试:

模式 1:

SELECT
 [Count1] = SUM(CASE WHEN ... THEN 1 ELSE 0 END),
 [Count2] = SUM(CASE WHEN ... THEN 1 ELSE 0 END)
FROM
 [Table]
GROUP BY
 [Field]

模式 2:

SELECT
  [COUNT1] = (SELECT COUNT(*) FROM [Table] WHERE ...),
  [Count2] = (SELECT COUNT(*) FROM [Table] WHERE ...)

在我的例子中,运行两个查询时,模式 2 占用了 36 % 的时间,模式 1 占用了 64%。对我来说,模式 1 看起来更优雅,但它在我的场景中表现不佳。

I realize this is an old question but I ran a test with the following patterns:

Pattern 1:

SELECT
 [Count1] = SUM(CASE WHEN ... THEN 1 ELSE 0 END),
 [Count2] = SUM(CASE WHEN ... THEN 1 ELSE 0 END)
FROM
 [Table]
GROUP BY
 [Field]

Pattern 2:

SELECT
  [COUNT1] = (SELECT COUNT(*) FROM [Table] WHERE ...),
  [Count2] = (SELECT COUNT(*) FROM [Table] WHERE ...)

In my case, when running both queries, pattern 2 took 36 % of the time and pattern 1 took 64%. To me, pattern 1 looks more elegant, but it didn't perform nearly as well in my scenario.

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