[wp-hackers] Expensive meta query - Is there a better way?

Casey Bisson casey.bisson at gmail.com
Thu Feb 9 14:13:27 UTC 2012


The post meta is really poorly structured for queries. If I remember correctly, the default schema doesn't even have an index on the value column, but that's just the start of reasons why queries against that table run slowly. 

You can add indexes to the post meta table, but you'd do well to consider using the taxonomies tables instead. Those are very well architected for post selection queries. 

The rules I follow for my sites:

Use postmeta for fielded data displayed once the post is found. 

Use taxonomies to find the posts to display. 

--Casey

On Feb 9, 2012, at 3:57 AM, Robert Lusby <nanogwp at gmail.com> wrote:

> Hello,
> 
> We're running a multiple meta query lookup in WordPress - however the DB query most be expensive as it's result in very slow (20/30 second) response times in PHP with only 300 posts (total) in the DB. Query below.
> 
> Is there a better way of doing the below? Essentially if $value is equal to key1 or key2 or key3 or key4 or key5 (the keys are numbered consecutively).
> 
> 
> 'post_type' => 'custom_post_type',
> 'meta_query' => array(
>        'relation' => 'OR',
>        array(
>            'key' => 'key1',
>            'value' => $value,
>            'compare' => '=',
>        ),
>        array(
>            'key' => 'key2',
>            'value' => $value,
>            'compare' => '=',
>        ),
>        array(
>            'key' => 'key3',
>            'value' => $value,
>            'compare' => '=',
>        ),
>        array(
>            'key' => 'key4',
>            'value' => $value,
>            'compare' => '=',
>        ),
>        array(
>            'key' => 'key5',
>            'value' => $value,
>            'compare' => '=',
>        ),
>    )
> );
> 
> Thanks,
> Rob
> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list