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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:07:01+00:00 2026-05-20T10:07:01+00:00

I have a system which will return all users from the database and order

  • 0

I have a system which will return all users from the database and order the results by lowest distance from a reference zip code.

For example: User will come on the site, enter zip code and it will return him all other users who are nearest to his zip (ascending order)

How am i doing this now and why is it a problem ?

The system contains more than 30 million users and their zipcodes. I am retreiving all the users in a particular state and city (narrows the dataset down to about 10,000).

This is where the problem actually happens. Now, all the result sent by mysql (10,000) rows to PHP are sent to a zipcode calculator library which calculates this distance between the base zip code and user’s zip code – 10,000 times. Then orders the result by the zip code nearest.

As you can see, this is very badly optimized code. And the 10,000 records are looped through twice. Not to mention the amount of RAM each httpd process takes just transferring data to and fro mysql.

What I would like to ask the gurus in here that is there anyway to optimize this ?

I have a few ideas of my own, but i’m not sure how efficient they are.

Try to do all the zipcode calculation and ordering in mysql itself and return the paginated number of rows.
For this, i will need to move the distance between zipcode calculation logic to a stored procedure. This way I am preventing the processing of 10,000 records in PHP. However, there is still a problem. I would not need to calculate distance for zip codes which have already been calculated (for 2 users having the same zip code).

Secondly, how do i order rows in mysql using a stored procedure ?

What do you guys think ? Is this a good way ? Can i expect a performance boost using this ?
Do you have any other suggestions ?

I know this question is huge, and i really appreciate the time you have taken to read till the end. I would really like to hear your thoughts about this.

  • 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-20T10:07:02+00:00Added an answer on May 20, 2026 at 10:07 am

    As I’m not overly familiar with PHP or MySQL, I can only give some basic tips but they should help. This also assumes you have no direct way of interfacing with the zip library from MySQL.

    First, as it’s doubtful that you have 10k zip codes in a city, take your existing query and do something like

    SELECT DISTINCT ZipCode FROM Users WHERE ...
    

    This will probably return a few dozen zip codes max, and no duplicates. Run this through your zip code library. That library itself is probably a source of slowness, as it has to look up the zip codes, and do a bunch of fancy trig to get actual distance. Take the results of this, and insert it into a temp table with just the zip code and the distance.

    Once done with that list, have another query that gets the rest of the user data you want, and JOIN into the the temp table on zip code to get your distance.

    This should give you quite a large speedup. You can do whatever paging you need in the second query after the results have been calculated. And no more looping through 10k rows.

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

Sidebar

Related Questions

Consider an automated build system that stores results in a database and offers tabular
I have two views which will both use the same Controller method: //webServiceController.cs //The
I want to write an user defined error collection class which should collect all
I'm working on a website, and I'm implementing a friend system. People can become
I'm trying to get a list of the real users on the local machine.
I'm currently working on a solution, where there are the concepts of users, work
I am currently working on a private messaging system. Right now I'm coding the
I'm not sure if this is even possible or not, and I can't quite
I'm attempting to use jQuery's autocomplete feature, and after reading several posts I still
I work on mvc.net application. Client is going to move production on a cluster

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.