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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:25:32+00:00 2026-05-27T08:25:32+00:00

I have a geo_locations table that looks like this: country | city | postalCode

  • 0

I have a “geo_locations” table that looks like this:

    country | city    | postalCode | latitude | longitude | metroCode | areaCode
    -------------------------------------------------------------------------
    US      | Chadler | 85226      | 33.2769  | -111.9444 | 753       | 480

more records...

And a “users” table that looks like this:

 user_id | sex | dob        | country | region | city     | zip          | latitude | longitude | email       | username | password

 --------------------------------------------------------------------------------------------------------------------------------------
 1       | m   | 1987-05-14 | US      | NY     | Flushing | 11398        | 40.7723  | -73.8722  | foo@bar.com | HiBye99  | 54524sAS%ASa2?&^312

more records...

My application requires that I recommend users to other users, for meetups, making friends, etc. I must recomend a user to other users who are within
their area. It doesn’t make sense to recomend someone from Bejing to someone in New York for example.

To achieve this I am using the following sql:

            SELECT postalCode, latitude, longitude, ACOS(SIN($lat) * SIN(RADIANS(latitude)) + COS($lat) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - $lon)) * $radius AS D
            FROM (
            SELECT postalCode, latitude, longitude
            FROM geo_locations
            WHERE latitude > $min_lat AND latitude < $max_lat AND longitude > $min_lon AND longitude < $max_lon
            ) AS FirstCut
            WHERE ACOS(SIN($lat) * SIN(RADIANS(latitude)) + COS($lat) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - $lon)) * $radius < $rad
            ORDER BY D

Before that sql, some fancy calculations are done on the $lat, $lon, $rad, $radius, $min_lat, $max_lat, $min_lon and $max_lon variables. Full code can be
seen here where I grabbed the code from:

http://www.movable-type.co.uk/scripts/latlong-db.html

Anyway what this sql returns in my code is a collection of all the “postal codes” that are close to the users area. So I then use those zip codes to build out another sql query which
usually looks crazy like this (SO wouldn’t let me submit the question because it was so huge):

http://jsfiddle.net/dgmHa/

And after running it on the users table I get my list of recommended users.

Question:

Now as you can see I have the latitude and longitude columns on the users table as well. So there’s really no need to select anything from the “geo_locations” table.
How can I alter my query so I can get all my recommended users directly from the “users” table in one query?

Also is my way of doing this whole thing a performance nightmare, is there a better way to go about the whole thing?

  • 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-27T08:25:32+00:00Added an answer on May 27, 2026 at 8:25 am

    To answer your first question, you should be able to write the query as:

        SELECT      user_id,
                    zip, 
                    latitude, 
                    longitude, 
                    (ACOS(SIN($lat) * SIN(RADIANS(latitude)) + COS($lat) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - $lon)) * $radius) AS SearchRadius 
        FROM users
        WHERE (latitude > $min_lat AND latitude < $max_lat) 
        AND (longitude > $min_lon AND longitude < $max_lon)
        AND (ACOS(SIN($lat) * SIN(RADIANS(latitude)) + COS($lat) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - $lon)) * $radius) < $rad 
        ORDER BY SearchRadius
    

    i.e. you can effectively ignore the geo_locations table and just select all of the same columns from users.

    As for the second part, to be perfectly honest, the best way to decide this is to do some testing. Populate the users table with a large amount of information and measure how long the query takes. Then duplicate the number of records and retest. That way you can see the impact more data has on your query.

    Alternatives would include just selecting the raw data out from the database and doing the calculations in code. Again, you’d have to test to see the performance benefits/negatives.

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

Sidebar

Related Questions

I have a table which contains Geo-Based information. (latitude and longitude) i would like
I have a XML like this: <StateTree> <State ID=01> <Name>State1</Name> <CityList> <City ID=01 Order=1
I have a table of users that I want other users to be able
Development language and DB: PHP/MySQL I have a table geo_places with something like 8
I have compiled a list of locations with latitude and longitude information. Can I
I have a json object collection of geo locations that I build in the
I have a php loop that is echoing out geolocation values. How can I
I currently have implemented an overlay item that shows an icon for Geo-point on
I have a query that searches for the all rows in some distance according
I have a table containing a number of records with their long / lat

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.