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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:11:27+00:00 2026-06-17T00:11:27+00:00

I have two tables, say A and B that contain city information with two

  • 0

I have two tables, say A and B that contain city information with two columns: latitude and longitude. A contains 100,000 records and B contains 1,000,000 records. My objective is to find the rows of B that are within 1 kilometre from A (for each row in A). How do I go about doing this efficiently? I am targeting a time of less than 30 minutes.

The following query takes forever (which I believe is the result of the cross-product of 100,000 * 1,000,000 = 100 billion row comparisons!):

select *
from A
inner join B
on is_nearby(A.latitude, A.longitude, B.latitude, B.longitude)

is_nearby() is just a simple function that finds the difference between the latitudes and longitudes.

I did a test for one row of A, it takes about 5 seconds per row. By my calculation, it is going to take several weeks for the query to finish execution, which is not acceptable.

  • 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-17T00:11:28+00:00Added an answer on June 17, 2026 at 12:11 am

    Yes, PostGIS will make things faster, since it (a) knows how to convert degrees of latitude and longitude to kilometres (I’ll use the geography type below), and (b) supports a GiST index, which is optimal for GIS.

    Assuming you have PostGIS version 2 available on your system, upgrade your datbase and tables:

    CREATE EXTENSION postgis;
    
    -- Add a geog column to each of your tables, starting with table A
    ALTER TABLE A ADD COLUMN geog geography(Point,4326);
    UPDATE A SET geog = ST_MakePoint(longitude, latitude);
    CREATE INDEX ON A USING GIST (geog);
    --- ... repeat for B, C, etc.
    

    Now to find the rows of B that are within 1 kilometre from A (for each row in A):

    SELECT A.*, B.*, ST_Distance(A.geog, B.geog)/1000 AS dist_km
    FROM A
    JOIN B ON ST_DWithin(A.geog, B.geog, 1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two Tables, Lunch and Dinner. I know that both contain
I have two tables say Table1 and Table2 that contains the following column with
Let's say I have two tables that look like this: TH TH TH TH
Lets say I have two tables - Cat and Cat owner that are linked
I have two tables with similar columns - let's say table A with column
I have two tables, lets say table1 and table2 with common columns, id and
Have two tables say ABC and XYZ and contain one column which data will
I have two tables, one contains a lot of product information such as product_name
Ok say I have two tables Product and Sales The product contains the id,
I have two MySQL tables say A and B. A contains just one varchar

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.