Slow queries

Local plugins ::: local_slow_queries
Maintained by Eduardo Kraus
An admin-only plugin that turns your mdl_log_queries table into a practical UI to find, triage, and analyze slow SQL statements executed by Moodle.

Slow queries 0.1.3

Moodle 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 5.0, 5.1
Released: Ijumaa, 30 Januari 2026, 9:15 PM

local_slow_queries — Slow Queries Viewer for Moodle

local_slow_queries is an admin-only plugin that turns your mdl_log_queries table into a practical UI to find, triage and analyze slow SQL statements executed by Moodle.

It focuses on three daily workflows:

  • List & filter slow queries quickly (default exectime > 3s).

Access is restricted to: require_capability("moodle/site:config", context_system::instance())

What this plugin assumes

This plugin does not capture queries by itself. It assumes your environment already logs queries into:

  • mdl_log_queries (or {log_queries} with Moodle prefix)

So the plugin is a viewer + analysis helper for an existing slow-query logging mechanism.

Data source

The plugin reads from:

Table: mdl_log_queries

Fields used by the UI:

  • id — unique identifier
  • qtype — query type (stored by your logger)
  • sqltext — SQL statement text
  • sqlparams — parameters (string representation)
  • error — non-zero marks error
  • info — optional extra data
  • backtrace — string backtrace
  • exectime — decimal seconds
  • timelogged — unix timestamp

Index suggestions in the install.xml are focused on viewer performance: - timelogged, exectime, qtype, error

Backtrace "caller" logic

The list view shows a simplified "caller" derived from backtrace:

  1. Find the first frame containing /lib/dml/
  2. Show the next frame (usually the Moodle component that invoked the DB layer)
  3. Shorten long absolute paths to keep it readable in a table

This is intentionally pragmatic: it helps you quickly identify whether the query comes from completion, search indexing, forum reports, etc.

CRON detection

A query is considered "CRON" when its backtrace contains:

  • /admin/cli/cron.php

The UI then: - sets the CRON column ✅ - adds a CRON badge

Parameter handling

sqlparams in production can vary depending on the logger implementation. This plugin attempts to parse common formats:

  • JSON arrays/objects
  • PHP serialize() format
  • "var_export-like" output (example: array ( 0 => 4, 1 => 50, ))

Then it applies parameters to SQL by replacing positional ? placeholders sequentially.

Notes: - This is a best-effort representation for analysis and reproduction. - It is not intended to be a perfect SQL reconstitution for every edge case.

UX & UI choices

This plugin intentionally keeps a light and readable look:

  • Cards with rounded corners
  • Soft shadows
  • Badges for quick categorization (CRON / WEB / ERROR)
  • Progress bars for distribution/share reports

It relies on Moodle’s loaded Bootstrap styles and adds minimal SCSS.

Security model

  • Pages are accessible only to users with:
    • moodle/site:config in context_system

This is critical because the plugin can reveal: - raw SQL - parameters (potentially containing sensitive values) - file paths in backtraces

Typical usage patterns

Ops triage

  • Open index.php
  • Filter exectime >= 10
  • Identify CRON-heavy spikes or web-timeouts
  • Open details, extract candidate indexes

Regression monitoring

  • Check last 7 days volume
  • Compare max/avg trends weekly
  • Identify new callers (components) from backtrace changes

Version information

Version build number
2026013000
Version release name
0.1.3
Maturity
Beta
MD5 Sum
dfa064998982f1aa2971fe1fcf680cb8
Supported software
Moodle 4.0, Moodle 4.1, Moodle 4.2, Moodle 4.3, Moodle 4.4, Moodle 4.5, Moodle 5.0, Moodle 5.1

Version control information

Version control system (VCS)
Other...
VCS repository URL

Default installation instructions for plugins of the type Local plugins

  1. Make sure you have all the required versions.
  2. Download and unpack the module.
  3. Place the folder in the "local" subdirectory.
  4. Visit http://yoursite.com/admin to finish the installation.