我还需要帮助加快这个查询,在 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