WordPress Rewrite rules – ballooning wp-options table

When xNepali blog grew in size, the database load on the server was huge. We had to move to the Virtual Private Servers to deal with. But, to our surprise, we had to constantly play around with the memory to get the optimum result. There were a lot of times when database ‘max_allowed_packet’ bytes error were observed in error logs.

To solve the database connection we tried adding another private server for database. But, it didn’t solve anything.

In the blog there were about 400 pages. Somebody suggested that a lot of pages were not good for the server. We started migrating the pages to posts as post have better flexibility and more keyword options (like tags) than pages. Now, the number of pages are only about 200. But, the problem is still the same.

I checked the rewrite-rules column in the wp-options table. It was almost 1mb in size.  There are
if( is_array( $attachment_uris ) ) {
foreach ($attachment_uris as $uri => $pagename) {
$this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
$rewrite_rules = array_merge($rewrite_rules, $this->
generate_rewrite_rules($page_structure, EP_PAGES, true, false));

In addition, I added the following code in the function.php in the theme:

function filter_rewrite_attachment($content) {
if (!is_array($content))
return $content;
foreach ($content as $key => $val) {
if (strpos($val, 'attachment') !== false)
return $content;
add_filter('page_rewrite_rules', 'filter_rewrite_attachment');
add_filter('post_rewrite_rules', 'filter_rewrite_attachment');

The first code didn’t have much effect in the database size. The second code by Johan Eenfeldt from wp-hackers group did help reducing the table size. I need to wait for at least a day to see what performance enhancement or problem it will cause.


Incoming search terms:

  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 WQq6RSYkyta8YqN1sD1r_FQtsJWLX9AUZlYQOIow1m1C5REa2LrLZUXK6_mhXFDh 94b10cc36f984e2dcf9ee811f41bce0309a376e1&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1835 L0Hag4B5yN5vPOghoZ5-stQenhGBLnP4tyAhLkks635V5UigNr5U7pMXPgjzNASg 6f2400b695f51b9c21722e9ba149714ec0517ebc&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

2 thoughts on “WordPress Rewrite rules – ballooning wp-options table

  1. How did this turn out for you? We are experiencing the same issue. Slow site, huge rewrite_rules table (2MB). I just migrated to a VPS and the db isn’t importing due to a max_allowed_packet error.

  2. The second code did reduce the size and load.
    But, I had to edit the permalinks of all attached images to the original images – it was a lot of work.

Leave a Reply

Your email address will not be published. Required fields are marked *