Organize Series plugin database optimization Comments RSS feed

While looking for posts in the series, this plugin makes two database queries for each post in the series : one to find out if the post is published, and another to get the part number in the series. So if you have 30 posts in the series, you will get 60 queries from this plugin alone on each post belonging to that series. I found this very wasteful, and rewrote one function so that it only uses one database query, no matter what the number of posts in the series. I think the author should be interested…

Basically, in the file series-utility.php, replace the function
function get_series_order($posts, $postid = 0, $skip = TRUE)
by the following one :

<?php
function get_series_order($posts, $postid = 0, $skip = TRUE) {
global $wpdb;
if (!isset($posts)) return false; //don’t have the posts array so can’t do anything.

if ( !is_array( $posts )
$posts = array($posts);

$series_posts = array();
$key = 0;

foreach ($posts as $spost) {
if (array_key_exists(‘object_id’, $posts)) {
if ($key!=0){
$spost_id .= “,”.$spost['object_id'];
}else{
$spost_id .= $spost['object_id'];
}
} else {
if ($key!=0){
$spost_id .= “,”.$spost;
}else{
$spost_id .= $spost;
}
}
$key++;
}

//Optimized : 1 SQL query instead of 2* number ofposts in series//
$results=$wpdb->get_results(“SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value
FROM $wpdb->posts LEFT JOIN $wpdb->postmeta
ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE post_status = ‘publish’ AND $wpdb->posts.ID
IN (“.$spost_id.”) AND ener_postmeta.meta_key=’”.SERIES_PART_KEY.”‘”);
/* 208 – fix by Matt Porter – to make sure unpublished posts are not made part of a series */
foreach ($results as $num=>$result) {
//echo “<h1>resultat”.$result->ID.$result->meta_value.”</h1>”;
$series_posts[$num]['id'] =$result->ID;
$series_posts[$num]['part'] = $result->meta_value;
}

if (count($series_posts) > 1)
usort( $series_posts, ‘_usort_series_by_part’ );

return $series_posts;
}
?>

And there you go, a faster blog…

Liked it? Share it!
  • del.icio.us
  • Facebook
  • Google
  • TwitThis
  • StumbleUpon

2 Responses to “Organize Series plugin database optimization”

  1. Darren says:

    Good work! I’ve bookmarked this page for review when I find the time to do a pass over the Organize Series code for updating :)

    Thanks!

  2. Thanks!
    I hope you don’t take too long, i’m refraining from updating so I don’t have to do everything over again.
    BTW, I saw that your plugin was doing a lot of SQL queries after reading a post on yoast.com about database optimization, maybe you should check it out, interesting stuff.

Leave a comment

If you register, these will be filled automatically