RavanH / xml-sitemap-feed

XML Sitemap & Google News feeds
GNU General Public License v2.0
16 stars 21 forks source link

Fix performance issue with `set_defaults` #11

Closed carlalexander closed 6 years ago

carlalexander commented 6 years ago

We're currently running into performance issues with the plugin on a very large WordPress site with 400k posts. The performance issue comes from the call to wp_count_posts in the set_defaults method of the XMLSitemapFeed class. On our site, wp_count_posts causes WordPress to perform a non-cacheable query that takes about 1s.

The solution in this PR is to use a transient to cache the result of wp_count_posts for a day. This prevents this expensive query from happening on every page load. I'm open to another solution if you don't like this one though. 😃

RavanH commented 6 years ago

Hi Carl, this is a VERY good idea :) but the things is that in the next release, the default will be 'yearly' without any if + wp_count_posts ... so you could do that on your site too if you wish.

Still, excellent solution! And thanks for your PR :)

jerclarke commented 6 years ago

Okay thanks @RavanH. (I work with Carl).

I'm not sure what you mean by yearly, but the main priority for us is that there's no wp_count_posts DB queries executing on every pageload, so as long as that's true, the solution will probably work fine for us too :)

RavanH commented 6 years ago

Hi Jer, the original if (wp_count_posts('post')->publish > 500) { $this->defaults['post_types']['post']['archive'] = 'yearly'; } will simply become $this->defaults['post_types']['post']['archive'] = 'yearly'; (so just without the IF conditional)

Thanks guys for pointing me to this resource intensive part, it's much better gone :)

I've planned streamlining the plugin more (mvc approach, jit inclusion, less queries) so if you run into anything else that needs trimming in your opinion, please don't hesitate to share your thoughts! Even in French if that makes it easier ;)

carlalexander commented 6 years ago

Ok, well this is perfect then. Will close :)