mysqlleft-joindiskspacequery-tuning

Mysql query is consuming too much space on disk


I'm trying to run the following query on mysql:

SELECT column1, column2, count(distinct t2.iduser)
 FROM table1 t1 
 LEFT JOIN table2 t2 ON t2.id = t1.id
 LEFT JOIN huge_table h ON h.column = t2.vouchercode 
 AND h.client IN (23, 42, 47, 50, 55, 54, 53) 
 AND h.id >= 1111
 AND h.starttime >=  '2015-01-01 04:00:00'  
 AND h.endtime <=  '2016-01-01 03:59:59'
 GROUP BY t1.id;

The huge_table has around 20 million rows and 26 columns, but I need only 5 of them to filter. I think those 21 surplus columns are consuming too much space (around 20GB! in 100 seconds). Is there any way to isolate only the 5 needed columns so that less space is used? Or some other form of not using join?

Or is there some other reason for so much space is being consumed ?

**** UPDATE ****

Here is the original query:

SELECT voucherprefix, 
description, 
count(distinct v.iduser) as usersVolume 
FROM vc vc 
LEFT JOIN voucher v ON v.idvouchercontrol = vc.idvouchercontrol 
LEFT JOIN radacct r ON r.username = v.vouchercode 
WHERE 1 = 1 
AND r.calledstationid IN (23, 42, 47, 50, 55, 54, 53) 
AND r.radacctid >= 695106 
AND r.acctstarttime >=  '2015-01-01 04:00:00'  
AND r.acctstarttime <=  '2016-01-01 03:59:59'
GROUP BY vc.idvouchercontrol;

And his EXPLAIN:

 '1', 'SIMPLE', 'radacct', 'range', 'PRIMARY,username,acctstarttime', 'PRIMARY', '8', NULL, '5915245', 'Using where; Using temporary; Using filesort'
 '1', 'SIMPLE', 'v', 'ref', 'vouchercode,sub_index', 'vouchercode', '63', 'func', '1', 'Using where'
 '1', 'SIMPLE', 'vc', 'eq_ref', 'PRIMARY', 'PRIMARY', '4', 'v.idvouchercontrol', '1', ''

Solution

  • To resolve this issue, I've replaced the t2.vouchercode on LEFT JOIN by a indexed column and the query was ran faster and does not consumed too much space.