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.


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 *