Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In this thread I presented what I thought was a good answer, and @toscho presented one afterwards which to me seemed inferior.

But after running some timed tests, my code turns out to be significantly slower. My code takes >3 times longer to execute, and I can't understand why. Hopefully a WordPress guru can enlighten me.

The OP in that thread only wanted the number of top-level comments, ignoring any nested ones. toscho's function returns the entire rowset of matches, while mine only returns the number of matches.

Here's my code:

function get_num_toplevel_comments() {
    global $wpdb;

    return $wpdb->get_var("
        SELECT COUNT(*) 
        FROM $wpdb->comments
        WHERE comment_parent = 0
    ");
}

And here's toscho's

add_filter( 'comments_clauses', 'wpse_78628_top_comments_only' );
$comments = get_comments();
remove_filter( 'comments_clauses', 'wpse_78628_top_comments_only' );
function wpse_78628_top_comments_only( $clauses )
{
    $clauses['where'] .= ' AND comment_parent = 0';
    return $clauses;
}

P.S. - I also included in my testing SELECT COUNT(comment_ID) instead of *. Both had very similar response times, negligible in fact since my tests are greatly affected by my shared hosting speed. That was also a surprise to me.

I'm far from an expert in MySQL, WordPress, or PHP. But I tend to get anal about any perceived efficiency in my code (and to a lesser extent, others' code) and always strive for efficient solutions.

share|improve this question
1  
What's the question? – Brian Fegter Jan 8 at 14:49
possible duplicate of get_comments_number of depth-1 (Level 1) (1 post) – Brian Fegter Jan 8 at 14:50
5  
My code is running fast because it is afraid of me. – toscho Jan 8 at 14:51
@BrianFegter The other question is not about SQL performance? – toscho Jan 8 at 14:52
1  
From quick test latter code is ~twice slower for me. Please include how are you timing it. – Rarst Jan 8 at 15:07
show 9 more comments

closed as not a real question by Brian Fegter, anu, brasofilo, toscho Jan 9 at 0:59

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

The two functions do very different things. Yours only gets a count while toscho's pulls an array of comment data, some of which require joins on other tables-- the 'author' stuff, for example. I am sure that is where the difference in speed is.

I would lean towards toscho's code because it should be more stable long term-- that is, as core WordPress code changes-- unless this were a site I personally maintain :)

Looks like you can pass get_comments a count parameter though and it should work very much like your.

share|improve this answer
But mine is the one that's significantly slower! Hence my confusion! – akTed Jan 8 at 15:03
1  
There is no way the raw query is slower. You have to be looking a caching issue. Are you running this on a page where comments would have already been pulled? – s_ha_dum Jan 8 at 15:10
I'm not sure what you mean by "pulled". If you mean were my queries cached, I have no idea. If it makes a difference, I don't run any WP page cacching plugins (yet!). – akTed Jan 8 at 15:24
WP_Query looks for comments in some circumstances. If that is the case, the query may not run a second time. Even without external caching PHP, and MySQL if the built-in cache (or any other cache) is enabled, will try to optimize themselves and avoid duplicating work if at all possible. It is the only thing I can think of. – s_ha_dum Jan 8 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.