Skip to content
Diamond Sierra

PB-2502: The same query ran many times in one request

Paul Bunyan code PB-2502 (Warning): The same query ran many times in one request. What it means and what to do.

Updated · By Diamond Sierra

On this page

What it means

A request ran 100 or more queries and at least one query shape repeated five or more times.

What to do

Usually a query inside a loop. Fetch the rows once before the loop, or keep the result in a variable or the object cache.

Common causes

  • A query inside a loop: one query per product, per post or per menu item (often called the N+1 problem).
  • Templates calling get_post_meta() or get_option() for items one by one when the data wasn't loaded in advance.
  • A plugin checking the same setting many times without keeping the result.
  • No persistent object cache (PB-3002), so repeated lookups always reach the database.

How to find and fix it

  1. The details show the repeated query shape and which function ran it.
  2. Fetch the rows once before the loop and look them up in an array inside it.
  3. Keep results in a variable, or in the object cache with wp_cache_get() and wp_cache_set().
  4. If the code belongs to a plugin, send its author the query shape and how many times it ran.

Details

CodeSeverityGroup
PB-2502WarningDatabase