We have two tables with polygons stored in geometry
type columns.
I want to fetch the polygons present in one table that are not present in another table.
As of now I am doing a left outer join
and using STAsText()
, however that is taking a very long time.
FYI, we have approximately 120 million polygons in both tables.
Is there a fast way of comparing geometry
type data? Maybe I need to use the spatial indexing, however I am not aware of this.
SELECT newPolygon.*
FROM table1 newPolygon
LEFT JOIN table2 oldPolygon
ON newPolygon.Shape.STAsText() = oldPolygon.Shape.STAsText()
WHERE oldPolygon.Shape IS NULL
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table2` ADD INDEX `table2_idx_shape` (`Shape`);
SELECT
newPolygon.*
FROM
table1 newPolygon
LEFT JOIN
table2 oldPolygon
ON newPolygon.Shape.STAsText() = oldPolygon.Shape.STAsText()
WHERE
oldPolygon.Shape IS NULL