Optimising WP E-Commerce’s SQL
7th of April
As part of my most recent project (which you’ll be hearing more about very soon), I’ve been working with WP e-Commerce and having a tonne of fun dealing with all the bits and pieces. In general, it has been quite handy, since it has meant I don’t have to deal with implementing all the payment handling and such. However, it does have its issues, including a fairly horrible API.
WPEC is also quite a bit inefficient, due in part to its customisability. However, it’s definitely nothing insurmountable with a bit of code and some clever tricks.
Note: I’ll be using code from 4.0-dev in examples, but it should all be the same for the latest stable version as well.
So, with all of that out of the way, let’s get started. First step in optimising anything in WordPress is to turn WP_DEBUG on. We’ll also want to turn SAVEQUERIES on so that we can see what exactly is getting queried. The Debug Bar plugin will also help to view the results of these.
To start off with, here’s the MySQL queries that were generated by WPEC for me on a non-WPEC page:
SELECT option_value FROM wpstore_options WHERE option_name = '_transient_timeout_wpsc_theme_path' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = '_transient_wpsc_theme_path' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = 'wpsc_replace_page_title' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = 'wpsc_hide_featured_products' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = 'base_zipcode' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = 'wpsc_ups_settings' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[productspage]%' AND `post_type` = 'page' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[shoppingcart]%' AND `post_type` = 'page' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[transactionresults]%' AND `post_type` = 'page' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[userlog]%' AND `post_type` = 'page' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = '_transient_timeout_wpsc_url_wpsc-default.css' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = '_transient_wpsc_url_wpsc-default.css' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = 'google_server_type' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = 'google_cur' LIMIT 1
That’s 14 queries for essentially nothing! Even worse are the four fulltext queries to find those shortcodes. Surely we can do better.
So, let’s start cutting pieces out. The first part that concerned me was the two google_ queries, as I’m not using Checkout. As it turns out, the Google Checkout plugin does all sorts of stuff even if it’s not loaded. This is not something we want. However, this is easy to fix. WPEC loads everything in the wpsc-merchants/ directory, but no other code relies on these merchants, so simply remove the ones you don’t need. We’re using Brent Shepherd’s PayPal Digital Goods payment gateway (which hopefully will make it into WPEC 4.0). This gateway uses the new 4.0 merchant gateway classes, so we don’t actually need anything in wpsc-merchants/. Before you remove all the files though, note that a blank directory will cause errors, so leave testmode.merchant.php to avoid this.
Right, we’re now down to 12 queries. Next job, cutting out the shipping information. Both base_zipcode and wpsc_ups_settings are being loaded, despite no shipping handlers being activated. As our store is purely virtual goods, we don’t need any of the shipping items, so we’ll do as before and remove them all. Be wary of the blank directory issue though, and leave at least one file in there (I chose flatrate.php).
OK, 10 queries! We’re making great progress. Next step is wpsc_replace_page_title and wpsc_hide_featured_products. Go into the presentation tab of your settings and resave, and this should save these to the database and set the autoload property, causing them to be loaded in the initial WordPress settings query. However, I noticed this was not happening on our server (I suspect that if they are set to off, they simply aren’t being saved), so I hardcoded them in the theme:
// pre_option_$x doesn't like false, so return 0 instead
add_filter('pre_option_wpsc_replace_page_title', '__return_zero');
add_filter('pre_option_wpsc_hide_featured_products', '__return_zero');
Of course, if you want to enable them, you should use '__return_true' here instead, however the settings page should work for this.
By now, we should be down to the following 8 queries:
SELECT option_value FROM wpstore_options WHERE option_name = '_transient_timeout_wpsc_theme_path' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = '_transient_wpsc_theme_path' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[productspage]%' AND `post_type` = 'page' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[shoppingcart]%' AND `post_type` = 'page' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[transactionresults]%' AND `post_type` = 'page' LIMIT 1 SELECT post_name FROM `wpstore_posts` WHERE `post_content` LIKE '%[userlog]%' AND `post_type` = 'page' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = '_transient_timeout_wpsc_url_wpsc-default.css' LIMIT 1 SELECT option_value FROM wpstore_options WHERE option_name = '_transient_wpsc_url_wpsc-default.css' LIMIT 1
So, first, let’s look at those transients. These transients work by caching where the WPEC theme files exist, to avoid having to check the stylesheet directory, then the template directory, then the default WPEC directory. There are two options to changing this: you can either head into your MySQL database and set the autoload value for these options to yes, or simply hardcode it. Personally, I know where these files are always going to live, so I went with hardcoding:
add_filter('pre_transient_wpsc_theme_path', array(__CLASS__, 'hardcode_wpsc_theme_path'));
add_filter('pre_transient_wpsc_url_wpsc-default.css', array(__CLASS__, 'hardcode_wpsc_theme_url'));
public function rm_hardcode_wpsc_theme_path($value) {
return WPSC_CORE_THEME_PATH;
}
public function rm_hardcode_wpsc_theme_url($value) {
return get_stylesheet_directory_uri() . '/wpsc-default.css';
}
We’ve now hardcoded most things and we’re down to four queries: the shortcode queries. Why does WPEC even need to look these up? Well, in order to create URLs for products, WPEC needs to know the base URL, which is set to the page where your productspage shortcode is set. There’s no easy way to get these, so it has to do a LIKE query across all of your pages. Doing this on each page load is a huge strain though (there is a bug filed about this though, so the developers are aware), especially given that we’re not going to be changing this often.
My favourite way to do this, as you may have noticed, is to hardcode it. Unfortunately, there are no filters on this, so you’ll need a custom patch to WPEC to add support for this. Essentially what the patch does is allow the page names to be set previously. I personally think that wp-config.php is the best place for these to live, but it’s your choice on where it is. Here’s what your code should look like:
global $wpsc_page_titles;
$wpsc_page_titles = array(
'products' => 'store',
'checkout' => 'checkout',
'transaction_results' => 'transaction-results',
'userlog' => 'your-account',
);
(The values should be set to the slug for each page respectively.)
Voilà, we’re down to zero queries from WPEC! This should minimise any extra stress on your MySQL server when it’s really not needed.
Sidenote: Some of these inefficiencies can be patched in WPEC, while others can’t be, due to the nature of hardcoding them. For those that can be patched, I’ll be attempting to work with the WPEC team to help them fix it. A quick site benefits everyone.
Edit: WordPress has __return_zero() built-in, thanks Rarst.
A Huge Platypus, and His Journeys Around India
1st of February
A friend of mine is in India at the moment and keeping a record of his travels on Tumblr. It’s a great read, and I’m insanely jealous of him.
IntenseDebate Is No More!
18th of January
I’ve finally had time to redo some of the styling for this site, so I’ve given comments a makeover and switched back to WordPress’s comment system. Initially, I used IntenseDebate to avoid having to style the comments. It was a good idea, but unfortunately I never got the motivation to finish it off.
Oops!
Explicit Versus Implict Coding
18th of January
Konstantin Kovshenin recently posted on his blog about creating a WP_Plugin class. He posted an example of a class to fit his thoughts around it on Gist, and from there, discussion has taken place on how such a class would be implemented.
There has been a fair bit of discussion on my fork of Konstantin’s code about this, and I’ve been updating the class with new ideas as we come across them.
However, as is usual with discussion regarding any semi-complicated piece of code, there has been some disagreement on how best to hook methods in. Mike Schinkel is a fan of mapping method names directly to hooks, whereas I much prefer prefixing methods that I want hooked with either action_ or filter_. (We both agree that PHPDoc tags are a good idea though, although Mike also adds a @wp-nohook to ignore any methods.)1 I thought I’d further flesh out why I’m not a fan of mapping the methods directly.
Personally, while I see the merit in naming methods for hooks directly, I hate magic. I hate not knowing when my code is used, and I think one of the biggest strengths of WordPress is that this hardly ever happens. If I want something used, I explicitly declare that through add_action/add_filter.
Don’t get me wrong: I love making things easier for myself. One of my favourite pieces of code ever is one written by Morten Fangel that I use in almost everything I do: _sortArgs(). This piece of code will take an associative array, like array('a' => 'b') and map the variables to parameters to my function. Combined with $_GET and $_POST, it’s an extremely powerful tool. However, _sortArgs isn’t really that magical when it comes down to it. I’m specifying which parameters I want, and everything is explicitly written by me.
I can see the same thing with this plugin class. If I prefix a method with action_ or filter_ (or using PHPDoc tags), I’m explicitly stating that I want this hooked. On the other hand, a method like init is completely implicit. It happens to match a WordPress action, but that could be a coincidence.
As an example of where this would be a problem for me: I often write a method like admin_page for whatever page I’m adding to the admin. If I have things spread across several pages, I’ll factorise the common header bits and footer bits into admin_header and admin_footer. Except with implicit hooking, I’ve accidentally just hooked my footer method into the administration footer. Now, I have to undo that by specifying that I don’t want it hooked.
To hook implicitly requires that I know every action/filter in WordPress to avoid conflicting with them.
Even worse than this is that hooking implicitly breaks forward as well as backward compatibility. Let’s say I add a method called after_post which I call from another class in my plugin, so I need it to be a public method. Everything is going well, until WordPress adds a hook into templates for adding content after a post. Oops, suddenly, my plugin breaks through no fault of my own, and through something that core developers shouldn’t (and wouldn’t) have to worry about.
Hooking implicitly breaks compatibility in every direction, and is too magical. It is absolutely not the way to consume a public API.
Sidenote: A discussion also emerged on how to use priorities. Mike and I both agree (I think) on using PHPDoc, while Thomas Scholz prefers preferred suffixing the method (i.e. action_init_2). My problem with this is that distinguishing between an named init with priority 2 and an action named init_2 is impossible.
Edit: Thomas dropped support for priorities in the method name, which I initially missed. Thanks for the correction.
Edit 2: Updated with a footnote about Mike’s position regarding implicit/explicit hooking.
- Mike has informed me that he does support explicit hooking for published code, but implicit hooking for prototyping. I’m not a fan of this either, since I can forsee people forgetting to do so. [↩]
Think Different
6th of October
Here’s to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They’re not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can’t do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do.
We’ll miss you, Steve. Thank you.
What’s Up?
27th of January
And, yet again, I’m falling into the trap of not posting. Despite the fact that I have at least four draft post sitting around, I haven’t had the motivation to post any yet. But never fear, I shall get around to it eventually!
So, here’s a quick update on what’s happening with me. I’ve begun my senior year in high school, with less than 10 months until I graduate. I’ve been slogging away at my projects, as per usual, and I’m hoping to get the SimplePie website fully migrated to Automattic’s servers some time in the next few weeks. The sooner that happens, the sooner I can push out a bug-fix release.
I’ve also had the pleasure of working on YesPlugins with Anthony Cole and Mark Bao, but I can’t reveal too much on this yet. Watch this space, as many things are yet to come. (Interested in working with us? Get in touch.)
I think that’s about it for now. I intend on keeping this journal up-to-date much more than previously, and with some luck, I can stick to that. Until next time, ciao.
Random Antics
27th of August
Quite a while ago, I was online with some friends in Bad Company 2. We decided to make some explosive footage and compile it into a masterpiece. While we wait for producer Newsworthy to finish our actual in-game video, here’s some of the raw footage. Enjoy!
On the Lack of Posting…
21st of August
Yeah, I haven’t been writing here much. More blog posts will be coming as soon as I have time to write them.
Silverchair Covers Yellow Submarine
29th of April
One of the best renditions I’ve heard of Yellow Submarine.
Typography Is Important
15th of April
A crash course to using typography. This is going in my folder with Five Simple Steps.