Calculating Distances Between Geographic Points with MySQL 5.7

Calculating Distances Between Geographic Points with MySQL 5.7

MySQL 5.7 provides the ST_Distance_Sphere function for calculating the great-circle distance between two geographic points.

Takahiro Iwasa
4 min read

MySQL 5.7 introduced the ST_Distance_Sphere function, which computes the great-circle distance between two geographic points without requiring a manual implementation of the Haversine formula.

Important

The arguments to ST_Distance_Sphere must be specified in longitude, then latitude order, which is the reverse of the conventional “latitude, longitude” notation. Reversing this order yields an incorrect result without raising an error.

Short Distance Example

The following example calculates the distance between two points in Osaka approximately 2 km apart.

SELECT
ST_Distance_Sphere(
GeomFromText('POINT(135.507260 34.693946)'),
GeomFromText('POINT(135.526201 34.687316)')
) AS distance_meter
FROM
dual;
rowdistance_meter
11882.1360099034516

Long Distance Example

To test the function over a longer, independently verifiable distance, this example uses JR Osaka Station and JR Tokyo Station, which are approximately 400 km apart.

SELECT
ST_Distance_Sphere(
GeomFromText('POINT(135.495951 34.702488)'), -- JR Osaka station
GeomFromText('POINT(139.767052 35.681168)') -- JR Tokyo station
) AS distance_meter
FROM
dual;
rowdistance_meter
1403048.2752256764

Near the Poles Example

To examine how the spherical approximation behaves at high latitudes, this example uses two points in Svalbard, a region well above the Arctic Circle.

SELECT
ST_Distance_Sphere(
GeomFromText('POINT(16.379258 78.655621)'), -- Pyramiden Container Hostel
GeomFromText('POINT(16.328528 78.655143)') -- Hotel Tulpan
) AS distance_meter
FROM
dual;
rowdistance_meter
11110.8932928975748

Difference from ST_Distance

Despite the similar names, ST_Distance and ST_Distance_Sphere use different models for space. In MySQL 5.7, spatial calculations assume SRID 0, an infinite flat Cartesian plane whose axes have no assigned unit. ST_Distance therefore returns the shortest planar distance between two geometries in the same units as their coordinates.

For example, when longitude and latitude are stored as POINT(135.5, 34.7), ST_Distance treats those values simply as X and Y positions on a flat plane. Its result is expressed in coordinate units—not meters or kilometers—and it does not account for the curvature of the Earth or the decreasing ground distance represented by one degree of longitude toward the poles.

ST_Distance_Sphere, by contrast, interprets a point’s X and Y values as longitude and latitude in degrees, calculates the shortest distance over a sphere, and returns the result in meters. MySQL 5.7 uses a default sphere radius of 6,370,986 meters unless a different radius is supplied.

ST_DistanceST_Distance_Sphere
Coordinate modelFlat Cartesian planeSphere
Result unit in MySQL 5.7Coordinate units; SRID 0 has no defined unitMeters
Accounts for Earth’s curvatureNoYes, using a spherical approximation
Geometry supportSupports applicable geometry type combinationsPoint/Point and Point/MultiPoint combinations
Typical usePlanar coordinates or distances between geometry shapesApproximate Earth-surface distance from longitude and latitude

Use ST_Distance_Sphere for longitude-latitude points when an approximate physical distance on Earth is needed. Use ST_Distance when the coordinates already belong to an appropriate planar system, or when calculating the shortest Cartesian distance between geometry shapes. Neither function calculates a road, rail, or walking route.

Conclusion

These three examples show how ST_Distance_Sphere calculates great-circle distances over approximately 2 km and 400 km, as well as near 78°N, where a simple planar approximation is especially prone to error. Because the function performs the spherical calculation itself, there is no need to implement the Haversine formula in application code or add a separate geospatial library simply to determine the distance between two points.

Remember that GeomFromText('POINT(...)') expects longitude before latitude. If you are used to the more common latitude-longitude convention, it is easy to reverse the arguments without noticing.

About the author

Takahiro Iwasa

Takahiro Iwasa

Software Developer

This blog shares technical notes from hands-on projects—architecture, implementation, and AWS service integrations.