And now for something completely different. New version of GD Star Rating. I have finally completed very difficult task on optimizing the plugin and number of SQL queries executed. I wanted to include this in 1.5.0, but I needed few more days.
Depending on the theme, number of loops and other things, during the forming of the page, plugin makes a number of calls to the database. But that was not always done the best possible way. In many cases there were duplicate calls for getting the same templates, same check for the recorded votes and few more things that needed to be dealt with. Last 2 weeks I have been testing all that, and I have found all the problems and I decided that best way to solve them is to use two methods: cache and prefetch.
Cache is used for templates, posts, comments and log checks. Prefetch is used for posts, comments and logs. So, if you have a front page with 10 posts, and you show standard rating for each post there, typically you need 4 queries per post: 2 for template, one to get rating data and one to check log for duplicated vote. For 10 posts that’s 40 queries on top of only 20 that WP needs for the whole page. Comments are in the similar state, but usually you can have 30-40 comments per page. Since (by default) all posts will use same template, instead for 20 queries for templates with need only 2, we save them to cache and use from there. And that cuts queries in half. Prefetch is done once the loop starts, and in one query we get data for all 10 posts at once, and in second query all 10 logs for these posts, cutting number of queries from starting 40 to 4 for the whole front page, making less then half a query per post. And saving over 90% on database access.
On single post you still need to execute all 4 queries, so nothing is gained, but single post has comments, and again with them we get 90% savings. When it comes to multi ratings they need an extra query for multi elements, but I have cleaned up some of the code, removing number of queries used for multi ratings. When I complete tests, in a typical situation, 1.5.1 executes up to 70% (yes, seventy percents) less queries than 1.5.0 and previous versions. Also, having to execute less SQL queries, this change reduced used memory for about 1MB.
As an interesting comparison, GD Star Rating now needs less queries for 10 posts than WP-PostRatings plugin. A lot of people didn’t want to replace WP-PostRatings with GD Star Rating because it was simpler, and that also caused that plugin to need less queries per page. Now that’s no longer case, and in most cases with all the features GD Star Rating has, it now needs very few queries to work.
If you use multiple loops and with them you show ratings in different places, than savings in your case can be even bigger. Anyway, there is not much to say, you simply need to upgrade to 1.5.1. It is very, very fast, with minimal impact to page loading performance and due to the all previous bug fixes during 1.4.x release history, very stable. But, if you find any problem you can safely go back to 1.5.0, and please report any problem you find.