Working at scale: queries, transients and caching
Make busy WordPress sites fast by asking the database fewer questions, using transients and the right caches, with real query counts.
In this lesson you will
- Count and reduce queries
- Use transients
- Choose the right cache layers
- Measure every change
About 17 minutes
When a site gets busy
Noor’s recipe site was fast with fifty recipes. At five thousand recipes and a few hundred visitors an hour, the archive pages crawl and the host sends a warning about database load. Nothing is broken; the code simply asks the database too many questions. Advanced WordPress work often means asking fewer questions, remembering answers and keeping heavy work away from visitors. This lesson shows how, with real measurements.
Think in queries
Every time WordPress needs data it asks the database. Ten visitors times fifty questions is five hundred questions; at scale this matters. The fix is not magic: ask for less, ask once and remember answers. Tools such as Query Monitor (a well-known free plugin) show how many questions each page asks and which are slowest. Always measure on a practice copy before and after.
Measured for real
A test created twelve posts, cleared WordPress’s memory between measurements, and counted database queries for several ways of asking for the same ten posts. The counts include a few queries WordPress runs to warm up, so compare them with each other, not with zero. The output is real:
default WP_Query: 7 queries, 10 posts | with no_found_rows: 6 queries, 10 posts
lean query (ids only, no meta or term caches): 2 queries, 10 posts
12 get_post_meta calls with a cold cache: 12 extra queries | after one priming call: 1 queries
transient: first call computed (slow), second call read from storage (fast), value: slow result
cleaned: yes
A normal search query used 7 queries. Adding no_found_rows, which skips counting the total number of matches (you only need that for page numbers), dropped it to 6. A lean version that asked only for post numbers and skipped extra caches used 2. Reading one custom field for each of twelve posts one at a time cost 12 queries, but priming the cache for all twelve first brought it down to 1. This is the famous ‘N plus 1’ problem: one question per item instead of one question for all.
Transients and caching
Some work is slow: calling another website, adding up thousands of rows, building a big list. A transient stores the answer for a set time, so the work is done once and then read quickly. In the test the first request had to compute (slowly); the second read the saved result (fast). Always handle the case where the saved answer has expired, as the sample does.
$data = get_transient('mysite_top_posts');
if (false === $data) {
$data = expensive_work();
set_transient('mysite_top_posts', $data, HOUR_IN_SECONDS);
}
return $data;
Object cache and page cache
| Cache | Remembers |
|---|---|
| Page cache | The finished page for visitors who are not logged in |
| Object cache | Database answers between requests (needs a server component) |
| Browser cache | Files on the visitor’s device |
| CDN | Files on servers near the visitor |
Ask your host which are available. A persistent object cache can help large, dynamic sites, shops and membership sites where page caching is limited. Use one caching layer of each kind and test that logged-in users and carts still behave correctly.
Other habits that scale
- Limit posts per page, and use pagination.
- Index heavy filters in custom queries, or avoid them.
- Do heavy jobs in the background, using scheduled tasks, not during a page view.
- Clean old revisions, spam and expired transients.
- Choose hosting that matches your traffic, with enough memory and a current PHP version.
Practice
On a practice site, install a query-
Any drop is a win; the point is learning to measure. If the number does not fall, look for a plugin or widget that asks the same question repeatedly.
Caching everything for a long time. Stale prices, stale carts and stale logins cause real harm. Cache what is slow and safe to repeat.
A scaling checklist
| Question | Action |
|---|---|
| Which pages are slowest? | Measure with a query monitor |
| Is the same query repeated? | Cache or prime it |
| Is heavy work done per visitor? | Move it to a scheduled job |
| Is the host enough? | Check memory, PHP version, caching |
Work through the list in order. Many sites are fixed by the first two lines, without touching the server. Only when the code is lean does it make sense to pay for bigger hosting, because a bigger server running wasteful code is just an expensive way to be slow.
Scheduled jobs
WordPress has a built-in scheduler, often called cron, which runs jobs such as publishing scheduled posts and cleaning expired data. It is triggered by visits, so on a quiet site jobs may run late. Many hosts let you replace it with a real server timer for reliability. Put long jobs there, split them into small steps and make each step safe to repeat.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does no_found_rows do?
- Deletes rows
- Adds posts
- Skips counting total matches
- Changes the theme
Useful when you do not need pagination.
What is a transient?
- A stored answer that expires
- A user role
- A theme file
- A menu
It avoids repeating slow work.
What is the N plus 1 problem?
- Too many plugins
- A slow theme
- One query per item instead of one for all
- A password rule
Prime the cache first.
Why measure before and after?
- To know whether a change helped
- To pass time
- To change colours
- To delete posts
Guessing wastes effort.
What should you not cache for long?
- Old images
- Carts, prices and logins
- Footer text
- Blog posts about history
Stale data hurts.
Summary
- Ask for less, ask once, remember answers.
- no_found_rows and cache priming cut queries.
- Transients store slow results for a while.
- Measure before and after.
Questions
Do I need an object cache?
Not for small sites. Large dynamic sites may benefit; ask your host.
What is Query Monitor?
A free plugin for developers that shows queries, hooks and slow spots on each page.
How long should a transient last?
As short as the data can stay fresh: minutes for prices, hours for popular lists.
