I have a blog with about 200k posts and each one have 20 custom fields.

I've been using WordPress Transients_API to cache queries, in the few cases where it can be used, but it can't be used in some other queries due to the nature of the blog.

My blog is http://fixapk.com

There is a problem when accessing applications (single.php).

My code to retrieve the relevant articles using the custom field *'_similar_apps'*, is as follows:

$args = array(
  'showposts' => 10,
  'post_type' => 'post',
  'meta_query' => array(
    'relation' => 'OR',
       'key' => '_similar_apps',
       'value' => $mainlink,
       'compare' => 'LIKE'
$q = new WP_Query( $args );

if ( $q->have_posts() ) : while( $q->have_posts() ): $q->the_post();
  // Do something....

But it is very slow.

You can visit following link to open an article and see its speed


...it takes about 6-10 second to load.

So, how can I optimize queries with meta_query?

Thank you very much!

  • I loaded the link but am not seeing a lot of content on that page. Can you elaborate what area is loading the content? Also can you articulate why you aren't able to use a caching plugin? If your queries are really that expensive a plugin like W3TC or WP Super Cache could help reduce the load time by pre-populating the cache. I know that doesn't resolve the query optimization question but it will assist with the time to load. – codearachnid Apr 12 '14 at 17:48
  • what does _similar_apps contain? do you have to use a LIKE comparison to get the results you need? – Milo Apr 12 '14 at 18:25
  • index your database properly if required use mysql partitioning. – Subodh Ghulaxe Apr 12 '14 at 19:24
  • The load time does not look to be WOrdPress to me. It looks like Discuss and ggpht.com (which looks to be controlled by Google). – s_ha_dum Apr 13 '14 at 15:24
  • What you're trying to do is inherently expensive, meta queries aren't fast when using large data sets, and LIKE queries aren't fast either – Tom J Nowell Jun 9 '15 at 1:16

It is recommended to stick with WP_query for grabbing Wordpress posts; but, you can build your own queries directly from the postmeta table, then iterate through the results.

Depending on the query, this may be faster (Not often; Wordpress works hard to make their database queries as fast as they can be).

global $wpdb;
$table = $wpdb->prefix . 'the_table_you_want';
$sql = $wpdb->prepare( 'SELECT id FROM ' . $table . ' WHERE some_field = %s AND another_field = %d;', 'some_value', 42 );
$results = $wpdb->get_results( $sql );

foreach( $results as $result ):

    //do stuff with $result ( it is an object, not an array )       


ALWAYS use prepare when constructing your own queries.

  • Thanks Douglas.Sesar, I tried it but not faster – quyet Apr 13 '14 at 1:04
  • I try with query: $table = $wpdb->prefix . 'post_meta'; $sql=$wpdb->prepare( "SELECT post_id from $table WHERE meta_key=%s AND meta_value LIKE %s LIMIT 5;",'_similar_apps', '%' . $similar. '%',OBJECT); $results = $wpdb->get_results( $sql ); foreach( $results as $post ): // get post by post_id endforeach; it seems faster but not much – quyet Apr 13 '14 at 2:34
  • Can you try it with = instead of LIKE ? That would speed it up more ( if you do not absolutely need LIKE). – Douglas.Sesar Apr 14 '14 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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