patternsqlModerate
Decimal or Point Data Type for storing Geo location data in MySQL
Viewed 0 times
storingpointdecimaltypemysqlgeofordatalocation
Problem
I want to store Geo location for each Address in my MySQL(Innodb) "tblAddress" table and I think I have 2 Options:
My program need to find all Address that fall under 1 Kilometer Radius from current Location (Mobile Client).
Also below command does not work for me, not sure what I need to have for this.
So what is the suggested approach to store data for faster calculation and retrieval here. Please advise.
1) 2 columns for Each Address:
Latitude DECIMAL(10, 8),
Longitude DECIMAL(11, 8)
2) One Column for Each Address:
GeoLocation POINTMy program need to find all Address that fall under 1 Kilometer Radius from current Location (Mobile Client).
Also below command does not work for me, not sure what I need to have for this.
CREATE SPATIAL INDEX GeoLocation ON tblAddress (GeoLocation);So what is the suggested approach to store data for faster calculation and retrieval here. Please advise.
Solution
You could use MySQL's spatial extensions with GIS.
In a code example from Google displaying points on a map, they state:
When you create the MySQL table, you want to pay particular attention
to the lat and lng attributes. With the current zoom capabilities of
Google Maps, you should only need 6 digits of precision after the
decimal.
To keep the storage space required for our table at a minimum, you can
specify that the lat and lng attributes are floats of size (10,6).
That will let the fields store 6 digits after the decimal, plus up to
4 digits before the decimal, e.g. -123.456789 degrees I wouldn't worry
about performance differences between numeric types. Decent indices
will have a far greater effect.
You could also try
exact arithmetic. Unlike
number, so by using it instead of
errors when doing some calculations. If you were just storing and
retrieving the numbers without calculation then in practice
would be safe, although there's no harm in using
calculations
8d.p. precision you should use
Latitudes range from -90 to +90 (degrees), so
that, but longitudes range from -180 to +180 (degrees) so you need
and the second is the number after the decimal point.
In short:
This explains how MySQL works with floating-point data-types.
Related information:
In a code example from Google displaying points on a map, they state:
When you create the MySQL table, you want to pay particular attention
to the lat and lng attributes. With the current zoom capabilities of
Google Maps, you should only need 6 digits of precision after the
decimal.
To keep the storage space required for our table at a minimum, you can
specify that the lat and lng attributes are floats of size (10,6).
That will let the fields store 6 digits after the decimal, plus up to
4 digits before the decimal, e.g. -123.456789 degrees I wouldn't worry
about performance differences between numeric types. Decent indices
will have a far greater effect.
You could also try
DECIMAL as @gandaliter suggest. DECIMAL is the MySQL data-type forexact arithmetic. Unlike
FLOAT its precision is fixed for any size ofnumber, so by using it instead of
FLOAT you might avoid precisionerrors when doing some calculations. If you were just storing and
retrieving the numbers without calculation then in practice
FLOATwould be safe, although there's no harm in using
DECIMAL. Withcalculations
FLOAT is still mostly ok, but to be absolutely sure of8d.p. precision you should use
DECIMAL.Latitudes range from -90 to +90 (degrees), so
DECIMAL(10,8) is ok forthat, but longitudes range from -180 to +180 (degrees) so you need
DECIMAL(11,8). The first number is the total number of digits stored,and the second is the number after the decimal point.
In short:
lat DECIMAL(10,8) NOT NULL, lng DECIMAL(11,8) NOT NULLThis explains how MySQL works with floating-point data-types.
Related information:
- MySQL and GIS, GeoIP: FAQs, How-To, Articles, Blogs
- Creating a Store Locator with PHP, MySQL & Google Maps
- Using the new spatial functions in MySQL 5.6 for geo-enabled applications
Context
StackExchange Database Administrators Q#107089, answer score: 12
Revisions (0)
No revisions yet.