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

Improve DB query in forum's observer when deleting a user from a course

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.7, 2.8, 3.0.3
    • Fix Version/s: 3.1
    • Component/s: Forum
    • Labels:

      Description

      In case of user_enrolment_deleted event, the forum observer delete records from four tables, where each delete command ends with the same syntax to get all the forums in the course.

       $forumselect = "IN (SELECT f.id FROM {forum} f where f.course = :courseid)";
      

      That means the same query is called every time.
      My suggestion is first retrieving this list of id's and then using the results for the next queries.

      if (!$forumselect = $DB->get_records_sql('SELECT id from {forum} WHERE course = '.$cp->courseid))
          return; // no results means no need to run the delete queries
      $forumselect = 'IN ('.implode(',', array_keys($forumselect)).')';
      // now we have a ready list and saved needless queries
      

        Attachments

          Activity

            People

            Assignee:
            l.gil Lior Gil
            Reporter:
            l.gil Lior Gil
            Peer reviewer:
            Dan Poltawski
            Integrator:
            David Monllaó
            Tester:
            John Okely
            Participants:
            Component watchers:
            Andrew Nicols, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Fix Release Date:
              23/May/16