Can ordering post list by meta_value cause performance issue?

For some code refinement and to add new features to my plugin, I want to display posts ordered by specific meta column value like this;

query_posts($query_string . '&meta_key=views&orderby=meta_value&order=DESC');

Can it cause performance issue for big blogs having total post more than 10000+?

  • I can use WP_Query() or get_posts() in place of query_post()

Related posts

Leave a Reply

1 comment

  1. meta_query & tax_query behavior

    When you’re defining a meta/tax_query and make use of the new array style argument list for multiple sets of term or custom field keys, then your query might look close to the following

    array(
      'tax_query' => array(
        'relation' => 'OR',
        array('taxonomy' => 'tax1', 'field' => 'slug', 'terms' => 'term1'),
        array('taxonomy' => 'tax2', 'field' => 'slug', 'terms' => 'term2'),
      )
    )
    

    This would be the result:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts  
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
    INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) 
    WHERE 1=1 AND
    ...
    AND (wp_term_relationships.term_taxonomy_id IN (XXX) 
      OR tt1.term_taxonomy_id IN (YYY) ) 
    ...
    

    Example code taken from this trac ticket by @Otto. Please subscribe there to follow its progress and vote for getting a change into core.

    The problem with the query …

    … basically is that you’ll get a JOIN for every column. It’s not necessary, as the tables are already melted together with the 1st JOIN. Sadly core simply behaves like that at the current state 3.3.x and will stay like this in 3.4.

    Maybe a solution?

    Your best chance would be to intercept the posts_clauses filter in your plugin and manually change the query. The real problem with this solution is that you’d have to do a str_replace( $search, $replace, $query ); in your plugin. On the one hand, that’s maybe slowing things down and on the other hand you’d have to follow this ticket as when this moves in, your plugin will simply break (better leave a link to the ticket in your plugins code).

    What can I do?

    Jump into trac and add your thoughts & patches to the ticket. If you’re a real hero, then you could try to get at the meta_query problem too.

    Community would thank you 🙂

    If you think this img is inappropriate, just delete it.