我也需要幫助加快這個查詢,在LIVE db上花25min,在TEST db上花1秒。優化SQL查詢sql
這是經過一些修改後,最初我一直在查詢數據庫服務器幾百次以從主頁查詢的每一行使用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
如果你沒有創建一個,它會提高性能,你有關鍵的QueryID和VisitID的NurQueryResults索引嗎? – Farfarak
如果測試數據庫需要1秒鐘,則需要查看生產數據庫的不同之處。 – Paparazzi