I'm trying to make a spatial query on two tables with around 10m rows each.
What I'm trying to do is to relate table1 to table2 based on the levenshtein distance of t1 and t2 in a radius of 50m around t1. If no record is in the radius, we chose the nearest neighbour.
My approach was to create two views: one for the levenshtein distance, and one for the nearest neighbour.
-- VIEW1
SELECT ID,a,b
FROM TABLE(SDO_JOIN(t1, 'SHAPE', t2, 'SHAPE', 'DISTANCE=50 UNIT=M')) geom_join
LEFT JOIN t1 ON geom_join."ROWID1" = t1."ROWID"
LEFT JOIN t2 ON geom_join."ROWID2" = t2."ROWID"
WHERE SDO_GEOM.VALIDATE_GEOMETRY(t2."SHAPE", 0.005) = 'TRUE'
AND SDO_GEOM.VALIDATE_GEOMETRY(t1."SHAPE", 0.005) = 'TRUE'
and taking the MIN of levenshtein distance.
-- VIEW2
SELECT ID,a,b
FROM t1, t2
WHERE SDO_NN(t2, t1, 'SDO_NUM_RES=1, UNIT=M') = 'TRUE'
AND SDO_GEOM.VALIDATE_GEOMETRY(t1."SHAPE", 0.005) = 'TRUE'
AND SDO_GEOM.VALIDATE_GEOMETRY(t2."SHAPE", 0.005) = 'TRUE'
And then create the table from these views as
SELECT ID,
(CASE WHEN VIEW1.a IS NULL THEN VIEW2.a ELSE VIEW1.a END) a,
(CASE WHEN VIEW1.b IS NULL THEN VIEW2.b ELSE VIEW1.b END) b
FROM VIEW2
LEFT JOIN VIEW1 USING(ID)
This seems to work for small datasets, however fails for the real tables and raise the following error:
cx_Oracle.DatabaseError: ORA-29902: error in executing ODCIIndexStart() routine
ORA-04036: PGA memory used by the instance exceeds PGA_AGGREGATE_LIMIT
ORA-06512: at "MDSYS.SDO_INDEX_METHOD_10I", line 588
I could potentially raise the limit (currently 4Gb), however I'd like to check first if there was a solution without doing that.