In my case joining table inside subquery or outside subquery gives very few difference with COUNT
CASE 1: about 6202 rows.
In this case table_c
is joined inside subquery, but is only joined (No other actions for the table, not selecting any data or filtering whole query with that table).
CASE 2: about 6235 rows.
In this case that table is joined outside of subquery but joining key (groupCol
) is the same.
Here is my self-explanatory MySQL query:
SELECT
COUNT(*)
FROM (
SELECT
a.*
, b.some
# no c columns
# main table
FROM table_a AS a
# no mention this
LEFT JOIN table_b AS b
ON a.col = b.bcol
#### CASE 1 ###
# this is left joined, this is matter
# this is only joined, does nothing more
LEFT JOIN table_c AS c
ON c.col = a.groupCol
#### CASE 1 ###
WHERE
*** SOME statements ***
# c table not participating in WHERE, or GROUP-ing clause
GROUP BY a.groupCol
ORDER BY a.dateCol
) AS subSelect
#### CASE 2 : table_c is joined outside of subquery ###
LEFT JOIN table_c AS c
ON c.col = subSelect.groupCol
#### CASE 2 ###
WHERE
*** SOME statements ***
# table_c still not participating of course
I also know how does MySQL LEFT JOIN
works: if data not found on second table, row corresponding fields from that table are set to null.
What what actually happens here I can't figure out.
Also case 1 is faster than case 2.
Why? Also for sure, that joining in case 1, contains least double amount of rows. In case 2 it's first filtered and then joined.
I thought this should be faster way...