我怎樣才能得到這些子查詢的rif?如何用聯接替換子查詢?
(所有表都列上創建和LastEdited作爲時間戳)
table Process
- ID
- Title
table ProcessHistory
- ID
- ProcessID
- HistoryID
table History
- ID
- Title (new, open, closed etc.)
當我試圖讓進程列表與上次狀態稱號的cols我做的:
SELECT DISTINCT Process.*, History.Title AS HistoryTitle, History.ID AS HistoryID
FROM `Process`
LEFT JOIN ProcessHistory AS ProcessHistory ON Process.ID=ProcessHistory.ProcessID
LEFT JOIN History AS History ON HistoryID=ProcessHistory.HistoryID
WHERE History.ID = (
SELECT HistoryID FROM ProcessHistory
WHERE ProcessID=Process.ID
ORDER BY ProcessHistory.ID DESC LIMIT 1
)
GROUP BY Process.ID
ORDER BY Process.ID DESC LIMIT 0, 100
當我嘗試獲取按特定狀態過濾的列表 (其中最新HistoryID爲1 - 「所有打開的過程」)
SELECT DISTINCT Process.*, History.Title AS HistoryTitle, History.ID AS HistoryID
FROM `Process`
LEFT JOIN ProcessHistory AS ProcessHistory ON Process.ID=ProcessHistory.ProcessID
LEFT JOIN History AS History ON HistoryID=ProcessHistory.HistoryID
WHERE History.ID=(
SELECT HistoryID FROM ProcessHistory
WHERE HistoryID =1
AND ProcessID=Process.ID ORDER BY ProcessHistory.ID DESC LIMIT 1
)
GROUP BY Process.ID
ORDER BY Process.ID DESC LIMIT 0, 100
由於性能原因,我想擺脫這些子查詢? 我如何替換子查詢?
在此先感謝!
由您創建和加載的sqlfiddle將幫助我們幫助您。注意:加載並不意味着完全加載。這可能意味着每個表格有5到10行數據。 – Drew
這裏的sql小提琴: http://sqlfiddle.com/#!9/3d9001/1 – derRobert
它真的是性能瓶頸還是這是不成熟的,甚至可能是非優化的優化? – e4c5