title: Wordpress SQL query too slow
body: <p>In wordpress, I want to get posts by longitude and latitude, but the server is responding in 20-25 seconds.</p>
  
  <pre><code>$querystr = "
  SELECT
  ACOS( SIN( RADIANS( $user_lat ) ) * SIN( RADIANS( pm1.meta_value ) ) + COS( RADIANS( $user_lat ) ) * COS( RADIANS( pm1.meta_value ) ) * COS( RADIANS( pm2.meta_value ) - RADIANS( $user_long ) ) ) *3959 AS distance,
  pm1.meta_value AS lat, pm2.meta_value AS lon,
  p.*
  FROM wp_posts p
  INNER JOIN wp_term_relationships ON ( p.id = wp_term_relationships.object_id )
  INNER JOIN wp_postmeta pm1 ON p.id = pm1.post_id AND pm1.meta_key = 'latitude'
  INNER JOIN wp_postmeta pm2 ON p.id = pm2.post_id AND pm2.meta_key = 'longitude'
  
  WHERE post_type = 'post'
  AND post_status = 'publish'
  HAVING distance &lt; 25
  ORDER BY distance ASC
  LIMIT 0 , 30
  ";
  
  
  $results = $wpdb-&gt;get_results($querystr);
  </code></pre>