K
K
KarambiG2021-07-26 23:52:38
MySQL
KarambiG, 2021-07-26 23:52:38

How to JOIN tables correctly?

Tell me good people. Is there a difference between the joins of these two rows in terms of database load?

SELECT... FROM `content` JOIN `likes`
SELECT... FROM `likes` JOIN `content`

I have such a question because I have a many-to-many relationship, and a third intermediate table has been created for this. Because it's better to be. Select an intermediate table, and join 2 others to it? Or is it better to join an intermediate one?

And a second similar question. If tables are joined like this:
ON content.id=likes.id_content, what is the best way to use WHERE content.id=$id or WHERE likes.id_content=$id ?

I understand that both there and there the result will be the same. But I mean is there a difference in terms of database load.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-07-27
@Akina

Is there a difference between the joins of these two rows in terms of database load?

Both queries are INNER JOIN. If STRAIGHT_JOIN is not used, then the server is guaranteed to build the same execution plan for both queries.
Moreover, if you read carefully how the server executes and optimizes queries, you will find out that the server actually results in both of these queries being a Cartesian product with selection. Or rather, preprocessing looks like the server is performing such a cast.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question