Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-72533

Calendar event table performance is slow

XMLWordPrintable

    • 3
    • HQ Team International Sprin 10, HQ Team International CI H2-21

      The events api was updated (MDL-66253) to use a UNION for selecting events of different sources (user, course, category, group), but with a large db of events (~157k), we have found a massive performance hit with this query.

      We regularly find query run-times of in excess of 2 seconds, often over 10, which makes the calendar (upcoming events, and timeline blocks) almost unusable.

      We noticed the change when the query was changed to include UNION subqueries instead of WHERE.

      i.e. Change:

      SELECT e.*
            FROM mdl_event e
      INNER JOIN (SELECT ev.modulename,
                      ev.instance,
                      ev.eventtype,
                      MIN(ev.priority) as priority
                 FROM (SELECT modulename, instance, eventtype, priority
                            FROM mdl_event ev
                           WHERE (ev.userid = ? AND ev.courseid = 0 AND ev.groupid = 0 AND ev.categoryid = 0) 
                      UNION SELECT modulename, instance, eventtype, priority
                            FROM mdl_event ev
                           WHERE  (ev.groupid IN (?, ?))
                      UNION SELECT modulename, instance, eventtype, priority
                            FROM mdl_event ev
                           WHERE  (ev.groupid = 0 AND ev.courseid IN (?, ?) AND ev.categoryid = 0)
                      UNION SELECT modulename, instance, eventtype, priority
                            FROM mdl_event ev
                           WHERE  (ev.categoryid != 0 AND ev.eventtype = 'category')) ev
             GROUP BY ev.modulename, ev.instance, ev.eventtype) fe
              ON e.modulename = fe.modulename
                 AND e.instance = fe.instance
                 AND e.eventtype = fe.eventtype
                 AND (e.priority = fe.priority OR (e.priority IS NULL AND fe.priority IS NULL))
       LEFT JOIN mdl_modules m
              ON e.modulename = m.name
           WHERE (m.visible = 1 OR m.visible IS NULL) AND (timestart >= ?) AND timestart <= ? 
              AND ((e.userid = ? AND e.courseid = 0 AND e.groupid = 0 AND e.categoryid = 0)
                      OR e.groupid IN (?,?)
                      OR (e.groupid = 0 AND e.courseid IN (?,?)) 
                      OR (e.groupid = 0 AND e.courseid = 0 AND e.categoryid != 0))
              AND (e.visible = 1)
        ORDER BY COALESCE(e.timesort, e.timestart) ASC, e.id ASC;
      

      to:

      SELECT e.*
            FROM mdl_event e
       LEFT JOIN mdl_modules m
              ON e.modulename = m.name
           WHERE (m.visible = 1 OR m.visible IS NULL) AND (timestart >= ?) AND timestart <= ? 
              AND ((e.userid = ? AND e.courseid = 0 AND e.groupid = 0 AND e.categoryid = 0)
                      OR e.groupid IN (?,?)
                      OR (e.groupid = 0 AND e.courseid IN (?,?)) 
                      OR (e.groupid = 0 AND e.courseid = 0 AND e.categoryid != 0))
              AND (e.visible = 1)
        ORDER BY COALESCE(e.timesort, e.timestart) ASC, e.id ASC;
      

      The part I'm unsure about is this query being used by different plugins, so perhaps the union is only required under certain circumstances. So am keen for others to look at this as well please.

        1. afterpatch.png
          afterpatch.png
          104 kB
        2. beforepatch.png
          beforepatch.png
          97 kB
        3. beforepatch-1.png
          beforepatch-1.png
          97 kB
        4. calendarperformance.patch
          8 kB
        5. image-2022-10-13-09-52-58-741.png
          image-2022-10-13-09-52-58-741.png
          32 kB
        6. image-2022-10-13-09-58-33-015.png
          image-2022-10-13-09-58-33-015.png
          32 kB
        7. image-2022-10-14-09-47-36-801.png
          image-2022-10-14-09-47-36-801.png
          65 kB
        8. image-2022-10-14-09-50-33-756.png
          image-2022-10-14-09-50-33-756.png
          33 kB

            psistrom Peter Sistrom
            sharpchi Mark Sharp
            Kevin Pham Kevin Pham
            Ilya Tregubov Ilya Tregubov
            CiBoT CiBoT
            Votes:
            29 Vote for this issue
            Watchers:
            47 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 day, 1 hour, 56 minutes
                1d 1h 56m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.