SQL Server:聚簇索引很慢

时间:2017-07-20 01:41:14

标签: sql-server tsql sql-server-2012

我有2个数据库表:

  1. Message包含gps消息的表
  2. MessageData包含消息传感器值
  3. 我加入它们以获取两个日期之间的传感器值。

    查询缓慢返回结果,99%的时间都在寻找聚集索引。

    如何改善这种表现?有关索引或查询方面的任何建议吗?

    我运行以下查询以进行加入:我使用主键>进行过滤3264353049,以防止与去年的行搜索日期

    SELECT 
        t.MessageId, t.DataSourceId, t.[Value],
        DataSource.SourceNameId, DataSource.Name
    FROM 
        [MessageData] t
    INNER JOIN 
        [Message] m ON t.MessageId = m.MessageId
    LEFT JOIN
        DataSource ON t.DataSourceId = DataSource.DataSourceId
    WHERE
        t.MessageId > 3264353049 
        AND m.ObjectId = @objId 
        AND m.GpsTime BETWEEN @dtFrom AND @dtTo 
        AND m.Valid = 1;
    

    Query plan time ?

    query plan screen2

    我有这些索引:

    ALTER TABLE [MessageData] 
        ADD CONSTRAINT [AnalogData_PK] 
        PRIMARY KEY CLUSTERED ([MessageId] ASC, [DataSourceId] ASC)
    
    CREATE NONCLUSTERED INDEX [MessageData_DataSourceId_IDX] 
    ON [MessageData] ([DataSourceId] ASC)
    GO
    
    CREATE NONCLUSTERED INDEX [IX_gpstime_objectid] 
    ON [Message] ([GpsTime] ASC)
    INCLUDE ([MessageId], [ObjectId]) 
    GO
    
    CREATE UNIQUE CLUSTERED INDEX [Message_Object_UK] 
    ON [Message] ([ObjectId] ASC, [GpsTime] ASC, [MessageId] ASC)
    GO
    

    消息表数据:

    MessageId            ObjectId    VectorAngle VectorSpeed Altitude    GpsTime                        X                      Y                      VisibleSatelites
    -------------------- ----------- ----------- ----------- ----------- ------------------------------ ---------------------- ---------------------- ----------------
    9988600080           192         0           0           0           2017-07-19 00:03:20            0             0             0
    9988600082           192         0           0           0           2017-07-19 00:08:20            0             0             0
    9988600086           192         0           0           0           2017-07-19 00:13:20            0             0             0
    9988600089           192         0           0           0           2017-07-19 00:18:20            0             0             0
    9988600092           192         0           0           0           2017-07-19 00:23:20            0            0             0
    

    MessageData表数据:

    MessageId            DataSourceId Value                  SourceNameId Name
    -------------------- ------------ ---------------------- ------------ ------------------------------
    9988600080           6364         0                      1            Engine
    9988600080           6365         0                      2            Digital Input Status 2
    9988600080           325346       0                      179          DOUT 1
    9988600080           325347       0                      180          DOUT 2
    9988600080           334214       0                      69           Bettary
    9988600082           6364         0                      1            Engine
    9988600082           6365         0                      2            Digital Input Status 2
    9988600082           325346       0                      179          DOUT 1
    9988600082           325347       0                      180          DOUT 2
    9988600082           334214       0                      69           Bettary
    9988600086           6364         0                      1            Engine
    9988600086           6365         0                      2            Digital Input Status 2
    9988600086           325346       0                      179          DOUT 1
    9988600086           325347       0                      180          DOUT 2
    9988600086           334214       0                      69           Bettary
    9988600089           6364         0                      1            Engine
    9988600089           6365         0                      2            Digital Input Status 2
    9988600089           325346       0                      179          DOUT 1
    9988600089           325347       0                      180          DOUT 2
    9988600089           334214       0                      69           Bettary
    9988600092           6364         0                      1            Engine
    9988600092           6365         0                      2            Digital Input Status 2
    9988600092           325346       0                      179          DOUT 1
    9988600092           325347       0                      180          DOUT 2
    9988600092           334214       0                      69           Bettary
    

1 个答案:

答案 0 :(得分:0)

正如评论中提到的,我肯定会更新我的统计数据。但在那之后我会看看这个指数:

CREATE UNIQUE CLUSTERED INDEX [Message_Object_UK] 
ON [Message] ([ObjectId] ASC, [GpsTime] ASC, [MessageId] ASC)

查询正在进行令人讨厌的嵌套循环连接,因为它必须寻找并查找具有特定messageID的记录,该记录必须从Message表索引中拉出EACH记录。尝试将其更改为:

CREATE UNIQUE CLUSTERED INDEX [Message_Object_UK] 
ON [Message] ([MessageId] ASC, [ObjectId] ASC, [GpsTime] ASC )

这有可能为您提供哈希或合并连接