Postgresql
子查詢還是內部聯接?
我有這個簡單的查詢,可以從屬於特定登錄名的帳戶中查找所有交易,並在交易中添加一些額外的條件。
SELECT t.id, t.date, t.amount, t.description FROM transaction t INNER JOIN account ac ON t.account = ac.id AND ac.login_id = ${loginId} WHERE t.processed = false AND t.updated_by_user = false AND t.category = 'uncategorized' ;
使用 上的子查詢,此查詢是否會執行得更快
accounts
,例如:SELECT t.id, t.date, t.amount, t.description FROM transaction t INNER JOIN (SELECT id FROM account WHERE login_id = ${loginId}) ac ON t.account = ac.id WHERE t.processed = false AND t.updated_by_user = false AND t.category = 'uncategorized' ;
我會感謝一些有見地的評論,在此先感謝!
INNER JOIN
盡可能使用,因為它更容易閱讀。否則它不應該有所作為。
您還可以使用 cte 的:
with definitely_better as (SELECT id FROM account WHERE login_id = ${loginId}) SELECT t.id, t.date, t.amount, t.description FROM transaction t INNER JOIN definitely better ac ON t.account = ac.id WHERE t.processed = false AND t.updated_by_user = false AND t.category = 'uncategorized' ;