Uploaded image for project: 'Plugins'
  1. Plugins
  2. CONTRIB-8325

Memory exhausted when trying to view course certificate instance

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.10
    • Fix Version/s: None
    • Labels:
      None
    • Plugin Version:
      3.9.2 (2020100700)
    • Affected Branches:
      MOODLE_310_STABLE

      Description

      We've been hit hard by a problem at the learn.moodle.org staging site where after creating a new Course certificate module instance, the view.php script died with all the available memory exhausted.

      I was able to trace down the problem to mod_coursecertificate\output\view_page::export_for_template() which has the line:

      $data['userstoissue'] = count(helper::get_users_to_issue($this->certificate, $this->cm));
      

      that is expected to render a line like "Currently, X students meet this activity's access restrictions and will be issued with their certificate once they access it." with "X" being replaced with the number of users.

      There are several major issues with this:

      • The get_users_to_issue() method calls get_enrolled_users() which in turn calls get_records_sql() - that is, it loads all the records into the PHP memory. In our case, the query returns 43.000 records.
      • That call uses u.* to obtain all user record fields include the ones it never needs such as rich text profile description fields.
      • All these records and all their fields are loaded into the memory just to display their count.

      These deserves some optimisation, for example:

      • Use get_enrolled_sql() to construct more effective SQL.
      • Load only those user fields that are actually needed (using asterisk wildcard * is almost never a good idea).
      • Have a dedicated method to return the number of these candidate users for this case where we do not actually need to load them.

        Attachments

        1. image-2020-12-09-13-06-15-832.png
          117 kB
          Mikel Martín Corrales
        2. screenshot-1.png
          204 kB
          David Mudrák (@mudrd8mz)

          Issue Links

            Activity

              People

              Assignee:
              mikelmartíncorrales Mikel Martín Corrales
              Reporter:
              mudrd8mz David Mudrák (@mudrd8mz)
              Participants:
              Component watchers:
              Emilio Lozano, Marina Glancy, Mikel Martín Corrales
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: