If Search Console says it couldn’t fetch your sitemap, or your site falls over when a crawler visits it, start by finding out how long the sitemap actually takes to build. I run a WordPress site with about 32,000 posts, and a slow sitemap once took it down every Tuesday for months. This is the checking routine I’d use now, in order, with the numbers from my own site.
(The full story of that outage, and the fix, is in Why My Site Went Down Every Tuesday. This post is the shorter “how do I find out what’s wrong” version.)
Step 1: time the sitemap
Ask for it with curl and print the status code and the time:
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" https://example.com/sitemap_index.xml
Run it 5 times, because one result can be a fluke. Here are mine, for my busiest post sitemap, which is built by Yoast:
200 1.248s
200 1.243s
200 1.194s
200 1.227s
200 1.240s
That’s steady at about 1.2 seconds. The index page that lists all 35 sitemaps took 0.56 seconds. For comparison, WordPress’s own built-in sitemap on two small sites of mine took about 0.45 and 0.49 seconds, so about half a second looks like the floor for any request from where I tested. As a rough guide, I’d worry about anything that takes several seconds, and my slow one took about 18.
If you get a 504, a 502 or a timeout instead of a number, that’s your answer to “is it slow”. Go to step 2.
Step 2: see who is asking for it
A sitemap that takes 2 seconds is fine when one crawler asks. It’s a problem when 20 ask at once, because each request holds a PHP worker. Count the sitemap requests in your access log by status and user agent:
awk -F'"' '/sitemap[^ ]*\.xml/ {split($3,a," "); print a[1], $6}' access.log | sort | uniq -c | sort -rn
I tested that on a made-up log of the same shape as my Tuesday (129 requests from an audit crawler, 6 from Googlebot, plus some ordinary page views), and it printed:
112 504 Mozilla/5.0 (compatible; AhrefsSiteAudit/6.1; ...)
17 200 Mozilla/5.0 (compatible; AhrefsSiteAudit/6.1; ...)
6 200 Mozilla/5.0 (compatible; Googlebot/2.1; ...)
The log was invented, so those numbers aren’t real traffic, but the pattern matches what I found on the real one: one crawler, all at once, mostly failing. If one user agent dominates the list, that’s your trigger. It might be a tool you set up yourself and forgot (that was mine).
Where the log lives depends on your host. On a Plesk server it’s usually under the domain’s own logs folder, and you may need root to read it.
Step 3: find out what is slow inside PHP
Timing tells you that it’s slow and the log tells you who asked. Neither tells you why. For that, turn on PHP-FPM’s slow log, which writes a stack trace for any request that takes longer than a limit you choose. I set it to 5 seconds in Plesk for the one domain and waited for the next bad day. Nearly every entry was a sitemap request, and the trace pointed at code I’d written myself.
If you can’t get at the slow log, a plugin such as Query Monitor will show you the slowest database queries on a single request. That’s slower going but works on shared hosting.
The usual suspects
- Your own code. Mine was a filter that took noindexed posts out of the sitemap by checking every post one at a time. Fine on a small site and 18 seconds on 32,000 posts. Any custom filter on your sitemap deserves suspicion first.
- Too many URLs per sitemap. A very large sitemap takes longer to build. Splitting it into smaller ones helps, as long as the building work doesn’t grow with the total number of posts.
- No caching of the expensive part. The fix for mine was to calculate the list once, keep it for 12 hours and rebuild it only when a post changes.
- Too few PHP workers. If every worker is busy, even a fast page waits. That’s why a slow sitemap can make the whole site look down.
- A crawler you don’t need. Some bots fetch every sitemap at once. You can refuse them with a rule in
.htaccessor your server config, but make sure Googlebot isn’t caught by it.
Step 4: measure again
After any fix, repeat step 1 the same way, so you compare like with like. My sitemap went from about 18 seconds to about 1.2, and Search Console stopped reporting it as timing out. If your number hasn’t moved, you’ve fixed the wrong thing, so go back to step 3 and look at the slow log again.
Gotchas
- Test the sitemap at a quiet time and a busy time. A sitemap that’s fine on its own can fail when a crawler hits it.
- Your sitemap address depends on the plugin. Mine is
sitemap_index.xmlfrom Yoast, whereas WordPress’s built-in one iswp-sitemap.xml. Check yourrobots.txtfor the right one. - Don’t rely on a single successful fetch in Search Console as proof. It tells you about one fetch, not about 20 at once.