title: Finding all users within X miles calculation via mysql is slow
body: <p>I'm having trouble with this query. It takes about 5 seconds to run against ~300 users. I assume it's because it's calculating the distance for every possible user.</p>
  
  <p>Is there a way for me to optimize this to make it run fast? Thanks in advance.</p>
  
  <pre><code>select
          t2.*,
          t2.city,
          t2.state,
          t2.county,
          ifnull(round((6371 * acos( cos( radians('32.7211') ) * cos( radians( t2.latitude ) ) * cos( radians( t2.longitude ) - radians('-117.16431') ) + sin( radians('32.7211') ) * sin( radians( t2.latitude ) ) ) ),0),1) AS distance
       from
          users t1
       inner join
          zipcodes_coordinates t2
          on t1.zip_code=t2.zipcode
       having
          distance &lt;= 150
  </code></pre>