title: Very slow query on wordpress caused by meta query
body: <p>I am trying to add in a slideshow 2 types of slides based on 2 different keys, just that the meta query is extremely slow. The wordpress query that I am using is:</p>
  
  <pre><code>$args = array(
      'meta_query' =&gt; array(
          'relation' =&gt; 'OR',
          array(
              'key' =&gt; 'sgt_slide',
              'value' =&gt; 'on',
              'compare' =&gt; '='
          ),
          array(
              'key' =&gt; 'sgt_slide_home',
              'value' =&gt; 'on',
              'compare' =&gt; '='
          )
      ),
      'no_found_rows' =&gt; true, //exclude unnecessary paging calculations
      'numberposts' =&gt; -1,
      'post_status' =&gt; array( 'publish', 'inherit' ),
      'post_type' =&gt; array( 'post', 'attachment' )
      );
  $slides = get_posts($args);
  </code></pre>
  
  <p>and the sql matching query that results is</p>
  
  <pre><code>SELECT   nd_posts.* FROM nd_posts  INNER JOIN nd_postmeta ON (nd_posts.ID = nd_postmeta.post_id)
  INNER JOIN nd_postmeta AS mt1 ON (nd_posts.ID = mt1.post_id) WHERE 1=1  AND nd_posts.post_type IN ('post', 'attachment') AND ((nd_posts.post_status = 'publish' OR nd_posts.post_status = 'inherit')) AND ( (nd_postmeta.meta_key = 'sgt_slide' AND CAST(nd_postmeta.meta_value AS CHAR) = 'on')
  OR  (mt1.meta_key = 'sgt_slide_home' AND CAST(mt1.meta_value AS CHAR) = 'on') ) GROUP BY nd_posts.ID ORDER BY nd_posts.post_date DESC
  </code></pre>
  
  <p>This sql resulted query loads in 7 seconds, tested in phpmyadmin. How can I overcome this.</p>
  
  <p>EXPLAIN SQL</p>
  
  <p><img src="https://i.stack.imgur.com/EnKGM.jpg" alt="explain sql"></p>