SDO_NN Returns Incorrect Results When Having LOCAL Partitioned Spatial Index (Doc ID 1362295.1)

Last updated on FEBRUARY 08, 2017

Applies to:

Oracle Spatial - Version: 11.2.0.2 and later   [Release: 11.2 and later ]
Information in this document applies to any platform.

Symptoms

You have a query to find 5 nearest neighbors and you have a GLOBAL spatial index on a partitioned table

You can use this query:

SELECT r.name, r.location, SDO_NN_DISTANCE(1) distance_in_miles
FROM restaurants_part_table r
WHERE SDO_NN(r.location,
MDSYS.SDO_GEOMETRY(2001,8307,MDSYS.SDO_POINT_TYPE(-110,35,Null),Null,Null),
'SDO_NUM_RES=5 distance=2 unit=MILE', 1) = 'TRUE'
ORDER BY distance_in_miles;

You change the spatial index from GLOBAL to LOCAL

The documentation specifies that you will get 5 results for each partition and you have to use ROWNUM
You rewrite your query like this:

SELECT r.name, r.location, SDO_NN_DISTANCE(1) distance_in_miles
FROM restaurants_part_table r
WHERE SDO_NN(r.location,
MDSYS.SDO_GEOMETRY(2001,8307,MDSYS.SDO_POINT_TYPE(-110,35,Null),Null,Null),
'SDO_NUM_RES=5 distance=2 unit=MILE', 1) = 'TRUE'
WHERE ROWNUM<=5
ORDER BY distance_in_miles;


This query will give you incorrect results because it is not ordering the results before getting the 5 top results.

Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms