473,787 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggestions for best practice: mysql vs array sorting

I am about to start on a module that will accept a location from
a user, use Google geolocation services to get the lat/lon and
then compute the distance from the site visitor to about 100
kennels (could grow to 1000 eventually).

Once I have the distance I need to sort the kennels by distance
and present those within 500 miles.

Approach 1: read the kennel lat/lon from the mysql kennel record
compute the distance
write the distance back into the kennel record
when done,
using the distance as an index, read back
the kennel data until I get 500 miles.
If I use this approach would I need to lock the database to
prevent a second site visitor from possibly colliding ?

Approach 2: read the kennel lat/lon from the mysql kennel record
compute the distance
put the kennel ID (20 char) and distance into an
associative array
when done,
sort the array
look up kennels by kennel ID that are within 500mi

Approach 3: read the kennel lat/lon from the mysql kennel record
compute the distance
put the data back into the $rows array, proceed as
in approach 2.
I don't know if one can add a column to an already
established array and/or re-use a dummy variable in the array.

I ask for suggestions on the best approach.
The kennel record has a row size of 1.118 bytes

bill
Jan 4 '07 #1
6 3122
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

bill wrote:
Once I have the distance I need to sort the kennels by distance
and present those within 500 miles.
[...]
I ask for suggestions on the best approach.
First, do use MySQL's spatial extensions to store the lat-long coordinates
of every kennel.

Second, do use spatial operators to filter and sort by distance. It should
go like this:

select name,Distance(G eomFromText('') ,kennels.positi on) as dist from kennels
where dist < 1500 sort by dist asc;

Easy, huh? I think that letting the hard work to MySQL is the fastest way.

However, you may run on a problem: the earth is flat, and calculating the
distance on the surface of a (quasi-)sphere is not trivial. You may
research on earth projection models (mercator, etc) to "flatten" out the
earth, so MySQL returns a nice cartesian distance.
The kennel record has a row size of 1.118 bytes
Why? You can't change that or what?

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: Shirley Bassey - The Remix Album ... Diamonds Are
Forever0 - [9] Light My Fire (Twelftree's Lady Mix) (4:04) (95%)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFnUQhR56 dWuhgxGgRAkICAJ 93I7DECfOJ3plsE qE6kZAInt/QYQCgn0CE
DPiFh9QN3iUK6Dq meQd7PKE=
=l9d2
-----END PGP SIGNATURE-----
Jan 4 '07 #2
bill wrote:
I am about to start on a module that will accept a location from a user,
use Google geolocation services to get the lat/lon and then compute the
distance from the site visitor to about 100 kennels (could grow to 1000
eventually).

Once I have the distance I need to sort the kennels by distance and
present those within 500 miles.

Approach 1: read the kennel lat/lon from the mysql kennel record
compute the distance
write the distance back into the kennel record
when done,
using the distance as an index, read back
the kennel data until I get 500 miles.
If I use this approach would I need to lock the database to prevent
a second site visitor from possibly colliding ?

Approach 2: read the kennel lat/lon from the mysql kennel record
compute the distance
put the kennel ID (20 char) and distance into an
associative array
when done,
sort the array
look up kennels by kennel ID that are within 500mi

Approach 3: read the kennel lat/lon from the mysql kennel record
compute the distance
put the data back into the $rows array, proceed as in
approach 2.
I don't know if one can add a column to an already
established array and/or re-use a dummy variable in the array.

I ask for suggestions on the best approach.
The kennel record has a row size of 1.118 bytes

bill
Bill,

The spatial extensions are good, but if you have a large database it
could take significant time to calculate the distance for every row.

Another option:

If you want kennels within 500 miles:

Compute the latitude 500 miles north of your user's location (nlat)
Repeat for 500 miles south (slat)
Do the same for east and west longitude (elong & wlong)

Now use the spacial extensions in MySQL, but also filter on the
longitude and latitude values you computed (in your WHERE clause), i.e.

WHERE long >= elong AND long <= wlong AND lat >= nlat AND lat <= slat

This can filter out a lot of the values before the computations (have an
index on long-lat for even better performance).
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jan 4 '07 #3
Assumption:
1. long/lat of kennels are already stored in database
2. Using MySQL database
3. ##SQRT(bla bla bla)## is the formula to find the distance

MySQL actually provide mathematical functions. So why not use it to your
advantage.
SELECT kennel.*, ##SQRT(bla bla bla)##
FROM kennel
WHERE ##SQRT(bla bla bla)## < 500
ORDER BY ##SQRT(bla bla bla)##

Albeit this will take some processing time for MySQL.
Unless if there is a MySQL buff here that knows how to optimize this further

Hope that helps

Hendri Kurniawan

bill wrote:
I am about to start on a module that will accept a location from a user,
use Google geolocation services to get the lat/lon and then compute the
distance from the site visitor to about 100 kennels (could grow to 1000
eventually).

Once I have the distance I need to sort the kennels by distance and
present those within 500 miles.

Approach 1: read the kennel lat/lon from the mysql kennel record
compute the distance
write the distance back into the kennel record
when done,
using the distance as an index, read back
the kennel data until I get 500 miles.
If I use this approach would I need to lock the database to prevent
a second site visitor from possibly colliding ?

Approach 2: read the kennel lat/lon from the mysql kennel record
compute the distance
put the kennel ID (20 char) and distance into an
associative array
when done,
sort the array
look up kennels by kennel ID that are within 500mi

Approach 3: read the kennel lat/lon from the mysql kennel record
compute the distance
put the data back into the $rows array, proceed as in
approach 2.
I don't know if one can add a column to an already
established array and/or re-use a dummy variable in the array.

I ask for suggestions on the best approach.
The kennel record has a row size of 1.118 bytes

bill
Jan 5 '07 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry Stuckle wrote:
Now use the spacial extensions in MySQL, but also filter on the
longitude and latitude values you computed (in your WHERE clause), i.e.

WHERE long >= elong AND long <= wlong AND lat >= nlat AND lat <= slat
No, you shouldn't do that. If you're using spatial extensions, then define a
bounding box (with GeomFromText('P OLYGON blahblahblah') ), then use a
spatial MBR intersection function to filter the data.

The result will be the same (filtering the points by a rectangle centered on
a point). However, by *only* using spatial functions, the Q-trees
algorithms can kick in, and filter the data faster than using four
non-spatial comparisons. After all, that's why Q-trees and spatial stuff
was invented.

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Well, if you're not running Windows, how the hell else are you supposed to
get memory leaks? They don't just grow on B-Trees, y'know!
-- Tackhead, on Slashdot.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFncO5R56 dWuhgxGgRAvvoAJ 9D2WBkImlUvuiXY Bc8q76ryiai9ACf Vfcv
vG4d6yRqt9IldEA iqDrwJ60=
=8y+S
-----END PGP SIGNATURE-----
Jan 5 '07 #5
bill wrote:
I am about to start on a module that will accept a location from a user,
use Google geolocation services to get the lat/lon and then compute the
distance from the site visitor to about 100 kennels (could grow to 1000
eventually).

Once I have the distance I need to sort the kennels by distance and
present those within 500 miles.

Approach 1: read the kennel lat/lon from the mysql kennel record
compute the distance
write the distance back into the kennel record
when done,
using the distance as an index, read back
the kennel data until I get 500 miles.
If I use this approach would I need to lock the database to prevent
a second site visitor from possibly colliding ?

Approach 2: read the kennel lat/lon from the mysql kennel record
compute the distance
put the kennel ID (20 char) and distance into an
associative array
when done,
sort the array
look up kennels by kennel ID that are within 500mi

Approach 3: read the kennel lat/lon from the mysql kennel record
compute the distance
put the data back into the $rows array, proceed as in
approach 2.
I don't know if one can add a column to an already
established array and/or re-use a dummy variable in the array.

I ask for suggestions on the best approach.
The kennel record has a row size of 1.118 bytes

bill

The record size 1,118 bytes was just information.

Thanks to all who made suggestions.
Are the MySql spatial extensions part of 4.1.2 (which is what my
provider provides) or an extension that I need to install ?

the formula for distance is:

// Convert lattitude/longitude (degrees) to radians for
calculations
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);

// Find the deltas
$delta_lat = $lat2 - $lat1;
$delta_lon = $lon2 - $lon1;

// Find the Great Circle distance
$temp = pow(sin($delta_ lat/2.0),2) + cos($lat1) *
cos($lat2) * pow(sin($delta_ lon/2.0),2);
$distance = 3956 * 2 * atan2(sqrt($tem p),sqrt(1-$temp));
Jan 5 '07 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

bill wrote:
Are the MySql spatial extensions part of 4.1.2 (which is what my
provider provides) or an extension that I need to install ?
The spatial extensions are avaiable by default, in MySQL 4.1 and higher.

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Afternoon very favorable for romance. Try a single person for a change.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFFnmW6R56 dWuhgxGgRAjZgAJ 9PNrbBfiqhxCYqQ KpmI/6dqr+rGQCfdPvg
cbP6I46fbViv5xe yrTh46pc=
=OV7v
-----END PGP SIGNATURE-----
Jan 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6372
by: John Hicks | last post by:
Is there an accepted best practice on whether to store decimal currency amounts (e.g. dollars and cents) in MySQL decimal column types? Certainly, the most straightforward way is to use decimal columns. But it appears that such values are stored as ASCII strings, which would be inefficient for calculations (requiring conversion to a numeric type for each calculation).
0
1286
by: Matt W | last post by:
Hi all, I'm planning to use MySQL's full-text search for my forum system (possibly 5+ million posts). I've been playing with it a lot lately to see the performance and functionality and have some suggestions/questions. First, since a few of you may be wanting to know, here is a thread where I was doing some speed/optimization tests and stuff with 3 million posts: http://www.sitepointforums.com/showthread.php?threadid=69555
11
9273
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
3
3334
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any resources that explains how to optimize code i.e. give some rules on c++ optimization? e.g. using memcpy to copy an array (which i have done). Also, what is the best sorting algorithm out there for sorting an array of of size 100 or less? I have...
193
9649
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
5
1516
by: Dan | last post by:
I wonder if anyone has suggestions for reducing the amount of time it would take to search my array using the function that I have written. I want to find a position in the array of an item that matches on all three variables. Suggestions? Public Shared Function GetArrayPosition(ByVal ipRemoteEndPoint As String, ByVal intEngine_Type As Integer, ByVal intEngine_ID As Integer) For i As Integer = 0 To aryDevice.GetUpperBound(1) If...
21
8153
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the maximum value N 2. loop through 1...N 3. count # times each occurred
4
929
by: al jones | last post by:
I need to create a collection of data and I'm not sure what approach to take - any suggestions appreciated. For example, using mp3's, If I collect: 1) song title 2) file name 3) producer 4) artist 5) bit rate and the user wants to see one list sorted by artist and another by title
10
2483
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the struct in the header file and including this file in all source files that need it? For example: mystruct.h
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.