Hi Juan,
did you make any progress on evaluating this issue? Please, take a look at MDL-18484: I've tried Matt's proposal, reviewing his patch (=> statslib.php-rev02.diff) but I'm in need to review the population of the mdl_tmp_stats_enrolments table since it sometime takes MDL-18484#comment_98383_header_matteo" target="_blank" rel="noreferrer noopener">too much time.
TIA,
Matteo