[英]Writing conditions on Where clause is better or ON clause?
哪個查詢是標准查詢和最佳查詢?
這個:
SELECT p.*
FROM posts p
JOIN favorites f ON p.id = f.post_id
WHERE f.user_id = ?
或這個:
SELECT p.*
FROM posts p
JOIN favorites f ON p.id = f.post_id
AND f.user_id = ?
兩種查詢均以標准方式編寫,幾乎可以在所有DBMS中使用 。 但是,如果您擔心性能,那么我會改用EXISTS
。
select p.*
from posts p
where exists (select 1
from favorites f
where p.id = f.post_id and f.user_id = ?
);
對我來說,在ON
子句或WHERE
子句中執行過濾器,只是樣式看起來如何查找INNER JOIN
。 因此,我將使用WHERE
子句:
SELECT p.*
FROM posts p INNER JOIN
favorites f
ON p.id = f.post_id
WHERE f.user_id = ?
ORDER BY f.date_time DESC;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.