WordPress 7.2 fixed its /random/ query 11 days after the community flagged it: 9 seconds to 5 ms at 10 million posts
On 6 November I wrote that WordPress 7.2’s new /random/ URL picked its post with ORDER BY RAND(), and that on a store with 1,000,000 products every hit cost 3.4 seconds of database time. Eleven days later, on 17 November, core committed a fix.
The new query reads about 53 rows whether the site has 100,000 posts or 10,000,000. That’s a proper fix, and it came from a lot of people pushing in the same direction. Here’s who did what, how the fix works, where it differs from the shuffle table I suggested, and what I found when I tested the one case I was still unsure about.
The performance case was made in public, and core opened a ticket four days later
Aaron Jorbin announced the feature in a dev note on 5 November. The comment thread passed 100 replies, most of them about it being on by default. The performance part of the conversation went like this:
- 6 November, Ryan McCue asked the question first:
ORDER BY RAND()is O(N), and bots hitting/random/could be an easy way to DoS a site. - 6 November, me. I said it could be made a lot faster and linked the shuffle table benchmark.
- 6 November, René Hermenau benchmarked core’s query up to 2,000,000 posts. It stayed linear: 1.87 seconds for the query at 2 million posts, and 20 concurrent requests took 14 seconds to finish. He also found that
/random/could serve a cacheable page without its no-cache and noindex headers, and opened #66625 with a patch. That was fixed on 12 November. - 7 November, Daniel Iser listed the performance problem among his objections and linked the shuffle table article as the documented fix.
- 8 November, Paul Wong-Gibbs pointed out that small and medium hosts, not just enterprise sites, would feel the load.
- 9 November, Ryan McCue came back with a practical suggestion from running this at scale: pick 50 posts, cache them, and serve from the cache.
- 10 November, Aaron Jorbin opened #66654 to improve performance on large sites.
- 17 November, Aaron committed the fix as r65542, reviewed in pull request 16050.
The commit’s props are jorbin, peterwilsoncc, sukhendu2002, rmccue, gddrt, renehermi and djpaul. That is the official record of who contributed, and it’s well deserved. For what it’s worth, both René’s benchmark and the fix’s pull request said openly that Claude Code helped produce them, which is how AI-assisted contributions should be done.
The thread got heated at times. The performance part of it didn’t. People raised a concern with numbers, and the maintainer opened a ticket and fixed it before release. That’s the system working.
Core now looks up 50 random post IDs by primary key instead of sorting every post
The new approach is rejection sampling. It’s in src/wp-includes/query.php, in wp_get_random_content_sample_ids() and wp_random_content_posts_pre_query():
- Read
MAX(ID)fromwp_posts, cached for 10 minutes. - Pick 50 distinct random numbers between 1 and that maximum.
- Run the normal archive query restricted to
ID IN (those 50). Each is a primary key lookup. - If any of them is an eligible post, pick one of those at random and redirect.
- If none are, try again with 50 new IDs, up to three times in total.
- If all three miss, fall back to a pool of up to 1,000 randomly ordered eligible posts, stored in the object cache and used for 10 minutes.

It’s a neat design. Every ID between 1 and the maximum is equally likely to be looked up, so gaps in the IDs only reduce the hit rate. They don’t make some posts more likely than others. It still runs through the main query, so every pre_get_posts filter a site has still applies. It needs no new table and no cron job, and it only reads from the database.
At 10 million posts, the median fell from about 9 seconds to about 5 ms
These are the pull request’s own figures, from Aaron’s local testing, not mine:
ORDER BY RAND() | Rejection sampling | |
|---|---|---|
| Rows read, 100,000 posts | every eligible post | 53 |
| Rows read, 10,000,000 posts | every eligible post | 53 |
| Median time, 10,000,000 posts | about 9 s | about 5 ms |
The cost no longer grows with the size of the site. For the case Ryan, René, Paul and I were worried about, a big site with bots hammering /random/, that’s the main problem solved.
Sampling and a shuffle table are different trade-offs, and core picked the right one for core
Core didn’t use the shuffle table, and I think that was the right call. A shuffle table is a new database table with a daily rebuild, and that’s a lot to add to every WordPress install for one feature. Here’s how the two compare:
| Core’s rejection sampling | Shuffle table | |
|---|---|---|
| New table or schema | None | One extra table |
| Scheduled work | None | Daily rebuild (3.5 s to 4.3 s at 1,000,000 products in my test) |
Honours every pre_get_posts filter | Yes, it’s the main query | No, post types and taxonomies only |
| New posts | Eligible straight away | Eligible after the next rebuild, unless appended on publish |
| Fairness | Every eligible post equally likely | Equal for a post type between rebuilds; close but not exact for categories |
| Big archive, dense IDs | A few primary key lookups | One primary key seek |
| Small category on a big site | Falls back to the cached pool of up to 1,000 posts | One primary key seek, walking forward to the next match |
| Needs an object cache | Only for the fallback pool | No |
In my 6 November benchmark on 1,000,000 products, the shuffle table picked a random product in 0.24 ms against ORDER BY RAND()‘s 3,372 ms, about 14,000 times faster. That comparison was against the old query. I haven’t benchmarked the shuffle table against the new one, so I’m not going to claim one beats the other. For a big archive, both are now a handful of index lookups.
Small categories on big stores fall back to a pool, and in my tests that stayed cheap
Sampling works when eligible posts are a decent share of all the IDs in wp_posts. On a WooCommerce store they often aren’t. Revisions, attachments, orders on older stores and product variations all take IDs, so a small category can be a tiny fraction of the ID range. Then all three rounds of sampling can miss, and core reads the archive to build its pool.
Core’s code is careful about this. The pool is cached for 10 minutes, kept for an hour as a fallback, and only one request rebuilds it at a time. Camille Spain asked about exactly this case in the dev note’s comments on 25 November, so I tested it on the 7.2 release candidate, on a local test site with no object cache at all.
Even with only 1 in 100 IDs in wp_posts being a published post, most requests found a post by sampling, and the ones that fell back to the pool stayed cheap. A six-post category took the fallback every time and still cost a fraction of a millisecond. And 20 concurrent /random/ requests finished faster than 20 concurrent home page loads. To make the fallback hurt, you’d need far more rows in wp_posts than even a very large store has.
You can still switch /random/ off with one line
On performance grounds, you no longer need to. The fix doesn’t change the other objections in the thread, such as custom post types that were never meant to be found. If you don’t want /random/ on your site, the filter still turns the whole feature off:
add_filter( 'wp_enable_random_content_redirect', '__return_false' );
If you’d rather install a plugin than add code, Jono Alderson published No Random Posts on WordPress.org on 12 November.
Summary
- WordPress 7.2’s
/random/no longer usesORDER BY RAND()to pick a post. The fix is r65542, committed on 17 November 2026, ahead of 7.2’s release. - It looks up 50 random IDs by primary key, up to three times, and reads about 53 rows at both 100,000 and 10,000,000 posts.
- Ryan McCue, René Hermenau, Paul Wong-Gibbs, Daniel Iser and others made the performance case in public. Aaron Jorbin opened the ticket and wrote the fix, with Peter Wilson and the others in the props.
- A shuffle table is a different trade-off: one seek for any archive, at the cost of a table and a daily rebuild. Core’s approach fits core better.
- Small categories on big stores fall back to a pool. In my tests, with no object cache, that stayed cheap, and
/random/cost less than loading the home page.
Comments