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 8030033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:37:58+00:00 2026-06-05T00:37:58+00:00

I am very new to MySQL and databases in general but I have a

  • 0

I am very new to MySQL and databases in general but I have a query that seems to be taking a long time. I am finding locations from a location database of about 700,000 that are within 5 miles of another location using lat and lon coordinates. The problem is, it’s taking 2.12 seconds for the query and I’m worried once I start getting traffic, MySQL will jam. Here is my code:

SELECT *,((ACOS(SIN(44.4726 * PI() / 180) * SIN(lat * PI() / 180) + COS(44.4726 * PI() /          180) * COS(lat * PI() / 180) * COS((-93.1785 - lon) * PI() / 180)) * 180 / PI()) * 60 *   1.1515) AS distance FROM locations HAVING distance<=5 ORDER BY distance ASC LIMIT 30;

I have the lat and lon fields indexed but it still takes a long time. Is this to be expected with what I’m asking the server to do? Could I speed it up by adding

WHERE state = "$state"

And if so where would I add that in the Select?

  • 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-06-05T00:38:00+00:00Added an answer on June 5, 2026 at 12:38 am

    That query is going to seq scan the entire table because there’s really not a filter on it that doesn’t derive from a calculated value (distance). Adding a where clause that filters by an indexed column will definitely help eliminate some overhead, but only if the DB believes that there’s enough data in the table to warrant using the index instead of the table. Thus, make sure you analyze it as well.

    The logic with the distance being in the query is incredibly ugly, but I can see why you wouldn’t want to transmit 700k+ rows over the wire every time you select from the table. It looks like you may be doing spatial calculations and might be wise to investigate spatial data types and indexes.

    Ed: Also, your question about a where clause…

    select fields [aggregate fields]
    from table
    where where clause
    group by fields
    having having clause
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to MySQL and I have a stored procedure that I'd like
Ok I'm very new to databases and C# in general, but I'm using a
Very new to XSL (and XML for that matter), but I need to step
New to mysql and VERY new to PDO - But am trying to learn
I'm very new to MySQL, although I've used SQL databases in other contexts before.
I am very new to PHP; ADD.PHP - I have a form that collects
I am very new to mysql and databases, so I need help here... How
I am very new to PHP and MySQL and I have some questions. First,
Before reading, please note that I am very new to both PHP and MYSQL.
I am very new to PHP and MySql. Is there any method by which

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.