Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between where and join?

Tags:

sql

postgresql

I have two identical queries. In first I use Where

SELECT dings.id, doorbots.id
FROM dings
INNER JOIN doorbots ON dings.doorbot_id = doorbots.id
WHERE doorbots.id = 1615131 AND 
      deleted_at is NULL
ORDER BY dings.created_at;

In second I put all filters in join

SELECT dings.id, doorbots.id
FROM dings
INNER JOIN doorbots ON dings.doorbot_id = doorbots.id AND 
           doorbots.id = 1615131 AND 
           deleted_at is NULL
ORDER BY dings.created_at;

I have a composite index: doorbot_id_idx btree (doorbot_id) WHERE deleted_at IS NULL

Is there any performance difference between this two queries? What is the best way to do?


1 Answers

It is very likely that both approaches will give a closer performance, if not the same. Let the query planner do the dirty job. :)

The question is to maintain a good readability in your SQL, so in this case you must:

  • Put filter expression on WHERE clause;
  • Put only joining expression on JOIN. That is, only the relationship between the tables goes here.
like image 154
Michel Milezzi Avatar answered Mar 12 '26 11:03

Michel Milezzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!