【问题标题】:Spatial Index slows down query with STContains空间索引使用 STContains 减慢查询速度
【发布时间】:2012-07-07 18:04:37
【问题描述】:

我正在测试是否可以在 SQL Server 2012 上使用空间索引。

所以,我有一张桌子

CREATE TABLE [dbo].[Records]
(
 [ID] [uniqueidentifier] PRIMARY KEY NOT NULL,
 [Value] [nvarchar](256) NOT NULL,
 [Padding] [nvarchar](max) NOT NULL,
 [Bounds] [geometry] NOT NULL
)

和一个索引

CREATE SPATIAL INDEX [RecordsSpatialIndex]
ON [Records]([Bounds])
USING GEOMETRY_GRID
WITH
(
    BOUNDING_BOX = (0, 0, 2000, 2000) -- all coordinates are within this range
);

[Bounds] 列包含 5 点多边形,实际上是矩形 (x1 y1, x1 y2, x2 y2, x2 y1, x1 y1)。

@bounds 变量包含相同类型的矩形。奇怪的是下面的查询

SELECT
    [ID], [Value], [Padding]
FROM
    [Records]
WHERE
    ([Bounds].STContains(@Bounds) = 1)

在没有空间索引的情况下运行速度快三倍以上。

对于索引,65% 的时间是聚集索引搜索记录表,而 29% 的时间是过滤器。总共 65 秒。

如果没有索引,92% 的时间是过滤器,8% 的时间是聚集索引扫描记录表。总共 19 秒。

那么,我在这里做错了什么?

【问题讨论】:

  • 查看查询计划。有时查询优化器不是最优的。但它会收集统计数据并变得更智能。

标签: sql-server tsql sql-server-2012 spatial-index spatial-query


【解决方案1】:

此外,这个 MSDN article 对我了解如何创建对特定查询有用的索引很有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多