我在GeoLocation列上有一个带有空间索引的Location表。以下查询有效:
SELECT *
FROM Location WITH (INDEX (SpatialIndex_GeoLocation))
WHERE GeoLocation.STDistance(GEOGRAPHY::STGeomFromText('POINT(4.271942
52.085289)', 4326)) < 50
但是,在添加第二个谓词时,查询:
SELECT *
FROM Location WITH (INDEX (SpatialIndex_GeoLocation))
WHERE GeoLocation.STDistance(GEOGRAPHY::STGeomFromText('POINT(4.271942
52.085289)', 4326)) < 50
OR
GeoLocation.STDistance(GEOGRAPHY::STGeomFromText('POINT(5.758871 52.7845827)', 4326)) < 50
抛出以下异常:
查询处理器无法为具有
的查询生成查询计划
空间索引提示。原因:无法找到所需的比较谓词
尝试删除索引提示或删除SET FORCEPLAN。
当然,删除索引提示会使查询起作用,但不使用索引。
非常感谢任何帮助
答案 0 :(得分:0)
在这种情况下的解决方法可能是使用UNION操作,如下所示:
SELECT *
FROM Location WITH (INDEX (SpatialIndex_GeoLocation))
WHERE GeoLocation.STDistance(GEOGRAPHY::STGeomFromText('POINT(4.271942
52.085289)', 4326)) < 50
UNION
SELECT *
FROM Location WITH (INDEX (SpatialIndex_GeoLocation))
WHERE
GeoLocation.STDistance(GEOGRAPHY::STGeomFromText('POINT(5.758871 52.7845827)', 4326)) < 50