G
G
Gotoh2021-10-11 15:56:14
SQL
Gotoh, 2021-10-11 15:56:14

Is there any difference in these SQL queries with join?

There are two queries that differ in condition. The difference lies in the selection of the table in the condition. Whether there is a difference on what table to write the filter in this case? Can such a difference affect performance in some cases?
I looked at the query plans, there was no difference, perhaps the server at the optimization stage will choose the most suitable option itself if, for example, one table has an index, and the other does not.

SELECT *
  FROM table1
  join table2 on table1.id = table2.table1_id
  where table1.id > 2  -- фильтруем по table1


SELECT *
  FROM table1
  join table2 on table1.id = table2.table1_id
  where table2.table1_id > 2 -- фильтруем по table2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-10-11
@gotoh

There will be no difference in the result.
There should be no difference in performance if the query optimizer worked correctly.
On simple queries like this, it usually does a great job, but the more complex the query, the more it makes sense to chew it up in more detail, specify the correct indexes, and all that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question