Skip to content

WordPress database query optimization: reading Query Monitor properly

Query Monitor on a WordPress site exposes the queries, the callers, and the time costs — but only if you know which columns matter. The audit pattern that turns Query Monitor output into a fix list.

Query Monitor on a WordPress site that takes 4 seconds to render an admin page will list 800 queries. The natural reaction is to assume the database is the problem — but five of those queries account for 80% of the time, and Query Monitor will tell you which five if you read the right columns. This article is the read-it-correctly guide that turns Query Monitor’s output into a prioritized fix list.

What’s happening

Query Monitor is a free WordPress plugin that intercepts every database query made during a request and surfaces them in an admin bar dropdown. By default it shows total query count and time, but the deeper panels (Queries by Caller, Queries by Component, Slowest Queries, Duplicate Queries) are where the diagnosis lives.

WordPress query problems fall into three categories:

  • Many small queries: a plugin or theme that calls get_post_meta inside a loop without update_meta_cache produces N+1 queries. Fix is bulk-prefetching.
  • One slow query: a missing index on wp_postmeta, wp_options, or wp_actionscheduler_actions. Fix is adding the index.
  • Repeated identical queries: the same query runs 50 times in one request because nothing caches the result. Fix is either WordPress object cache or a function-level memoization.

How to verify

Install Query Monitor and reproduce the slow request as a logged-in admin:

wp plugin install query-monitor --activate

Open the admin bar dropdown. The relevant panels:

  • Queries → Queries by Component: which plugin or theme is responsible for the most query time. The top entry is usually the culprit.
  • Queries → Queries by Caller: which function called the query. Useful when the component is “Core” but the query came from a specific plugin’s hook.
  • Queries → Slowest Queries: the literal slowest queries by duration. A query above 50 ms is worth investigating; above 200 ms is a clear fix target.
  • Queries → Duplicate Queries: identical queries run multiple times. Each duplicate is a missed caching opportunity.

For continuous monitoring on production without Query Monitor enabled, use MySQL slow log:

# /etc/mysql/mysql.conf.d/slow.cnf
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 0.5
log_queries_not_using_indexes = 1

Then mysqldumpslow -s t /var/log/mysql/slow.log | head -20 to find the worst offenders.

The fix

  1. For many small get_post_meta queries in a loop: prefetch with update_meta_cache('post', $post_ids) before the loop. WordPress will pull all meta for the listed IDs in one query, and subsequent get_post_meta calls will hit the in-memory cache.
  2. For slow queries on wp_postmeta: add the standard composite index if missing. ALTER TABLE wp_postmeta ADD INDEX meta_key_value (meta_key(191), meta_value(191)). The 191-character prefix is required for utf8mb4 collations.
  3. For slow queries on wp_options with autoload='yes': an oversized autoloaded option is the cause. SELECT option_name, length(option_value) FROM wp_options WHERE autoload='yes' ORDER BY length(option_value) DESC LIMIT 20. Anything over 100 KB should be autoload='no'.
  4. For repeated identical queries: enable an external object cache (Redis), then verify the affected query is using the cache. Some plugins query the database directly via $wpdb and bypass WP_Object_Cache — those need a plugin patch or a wrapping cache.
  5. For Action Scheduler slow queries: the wp_actionscheduler_actions table needs maintenance. Run wp action-scheduler clean weekly and add the composite indexes recommended in the Action Scheduler README.
  6. For WooCommerce slow product queries: enable HPOS (High-Performance Order Storage) if not already on. Audit metadata that should be a database column on the orders table.

Re-measure with Query Monitor after each fix to verify the query count and time dropped. Do not move to the next fix until the previous one is confirmed.

Stack Harbor runs WordPress database audits across enterprise WordPress support accounts and maintains the index and option-autoload hygiene continuously. If your admin pages take more than 3 seconds to load, book a database review.