Hi Tony,
You are not alone with this issue. I am seeing the same behavior. What is really happening here from what I can tell is that the insert query is running and has locked the mdl_context table. This keeps the other simple queries from processing; as you have observed. Eventually everyone using the site ends up 'waiting' for this very slow query because it doesn't take many clicks to require a call to mdl_context...This can be alleviated a little bit if you configure mysql correctly, but unfortunatly not good enough for our site. The problem query for us (and it looks like for you too) is as follows:
INSERT INTO mdl_stats_daily (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', timeend, courseid, roleid, COUNT(DISTINCT userid), 0
FROM (
SELECT 1218686400 AS timeend, pl.courseid, pl.roleid, pl.userid
FROM (
SELECT DISTINCT ra.roleid, ra.userid, c.id as courseid
FROM mdl_role_assignments ra JOIN mdl_context ctx
ON ctx.id = ra.contextid
CROSS JOIN mdl_course c
JOIN mdl_role_capabilities rc
ON rc.roleid = ra.roleid LEFT OUTER JOIN mdl_course_categories cc1
ON cc1.id = c.category
WHERE ((rc.capability = 'moodle/course:view' )
AND rc.
permission = 1 AND rc.contextid = 1
AND (ctx.contextlevel = 10
OR (c.id = ctx.instanceid AND ctx.contextlevel = 50) OR (cc1.id = ctx.instanceid AND ctx.contextlevel = 40)))
) pl
) inline_view
GROUP BY timeend, courseid, roleid
This query would kick off via the cron job and would not finish before the next cron job. I have not spent any time debugging yet -- for now I have simply disabled stats. We upgraded from 1.6+ where stats worked perfectly. I miss them and would like to get them going again. We do have a fairly busy site with lots of cool logs and stats. This query, though, is not cool...
I believe this issue is in the tracker here.... vote, or comment!!
http://tracker.moodle.org/browse/MDL-13548Cheers,
Neil Streeter
eLearning Systems Administrator
Northwestern Michigan College