Let me show you something:
# Query_time: 1 Lock_time: 0 Rows_sent: 1 Rows_examined: 150000
SELECT COUNT(DISTINCT(u.unit_id))
FROM unit u
RIGHT JOIN
(SELECT up1.unit_id
FROM unit_param up1
WHERE up1.unit_type_param_id = 24
AND up1.value = ‘ServiceA’
) nmp0
ON u.unit_id = nmp0.unit_id;
This query is fine. No problem. Now, I have to make another selection from the unit_param table, for which I need to do another JOIN in addition to the previous query. The first section of the query is exactly the same as the previous query. Beware:
# Query_time: 1138 Lock_time: 0 Rows_sent: 0 Rows_examined: 2271979789
SELECT COUNT(DISTINCT(u.unit_id))
FROM unit u
RIGHT JOIN
(SELECT up1.unit_id
FROM unit_param up1
WHERE up1.unit_type_param_id = 24
AND up1.value = ‘ServiceA’
) nmp0
ON u.unit_id = nmp0.unit_id
RIGHT JOIN
(SELECT up1.unit_id
FROM unit_param up1
WHERE up1.unit_type_param_id = 23
AND up1.value = ‘Bigland’
) nmp1
ON u.unit_id = nmp1.unit_id;
This query never responded, I had to cancel it (but not before it had run for 20min!! running on 100% CPU) However, the number of rows with “Bigland” was just 75K rows compared to rows with “ServiceA” which was 50K rows. The initial table (unit) was 100K rows. Each row record is approx. 500B. So adding ONE JOIN extra, with an additional 75K rows to JOIN, the query went from OK to a DISASTER!!!!
The number of rows involved went from 150K rows to 2.2G rows!!!!! How on earth could this happen? Has the JOIN thing gone completely crazy??? What can I do about this? I have tried adjusting the join_buffer_size (128M and 16M) but to no avail.
I must admit I am getting desperate now.
Sincerly grateful for any input here!
Morten Simonsen