I
I
Igor2021-06-11 10:19:18
MySQL
Igor, 2021-06-11 10:19:18

What is the fundamental difference between the two requests?

SELECT * FROM (
    SELECT MAX(c5_.id) AS sclr_32 FROM contacts_history c5_ 
    WHERE (c5_.organization_id = '1') GROUP BY c5_.contact_id
) tt 
JOIN contacts_history ch ON tt.sclr_32 = ch.id;

SELECT * FROM contacts_history WHERE id IN (
    SELECT MAX(c5_.id) AS sclr_32 FROM contacts_history c5_ WHERE (c5_.organization_id = '1') GROUP BY c5_.contact_id
);


Which of the queries will have the most degradation as the amount of data grows.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SagePtr, 2021-06-12
@SagePtr

Instead of guessing, you can look at the explain of both requests.
In extreme cases, generate test data and score in tables, measure after that with the query cache disabled.

C
ComodoHacker, 2021-06-11
@ComodoHacker

There is no fundamental difference.

G
grebenyukov, 2021-06-12
@grebenyukov

The number of columns will be different, because in the first case, select * is made from (tt join subquery of contacts_history table), and in the second case - only from contacts_history table. That is, in the first option there will be a plus field sclr_32.
In terms of performance, I empirically like the first option more, but as SagePtr correctly wrote above, you need to look at the plan, moreover, with a realistic number of lines and the ratio of the number of lines to the number of unique values ​​​​of the contacts_history.contact_id field.
And if we consider the use of such queries in practice, then perhaps it will still be wrapped in select * from (...) limit xx offset yyy; Then the analysis will have to be carried out in relation to the full text. And here it seems to me. the first option will produce the first xx rows faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question