我想使用空间索引提示查询最近的记录到更多不同的点。如果我在它们之间加上“和”(就像第一个块一样),那就没问题了。但如果我使用“Or”(如第二个块),我会收到错误:
查询处理器无法为具有空间索引提示的查询生成查询计划。原因:无法找到所需的比较谓词。尝试删除索引提示或删除SET FORCEPLAN。
有什么问题?有没有替代解决方案?
第一块:
SELECT
[Id],[Geocode]
FROM
[dbo].[Companies] with (INDEX(SpatialIndex_GeoCode))
WHERE
([Geocode].STDistance(geography::Point(42.7085, -74.9456, 4326)) < 8046.72)
and ([Geocode].STDistance(geography::Point(42.7085, -74.9456, 4326)) < 18046.72)
第二块:
SELECT
[Id],[Geocode]
FROM
[dbo].[Companies] with (INDEX(SpatialIndex_GeoCode))
WHERE
([Geocode].STDistance(geography::Point(42.7085, -74.9456, 4326)) < 8046.72)
OR ([Geocode].STDistance(geography::Point(42.7085, -74.9456, 4326)) < 18046.72)
答案 0 :(得分:2)
您始终可以分隔or
生成的两个数据集,然后将它们重新组合在一起。使用union
代替union all
应避免重复:
SELECT
[Id],[Geocode]
FROM
[dbo].[Companies] with (INDEX(SpatialIndex_GeoCode))
WHERE
([Geocode].STDistance(geography::Point(42.7085, -74.9456, 4326)) < 8046.72)
union
SELECT
[Id],[Geocode]
FROM
[dbo].[Companies] with (INDEX(SpatialIndex_GeoCode))
WHERE
([Geocode].STDistance(geography::Point(42.7085, -74.9456, 4326)) < 18046.72)