SQL Server从大表中选择慢

时间:2017-04-06 15:48:26

标签: sql sql-server sql-server-2014

我有2个用于物联网项目的真正大的SQL服务器数据库表

First TABLE IS Message(行数7,423,889,085行)

app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"), branch =>
{
    branch.UseIdentity();
});

第二个表是MessageSensors,行数(26,359,568,037行),此表具有消息表中每个传感器的值

CREATE TABLE [aymax].[Message](
    [MessageId] [bigint] IDENTITY(1,1) NOT NULL,
    [ObjectId] [int] NOT NULL,
    [TimeStamp] [datetime] NOT NULL CONSTRAINT [DF__Message__TimeSta__3B75D760]  DEFAULT (getdate()),
    [GpsTime] [datetime] NOT NULL,
    [VisibleSatelites] [int] NOT NULL,
    [X] [float] NOT NULL,
    [Y] [float] NOT NULL,
 CONSTRAINT [Message_PK] PRIMARY KEY NONCLUSTERED 
(
    [MessageId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

我的问题是在2个日期时间之间的时间间隔寻求真的很慢,如果我选择了消息传感器数据也会变得更慢,同时当我使用来自brentozar.com的sp_BlitzIndex检查它说我有

  

" Indexaphobia:高价值缺失指数"

     
      
  1. [aymax]。[MessageSensors](EQUALITY:[DataSourceId],[Value]包括:[MessageId])
  2.   
  3. [aymax]。[MessageSensors] EQUALITY:[Value]包括:[MessageId],[DataSourceId]
  4.   

我相信创建这个2索引会增加存储空间,也会花费太多时间来创建,我需要你对这两个关于索引的表的建议

我当前的索引

1 -

CREATE TABLE [aymax].[MessageSensors](
    [MessageId] [bigint] NOT NULL,
    [DataSourceId] [int] NOT NULL,
    [Value] [float] NOT NULL CONSTRAINT [DF__AnalogDat__Value__5812160E]  DEFAULT ((0)),
 CONSTRAINT [AnalogData_PK] PRIMARY KEY CLUSTERED 
(
    [MessageId] ASC,
    [DataSourceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

2 -

    CREATE NONCLUSTERED INDEX [IX_gpstime_objectid] ON [aymax].[Message]
(
    [GpsTime] ASC
)
INCLUDE (   [MessageId],
    [ObjectId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

第三 -

alter TABLE [aymax].[Message] ADD  CONSTRAINT [Message_PK] PRIMARY KEY NONCLUSTERED 
    (
        [MessageId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
    GO

4 -

ALTER TABLE [aymax].[MessageSensors] ADD  CONSTRAINT [AnalogData_PK] PRIMARY KEY CLUSTERED 
(
    [MessageId] ASC,
    [DataSourceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

任何帮助请,我需要从消息和消息传感器快速检索

更新 在进行一些调查时我发现选择浮动值会使结果放慢太多,从1秒到3分钟

CREATE NONCLUSTERED INDEX [MessageData_DataSourceId_IDX] ON [aymax].[MessageSensors]
(
    [DataSourceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

1 个答案:

答案 0 :(得分:1)

可能的解决方案:

  1. 过滤索引(按日期过滤,不要索引旧数据) https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes
  2. GpsTime上的聚簇索引,MessageId(特别是如果您没有关于其他索引的计划)。需要重建你的桌子。
  3. 分区(参见@ Siyaul的评论)