我正在尝试学习如何在SQL Server中使用geometry
数据类型,并通过一个简单的示例进行模拟。
现在,我面临的问题是没有使用我在测试表上创建的空间索引的查询优化器。据documentation所见,我正在执行的联接类型应与空间索引兼容。
我在这里想念什么?
自包含工作示例的代码(使用CTRL + M启用实际的执行计划):
CREATE TABLE dbo.GeometryTest
(
id int PRIMARY KEY CLUSTERED NOT NULL,
temppos geometry NOT NULL
)
CREATE SPATIAL INDEX IX_temppos ON dbo.GeometryTest (temppos)
USING GEOMETRY_AUTO_GRID WITH (
BOUNDING_BOX = (
0, 0, 1000, 1000
)
)
;
-- This CTE is just used to produce multiple rows without a loop
WITH
L0 AS (SELECT c FROM (SELECT 1 UNION ALL SELECT 1) AS D(c)),
L1 AS (SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B),
L2 AS (SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B),
L3 AS (SELECT 1 AS c FROM L2 AS A CROSS JOIN L2 AS B),
L4 AS (SELECT 1 AS c FROM L3 AS A CROSS JOIN L3 AS B),
L5 AS (SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B),
Nums AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS n
FROM L5)
INSERT dbo.GeometryTest SELECT TOP 100 n, geometry::STPolyFromText(CONCAT('POLYGON ((',n*10,' ',n*10,',',n*10+15,' ',n*10,',',n*10+15,' ',n*10+15,',',n*10,' ',n*10+15,',',n*10,' ',n*10,'))'), 0) FROM Nums
SELECT * FROM dbo.GeometryTest a INNER JOIN dbo.GeometryTest b ON b.temppos.STOverlaps(a.temppos) = 1
IF EXISTS (SELECT 1 WHERE OBJECT_ID('dbo.GeometryTest') IS NOT NULL) DROP TABLE dbo.GeometryTest