I’m working on a location based website within WordPress. However, the query for pulling events within a range can take some time when the range is increased. I’ve had the query take upwards on 10 seconds to run on appox. 65,000 events.

I’ve got the outputted SQL below, if anyone can see a glaring error as to why it might be taking a long time to run and can offer suggestions as to make it quicker, please let me know.

SELECT  DISTINCT wp_posts.*, ( 3959 * acos(
    cos( radians(52.486243) ) *
    cos( radians( latitude.meta_value ) ) *
    cos( radians( longitude.meta_value ) - radians(-1.890401) ) +
    sin( radians(52.486243) ) *
    sin( radians( latitude.meta_value ) )
) ) AS distance , latitude.meta_value AS latitude , longitude.meta_value AS longitude  
FROM wp_posts  
INNER JOIN wp_postmeta AS latitude ON wp_posts.ID = latitude.post_id  
INNER JOIN wp_postmeta AS longitude ON wp_posts.ID = longitude.post_id  
WHERE 1=1  
AND wp_posts.post_type = 'event' 
AND ((wp_posts.post_status = 'publish')) 
AND latitude.meta_key="lat"  
AND longitude.meta_key="long"  
HAVING distance <= 25   
ORDER BY distance ASC, wp_posts.post_date DESC

Read more here: Optimise geolocation SQL Query


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , ,

Wordpress related questions and answers: