Query Monitor: Diagnosing Slow Queries and Hooks in WordPress

Query Monitor: find what makes a WordPress page slow

A slow page has a cause somewhere between the database, PHP code, hooks and external HTTP requests. Query Monitor is a free debugging plugin that shows all of this for the current request in a panel under the admin bar: every database query with its time and the function that called it, HTTP API calls, hooks, template files, PHP errors. It answers “which plugin is it?” in a few minutes.

Problem: A product page needs 2.4 seconds to generate. Deactivating plugins one by one on a live shop is not an option, and the slow query log of MySQL shows nothing unusual.

Solution: Install Query Monitor, open the page as administrator and read the panels in this order: Overview, Queries by Component, Duplicate Queries, HTTP API Calls. Measure own code with the qm/start and qm/stop actions and send debug messages to the Logs panel with qm/debug instead of error_log().

Read the panels

  • Overview: total generation time, peak memory, number and total time of queries. If the query time is a small part of the total, the problem is PHP or HTTP, not the database.
  • Queries → Queries by Component: number and time of queries per plugin, theme and core. The plugin with 300 queries stands out at once.
  • Duplicate Queries: the same SQL run several times, with the callers. Typical cause: get_post_meta() or get_the_terms() per item in a custom loop without primed caches, see N+1 queries in WordPress.
  • Slow Queries: queries above the threshold (0.05 seconds by default), with the full call stack.
  • HTTP API Calls: requests made with wp_remote_get() and friends during page generation, with their duration. A license check or an API call on every page view often costs more than all queries together.
  • Hooks & Actions and Template: which callbacks are attached, which template file and template parts were used.

The panels are only visible to administrators by default. For a logged-out view, Query Monitor can set an authentication cookie (button in the Settings panel of Query Monitor), so the same data is shown while browsing as a visitor, which matters because caches and plugins behave differently for logged-in users.

Time your own code

function ha_build_related_products( int $product_id ): array {
    do_action( 'qm/start', 'ha_related' );

    $ids = ha_find_related_ids( $product_id );
    do_action( 'qm/lap', 'ha_related', 'ids found' );

    $products = array_filter( array_map( 'wc_get_product', $ids ) );

    do_action( 'qm/stop', 'ha_related' );
    return $products;
}

The Timings panel shows the duration and memory of ha_related and of each lap. When Query Monitor is not active, nobody listens to these actions, so they cost nothing and can stay in the code.

Debug messages in the Logs panel

do_action( 'qm/debug', 'Related products cache miss for {id}', array( 'id' => $product_id ) );
do_action( 'qm/warning', 'API answered in {ms} ms', array( 'ms' => $ms ) );
do_action( 'qm/error', $wp_error ); // a WP_Error or an exception is printed with its message

The levels follow PSR-3 (debug, info, notice, warning, error and more), and {placeholders} are filled from the context array. Messages appear with the component that logged them, only for the current request, without writing a file.

When Query Monitor is not enough

Query Monitor sees one request: the page you are looking at. Slow AJAX and REST calls appear in its panel too (in the browser’s developer tools, as response headers, when you are logged in as administrator), but cron runs, background processes and the requests of other users are not visible. For those, the database side has the slow query log: MySQL slow query log. The query counts are also a good number to record before and after an optimization, as part of an audit: a WordPress performance audit checklist.

NOTE: Query Monitor adds work to every request while it is active, and its panels contain paths and SQL. Use it on staging, or activate it on production only for the time of the analysis and only with administrator accounts. It also loads early as a drop-in (wp-content/db.php) to catch all queries; if another plugin already uses db.php (some caching plugins do), Query Monitor shows fewer details about the queries.