我正在开发一个监控项目,每次收集数据时,我都会为该集合生成一个新的ID。
我希望在索引视图中获得最新代码。
SELECT THE_ID = MAX(THE_ID) FROM [dbo].[monit_server_space_by_drive]
类似的东西:
CREATE VIEW V_LAST_ID
WITH SCHEMABINDING
AS
SELECT THE_ID = MAX(THE_ID) FROM [dbo].[monit_server_space_by_drive]
CREATE UNIQUE CLUSTERED INDEX UC_IDX_V_LAST_ID ON V_LAST_ID (THE_ID)
这可能还是有其他选择?
这就是我打算如何使用它:
SELECT
M.[THE_ID]
,M.[serverid]
,s.SQLServerName
,s.[Environment]
,M.[DRIVE]
,M.[Volume_Size_GB]
,[VolumeUsed_GB] = CAST (M.[VolumeUsed_MB] / 1024.00 AS NUMERIC (18,2))
,M.[VolumeSpaceAvailable_GB]
, DrivePercentUsed = 100 - M.[VolumePercentAvailable]
,M.[VolumePercentAvailable]
FROM [dbo].[monit_server_space_by_drive] M
INNER JOIN DBO.V_LAST_ID V
ON M.THE_ID = V.THE_ID
INNER JOIN [dbo].[tblServers] S
ON M.serverid = s.ServerID
order by M.[ClusterName]
表[dbo]的主键。[monit_server_space_by_drive]由三个字段组成:
USE [Monitoring]
GO
ALTER TABLE [dbo].[monit_server_space_by_drive] ADD CONSTRAINT [PK_monit_server_space_by_drive] PRIMARY KEY CLUSTERED
( [THE_ID] ASC,
[serverid] ASC,
[volumeID] ASC
)ON [FGMONITORING]
表格定义:
CREATE TABLE [dbo].[monit_server_space_by_drive](
[THE_ID] [int] NOT NULL,
[serverid] [int] NOT NULL,
[ClusterName] [nvarchar](260) NOT NULL,
[nodeid] [int] NOT NULL,
[DRIVE] [nchar](1) NULL,
[volumeID] [int] NOT NULL,
[LastSync] [datetime] NULL,
[Volume_Size_MB] [numeric](18, 2) NULL,
[Volume_Size_GB] [numeric](18, 2) NULL,
[VolumeSpaceAvailable_MB] [numeric](18, 2) NULL,
[VolumeSpaceAvailable_GB] [numeric](18, 2) NULL,
[VolumePercentAvailable] [numeric](5, 2) NULL,
[dt] [datetime] NULL,
[_year] [smallint] NULL,
[_month] [tinyint] NULL,
[_day] [tinyint] NULL,
[_week] [tinyint] NULL CONSTRAINT [DF_monit_server_space_by_drive__week] DEFAULT ((0)),
[_hour] [tinyint] NULL,
[_min] [tinyint] NULL,
[_weekday] [tinyint] NULL,
[VolumeUsed_MB] AS ([volume_size_MB]-[volumeSpaceAvailable_MB]) PERSISTED,
CONSTRAINT [PK_monit_server_space_by_drive] PRIMARY KEY CLUSTERED
(
[THE_ID] ASC,
[serverid] ASC,
[volumeID] ASC
) ON [FGMONITORING_INDEX]
) ON [FGMONITORING]
以下是此表所包含数据的示例。每次我收集连接数据时,THE_ID都会增加一个。在同一个THE_ID中,我拥有环境中的所有服务器。
答案 0 :(得分:1)
THE_ID是否定义为[dbo]。[monit_server_space_by_drive]表中的唯一聚簇索引?这个领域是一个身份吗?如果是这样,我认为你不会通过使用索引视图来实现任何收益。
比较这些可能性的表现:
Set Statistics IO ON;
Set Statistics Time ON;
Select * from V_LAST_ID;
Set Statistics IO OFF;
Set Statistics Time OFF;
OR
Set Statistics IO ON;
Set Statistics Time ON;
SELECT THE_ID = MAX(THE_ID) FROM [dbo].[monit_server_space_by_drive];
Set Statistics IO OFF;
Set Statistics Time OFF;
OR
Set Statistics IO ON;
Set Statistics Time ON;
SELECT Top 1 THE_ID FROM [dbo].[monit_server_space_by_drive] Order by THE_ID Desc
Set Statistics IO OFF;
Set Statistics Time OFF;
在ssms中单独运行每个,消息选项卡将显示cpu和持续时间。
我怀疑这两种替代解决方案的性能至少与索引视图一样好,但您需要自己测试它们。
答案 1 :(得分:1)
如果这样做,那么每次插入新记录时,都必须更新视图的索引。
为什么不直接使用触发器将最后一个ID更新到一个小表中。
CREATE TABLE DBO.V_LAST_ID( [THE_ID] [int] NOT NULL );
INSERT DBO.V_LAST_ID (THE_ID) VALUES (0); --Starting row
CREATE TRIGGER Dbo.KeepV_LAST_IDNumber ON [dbo].[monit_server_space_by_drive]
AFTER INSERT AS
BEGIN
DECLARE @MaxInserted int;
SELECT @MaxInserted = MAX(THE_ID) FROM inserted;
UPDATE DBO.V_LAST_ID
SET THE_ID = @MaxInserted
WHERE THE_ID < @MaxInserted;
END
然后,您可以按照预期使用此表而不是视图。