0

我还需要帮助加快这个查询,在 LIVE db 上需要 25 分钟,在 TEST db 上需要 1 秒。

这是经过一些修改后,最初我查询数据库服务器数百次以使用主查询的每一行的 while 循环从 php 页面获取数据,然后我尝试使用临时表创建一个程序来返回数据,我在 45 分钟后取消了执行。所以我尝试了这个。

我想可以对“从 NurQueryResults 中选择前 1 个”查询进行内部连接,但我无法弄清楚。我得到了结果,但不是最近的结果,我做了一个“按 t1.time、t2.time、t3.time 排序......”虽然它确实有 t1 最近的结果在顶部,但它返回了更多的结果比它应该有的。

SELECT o.VisitID                                          AS VisitID,
       (SELECT TOP 1 Response
        FROM   NurQueryResults
        WHERE  QueryID = 'OEDTCAT'
               AND VisitID = o.VisitID
        ORDER  BY DateTime DESC)                          AS PPN,
       (SELECT TOP 1 Response
        FROM   NurQueryResults
        WHERE  QueryID = 'OEDTMEAT'
               AND VisitID = o.VisitID
        ORDER  BY DateTime DESC)                          AS MEAT,
       (SELECT TOP 1 Response
        FROM   OeOrderQueries
        WHERE  QueryID = 'OESPMOD'
               AND VisitID = o.VisitID
        ORDER  BY RowUpdateDateTime DESC)                 AS SPMOD,
       (SELECT TOP 1 Response
        FROM   OeOrderQueries
        WHERE  QueryID = 'OERT3'
               AND VisitID = o.VisitID
        ORDER  BY RowUpdateDateTime DESC)                 AS SPMOD2,
       (SELECT TOP 1 Response
        FROM   NurQueryResults
        WHERE  QueryID = 'OEDTDECUB'
               AND VisitID = o.VisitID
        ORDER  BY DateTime DESC)                          AS DECUB,
       (SELECT TOP 1 Response
        FROM   NurQueryResults
        WHERE  QueryID = 'OEALL2'
               AND VisitID = o.VisitID
        ORDER  BY DateTime DESC)                          AS FOODALL,
       o.OrderedProcedureName,
       o.OrderDateTime,
       a.RoomID,
       a.BedID,
       a.Name,
       a.Sex,
       DATEDIFF(year, a.ComputedBirthDateTime, GETDATE()) AS Age
FROM   OeOrders o
       INNER JOIN AdmVisits a
         ON o.VisitID = a.VisitID
            AND o.Category = 'DIET'
            AND o.StatusChoice = 'S'
            AND a.Status = 'ADM IN'
ORDER  BY o.VisitID,
          o.OrderDateTime DESC 
4

2 回答 2

0

打开执行计划并查看结果。它可能表明您缺少索引或类似性质的东西。说到索引,检查所涉及索引的碎片,如果它们很高,请考虑重建它们。我最近取得了相当大的成功,通过遵循上述过程,在生产中运行需要 2 分钟的程序在 1-3 秒内执行。

于 2012-11-30T23:48:31.393 回答
0

我只是想知道,当您执行以下操作时,结果会如何:

SELECT
    o.VisitID AS VisitID,
    PPN.Response
FROM OeOrders o
JOIN AdmVisits a
    ON o.VisitID = a.VisitID
    AND o.Category = 'DIET'
    AND o.StatusChoice = 'S'
    AND a.Status = 'ADM IN'
join
(
    SELECT 
        Response, 
        VisitID, 
        ROW_NUMBER() OVER(ORDER BY DateTime DESC) AS MyRowNumber 
    FROM NurQueryResults 
    WHERE QueryID = 'OEDTCAT'
) AS PPN
    on o.VisitID = PPN.VisitID
    and PPN.MyRowNumber = 1

这不是您的整个查询,而只是获取列 PPN 的第一个子查询。

如果像这个 JOIN 一样更改所有子查询,它是否有任何性能提升?

于 2012-11-16T14:59:52.880 回答