我试图在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;
huge_table
有大约2000万行和26列,但我只需要其中的5个来过滤。我认为这21个多余的列占用了太多的空间(大约20GB!)在100秒内)。有没有办法只隔离5个需要的列,以便使用更少的空间?或者其他不使用join的形式?
还是有其他原因占用了这么多空间?
**** UPDATE ****
原查询:
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;
和他的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', ''
为了解决这个问题,我用索引列替换了LEFT JOIN
上的t2.vouchercode
,查询运行得更快,并且不消耗太多空间。