Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:08:28+00:00 2026-05-25T11:08:28+00:00

I have a query that is working just fine however it is a bit

  • 0

I have a query that is working just fine however it is a bit on the slow-side. After researching a bit I found that using the HAVING part of my query is causing a slow-down. Is there a way to pull this off WITHOUT using the HAVING piece?

$query = sprintf("SELECT p.*,( 3959 * acos( cos( radians('%s') ) *
   cos( radians( `loclat` ) ) * cos( radians( `loclong` ) - radians('%s') ) 
   + sin( radians('%s') ) * sin( radians( `loclat` ) ) ) ) AS distance
     FROM postLocation as pl, posts as p
     WHERE pl.pid = p.id    %s  %s      %s  
     HAVING distance < '%s'
     ORDER BY p.utc DESC, distance ASC LIMIT 0 , %s ",
     $loclat,
     $loclong,
     $loclat,   $status,    $type,  $start,
     $radius,   $limit);

I have the sprintf function to use while still working on the final versoin of my query.. makes it easier for me currently to modify things etc. So I know having a function would add time.. primarily focusing on the query itself… if there is anything else HUGELY wrong I’m all ears but I believe the HAVING part is the trouble maker.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:08:29+00:00Added an answer on May 25, 2026 at 11:08 am

    The culprit is not the HAVING clause per se, but all the transcendental functions (sin, cos, acos) you are using to calculate distance. They are very computationally expensive.

    You can try to get an approximation of the distance by using:

    sqrt(x * x + y * y)
    
    where x = 69.1 * (lat2 - lat1) 
    and y = 53.0 * (lon2 - lon1) 
    

    You can improve the accuracy of this approximate distance calculation by adding the cosine function:

    sqrt(x * x + y * y)
    
    where x = 69.1 * (lat2 - lat1) 
    and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3) 
    

    source: http://www.meridianworlddata.com/Distance-Calculation.asp

    The approximations are much faster and fairly accurate, especially for small distances.
    Either you do that, or get a better server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SOSL query that finds Salesforce Campaigns, and that's working just fine.
I have a Linq to SQL query that was working just fine with SQL
I have this query that works just fine, however I want to change my
I have below a working query that needs to be simplified. The reason is
I have inherited a somewhat messy query that I am working on refactoring to
I am using in C# MYsql .I have query that works if I run
I have a query that I know can be done using a subselect, but
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a text box with autocomplete set and working perfectly fine. However when
I have a Linq query which is working fine but i have noticed when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.