﻿id,summary,reporter,owner,description,type,status,priority,milestone,component,version,severity,resolution,keywords,cc
8958,Optimize rewrite rule generation,matthijs,,"The problem was introduced after the upgrade from 2.6.5 to 2.7.
The site has a permalink structure of /%category%/%year%/%monthnum%/%day%/%postname%/

After the upgrade the blog runs more then 2400 queries per page load. The site works normal, only very very slow (of course).
Research into the queries being run shows wordpress is trying to insert the rewrite_rules in the wp_options table. Only problem is: the field rewrite_rules contains about 20,000 lines of code/rules, putting that single field in a text file is >1.2 Mb large. So it chokes on that.

Disabling plugins, re-importing the db file, renewing the wp files, nothing helps. Going back to 2.6.5 and then again upgrading reintroduces the problem.

Changing the permalink structure to something else, for example:
/%year%/%monthnum%/%day%/%postname%/

also solves the problem temporarily. As soon as I change back to
/%category%/%year%/%monthnum%/%day%/%postname%/

wordpress again tries to insert the huge query into the field rewrite_rules, which isn't possible.

Looking at the rewrite_rules it seems it repeats the same rules for every page and attachment, like

s:35:\""vacancies/473/attachment/([^/]+)/?$\"";
s:32:\""index.php?attachment=$matches[1]\"";
s:45:\""vacancies/473/attachment/([^/]+)/trackback/?$\"";
s:37:\""index.php?attachment=$matches[1]&tb=1\"";
s:65:\""vacancies/473/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\"";
s:49:\""index.php?attachment=$matches[1]&feed=$matches[2]\"";
s:60:\""vacancies/473/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\"";
s:49:\""index.php?attachment=$matches[1]&feed=$matches[2]\"";
s:60:\""vacancies/473/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\"";
s:50:\""index.php?attachment=$matches[1]&cpage=$matches[2]\"";
s:28:\""(vacancies/473)/trackback/?$\"";

Interestingly:

1- when I do an export of the posts to the xml file, install a fresh wp and then re-import the posts, the problem goes away. In that case, the field rewrite_rules, suddenly ""only"" contains around 5000 rewrite_rules.

2 - all the rules seem to relate to (older) versions of the pages and attachments
So maybe the problem is that wordpress is writing rules for every page revision and every page attachment it has ever had. The website is not that big, but with a few hundred pages, a few hundred attachments it seems to go into the 20,000 rules with this specific permalink structure. That could also explain that when I do the import of xml in a fresh install, there are no page revisions anymore and the rewrite rules are not that many.

I cannot imagine it is something else, because besides the fact that that one query is too large to be run, everything is fine. Database tables are in good condition, files are correct, I tried it on different servers, etc so I can almost certainly rule out another causes. Besides the fact that wp cannot run the query to insert the rewrite_rules, everything works fine, front- and backend.
",enhancement,closed,normal,,Optimization,2.7,major,duplicate,,robinmarshall mikeschinkel@…
