<->
Prev | 8.10.2. Distance Operators | Next

## Name

<-> — Returns the 2D distance between A and B.

## Synopsis

`double precision <->(` geometry A , geometry B `)`;

`double precision <->(` geography A , geography B `)`;

## Description

The `<->` operator returns the 2D distance between two geometries. Used in the "ORDER BY" clause provides index-assisted nearest-neighbor result sets. For PostgreSQL below 9.5 only gives centroid distance of bounding boxes and for PostgreSQL 9.5+, does true KNN distance search giving true distance between geometries, and distance sphere for geographies.

This operand will make use of 2D GiST indexes that may be available on the geometries. It is different from other operators that use spatial indexes in that the spatial index is only used when the operator is in the ORDER BY clause.

Index only kicks in if one of the geometries is a constant (not in a subquery/cte). e.g. 'SRID=3005;POINT(1011102 450541)'::geometry instead of a.geom

Refer to PostGIS workshop: Nearest-Neighbor Searching for a detailed example.

Enhanced: 2.2.0 -- True KNN ("K nearest neighbor") behavior for geometry and geography for PostgreSQL 9.5+. Note for geography KNN is based on sphere rather than spheroid. For PostgreSQL 9.4 and below, geography support is new but only supports centroid box.

Changed: 2.2.0 -- For PostgreSQL 9.5 users, old Hybrid syntax may be slower, so you'll want to get rid of that hack if you are running your code only on PostGIS 2.2+ 9.5+. See examples below.

Availability: 2.0.0 -- Weak KNN provides nearest neighbors based on geometry centroid distances instead of true distances. Exact results for points, inexact for all other types. Available for PostgreSQL 9.1+

## Examples

    SELECT ST_Distance(geom, 'SRID=3005;POINT(1011102 450541)'::geometry) as d,edabbr, vaabbr
    FROM va2005
    ORDER BY d limit 10;

            d         | edabbr | vaabbr
    ------------------+--------+--------
                    0 | ALQ    | 128
     5541.57712511724 | ALQ    | 129A
     5579.67450712005 | ALQ    | 001
      6083.4207708641 | ALQ    | 131
      7691.2205404848 | ALQ    | 003
     7900.75451037313 | ALQ    | 122
     8694.20710669982 | ALQ    | 129B
     9564.24289057111 | ALQ    | 130
      12089.665931705 | ALQ    | 127
     18472.5531479404 | ALQ    | 002
    (10 rows)

Then the KNN raw answer:

    SELECT st_distance(geom, 'SRID=3005;POINT(1011102 450541)'::geometry) as d,edabbr, vaabbr
    FROM va2005
    ORDER BY geom <-> 'SRID=3005;POINT(1011102 450541)'::geometry limit 10;

            d         | edabbr | vaabbr
    ------------------+--------+--------
                    0 | ALQ    | 128
     5541.57712511724 | ALQ    | 129A
     5579.67450712005 | ALQ    | 001
      6083.4207708641 | ALQ    | 131
      7691.2205404848 | ALQ    | 003
     7900.75451037313 | ALQ    | 122
     8694.20710669982 | ALQ    | 129B
     9564.24289057111 | ALQ    | 130
      12089.665931705 | ALQ    | 127
     18472.5531479404 | ALQ    | 002
    (10 rows)

If you run "EXPLAIN ANALYZE" on the two queries you would see a performance improvement for the second.

For users running with PostgreSQL < 9.5, use a hybrid query to find the true nearest neighbors. First a CTE query using the index-assisted KNN, then an exact query to get correct ordering:

    WITH index_query AS (
      SELECT ST_Distance(geom, 'SRID=3005;POINT(1011102 450541)'::geometry) as d,edabbr, vaabbr
    	FROM va2005
      ORDER BY geom <-> 'SRID=3005;POINT(1011102 450541)'::geometry LIMIT 100)
      SELECT *
    	FROM index_query
      ORDER BY d limit 10;

            d         | edabbr | vaabbr
    ------------------+--------+--------
                    0 | ALQ    | 128
     5541.57712511724 | ALQ    | 129A
     5579.67450712005 | ALQ    | 001
      6083.4207708641 | ALQ    | 131
      7691.2205404848 | ALQ    | 003
     7900.75451037313 | ALQ    | 122
     8694.20710669982 | ALQ    | 129B
     9564.24289057111 | ALQ    | 130
      12089.665931705 | ALQ    | 127
     18472.5531479404 | ALQ    | 002
    (10 rows)

## See Also

ST_DWithin, ST_Distance, <#>

Prev | Up | Next
~= | Home | |=|