我有2個表Person_Organization
和Person_Organization_other
和嵌套查詢:加入查詢花費更多的執行時間比其相應的嵌套查詢
SELECT
Person_Organization_id
FROM
Person_Organization_other
WHERE
company_name IN (SELECT company_name
FROM Person_Organization_other
WHERE Person_Organization_id IN (SELECT Person_Organization_Id
FROM Person_Organization
WHERE person_id = 117
AND delete_flag = 0)
)
鑑於上面的查詢與聯接我試過相應的查詢是: -
SELECT
poo.Person_Organization_id
FROM
Person_Organization_other poo, Person_Organization_other poo1, Person_Organization po
WHERE
poo1.Person_Organization_id = po.Person_Organization_Id
AND po.person_id = 117
AND po.delete_flag = 0
AND poo.company_name = poo1.company_name
GROUP BY
poo.Person_Organization_id
但是嵌套查詢發現花費更少的時間相比,其相應的查詢與連接。我使用SQL事件探查器跟蹤來比較執行的查詢的時間。對於嵌套查詢需要30 ms。對於連接查詢花了41多毫秒
我的印象是,作爲一項規則嵌套查詢不太perfomant,應該是在「夷爲平地」使用連接。
有人能解釋我做錯了嗎?
問候 尼廷