每天在 SQL Server 2012 Express 上将脚本归档到 运行,以保持良好的性能
Archiving scripts to run daily on SQL Server 2012 Express in order to maintain good performance
我有一种情况,我从硬件设备接收到多行事务数据。我分析这些数据,把前一天的数据做成报告。结果,我的应用程序经常梳理整个 table 来生成报告,但它只真正需要处理前 72 小时的数据。我每天 运行 一个索引脚本(重建和重新组织),并且 运行 在同一个数据库中的 tables 上执行此操作会导致数据库在我的报告应用程序中挂起很多时间。因此,我的想法是将归档数据放入一个新的数据库中,该数据库可以单独管理到该数据库,从而减少由于重新组织和重新索引数百万个数据库的多个索引而导致的该数据库的挂起时间行数。
我目前有一个脚本(如下),可将数据移动到超过 7 天的单独 table。我计划将其更改为 3 天前,但出于性能原因,我想将其放入 单独的数据库 中。该脚本仅处理 1000000 组(刚好超过每天进入的行数)
如您所见,它绑定了一个数据库。如何使下面的脚本使用多个数据库?我已经四处乱砍,但没有运气。第一个脚本是原始的,第二个是我尝试更改和使用第二个数据库。名称是 activetrackdb(原始)和 activetrackarchivedb(新存档数据库)。存档数据库具有相同的列,并且有自己的主键。
USE activetrackdb;
DECLARE @3daysago datetime
SELECT @3daysago = DATEADD(d, -7, GetDate());
SET IDENTITY_INSERT [dbo].[Archived Data Import] ON;
WITH CTE as (
SELECT TOP 1000000 *
FROM [dbo].[Data Import]
WHERE [Data Import].[Receive Date] < @7daysago)
DELETE CTE
OUTPUT DELETED.id,
DELETED.[Company id],
DELETED.[Site id],
DELETED.[Site name],
DELETED.[Receive date],
DELETED.[Detect date],
DELETED.[Type],
DELETED.[eventType],
DELETED.[Tag name],
DELETED.[Tag RFID],
DELETED.[Route id],
DELETED.[Route name],
DELETED.[Patrol id],
DELETED.[Device s/n],
DELETED.[Guard id],
DELETED.[Guard name],
DELETED.[Employee id],
DELETED.[Employee name],
DELETED.[Alarm group id],
DELETED.[Comment],
DELETED.[GPS latitude],
DELETED.[GPS longitude],
DELETED.[GPS accuracy],
DELETED.[GPS vertical accuracy],
DELETED.[GPS height],
DELETED.[GPS speed],
DELETED.[GPS heading],
DELETED.[Additional data],
DELETED.[Device id],
DELETED.[Device name],
DELETED.[Company name],
DELETED.[Occurrence date],
DELETED.[GPS date]
INTO [dbo].[Archived Data Import]
(id,
[Company id],
[Site id],
[Site name],
[Receive date],
[Detect date],
[Type],
[eventType],
[Tag name],
[Tag RFID],
[Route id],
[Route name],
[Patrol id],
[Device s/n],
[Guard id],
[Guard name],
[Employee id],
[Employee name],
[Alarm group id],
[Comment],
[GPS latitude],
[GPS longitude],
[GPS accuracy],
[GPS vertical accuracy],
[GPS height],
[GPS speed],
[GPS heading],
[Additional data],
[Device id],
[Device name],
[Company name],
[Occurrence date],
[GPS date]);
我的尝试:
DECLARE @7daysago datetime
SELECT @7daysago = DATEADD(d, -7, GetDate());
SET IDENTITY_INSERT [activetrackarchivedb].[Data Import] ON;
WITH CTE as (
SELECT TOP 1000000 *
FROM [activetrackdb].[Data Import]
WHERE [Data Import].[Receive Date] < @7daysago)
DELETE CTE
OUTPUT DELETED.id,
DELETED.[Company id],
DELETED.[Site id],
DELETED.[Site name],
DELETED.[Receive date],
DELETED.[Detect date],
DELETED.[Type],
DELETED.[eventType],
DELETED.[Tag name],
DELETED.[Tag RFID],
DELETED.[Route id],
DELETED.[Route name],
DELETED.[Patrol id],
DELETED.[Device s/n],
DELETED.[Guard id],
DELETED.[Guard name],
DELETED.[Employee id],
DELETED.[Employee name],
DELETED.[Alarm group id],
DELETED.[Comment],
DELETED.[GPS latitude],
DELETED.[GPS longitude],
DELETED.[GPS accuracy],
DELETED.[GPS vertical accuracy],
DELETED.[GPS height],
DELETED.[GPS speed],
DELETED.[GPS heading],
DELETED.[Additional data],
DELETED.[Device id],
DELETED.[Device name],
DELETED.[Company name],
DELETED.[Occurrence date],
DELETED.[GPS date]
INTO [activetrackarchivedb].[Data Import]
(id,
[Company id],
[Site id],
[Site name],
[Receive date],
[Detect date],
[Type],
[eventType],
[Tag name],
[Tag RFID],
[Route id],
[Route name],
[Patrol id],
[Device s/n],
[Guard id],
[Guard name],
[Employee id],
[Employee name],
[Alarm group id],
[Comment],
[GPS latitude],
[GPS longitude],
[GPS accuracy],
[GPS vertical accuracy],
[GPS height],
[GPS speed],
[GPS heading],
[Additional data],
[Device id],
[Device name],
[Company name],
[Occurrence date],
[GPS date]);
如有任何帮助,我们将不胜感激。我假设分区通常会解决这个问题,但出于 $$$ 的原因,我在 SQL Server Express 上。
使用三部分对象名称 (database.owner.table
):
DECLARE @7daysago datetime
SELECT @7daysago = DATEADD(d, -7, GetDate());
SET IDENTITY_INSERT [activetrackarchivedb].dbo.[Data Import] ON;
WITH CTE as (
SELECT TOP 1000000 *
FROM [activetrackdb].dbo.[Data Import]
WHERE [activetrackdb].dbo.[Data Import].[Receive Date] < @7daysago
)
DELETE CTE
OUTPUT DELETED.id,
DELETED.[Company id],
DELETED.[Receive date],
....
INTO [activetrackarchivedb].dbo.[Data Import]
(id,
[Company id],
[Receive date],
....
);
我有一种情况,我从硬件设备接收到多行事务数据。我分析这些数据,把前一天的数据做成报告。结果,我的应用程序经常梳理整个 table 来生成报告,但它只真正需要处理前 72 小时的数据。我每天 运行 一个索引脚本(重建和重新组织),并且 运行 在同一个数据库中的 tables 上执行此操作会导致数据库在我的报告应用程序中挂起很多时间。因此,我的想法是将归档数据放入一个新的数据库中,该数据库可以单独管理到该数据库,从而减少由于重新组织和重新索引数百万个数据库的多个索引而导致的该数据库的挂起时间行数。
我目前有一个脚本(如下),可将数据移动到超过 7 天的单独 table。我计划将其更改为 3 天前,但出于性能原因,我想将其放入 单独的数据库 中。该脚本仅处理 1000000 组(刚好超过每天进入的行数)
如您所见,它绑定了一个数据库。如何使下面的脚本使用多个数据库?我已经四处乱砍,但没有运气。第一个脚本是原始的,第二个是我尝试更改和使用第二个数据库。名称是 activetrackdb(原始)和 activetrackarchivedb(新存档数据库)。存档数据库具有相同的列,并且有自己的主键。
USE activetrackdb;
DECLARE @3daysago datetime
SELECT @3daysago = DATEADD(d, -7, GetDate());
SET IDENTITY_INSERT [dbo].[Archived Data Import] ON;
WITH CTE as (
SELECT TOP 1000000 *
FROM [dbo].[Data Import]
WHERE [Data Import].[Receive Date] < @7daysago)
DELETE CTE
OUTPUT DELETED.id,
DELETED.[Company id],
DELETED.[Site id],
DELETED.[Site name],
DELETED.[Receive date],
DELETED.[Detect date],
DELETED.[Type],
DELETED.[eventType],
DELETED.[Tag name],
DELETED.[Tag RFID],
DELETED.[Route id],
DELETED.[Route name],
DELETED.[Patrol id],
DELETED.[Device s/n],
DELETED.[Guard id],
DELETED.[Guard name],
DELETED.[Employee id],
DELETED.[Employee name],
DELETED.[Alarm group id],
DELETED.[Comment],
DELETED.[GPS latitude],
DELETED.[GPS longitude],
DELETED.[GPS accuracy],
DELETED.[GPS vertical accuracy],
DELETED.[GPS height],
DELETED.[GPS speed],
DELETED.[GPS heading],
DELETED.[Additional data],
DELETED.[Device id],
DELETED.[Device name],
DELETED.[Company name],
DELETED.[Occurrence date],
DELETED.[GPS date]
INTO [dbo].[Archived Data Import]
(id,
[Company id],
[Site id],
[Site name],
[Receive date],
[Detect date],
[Type],
[eventType],
[Tag name],
[Tag RFID],
[Route id],
[Route name],
[Patrol id],
[Device s/n],
[Guard id],
[Guard name],
[Employee id],
[Employee name],
[Alarm group id],
[Comment],
[GPS latitude],
[GPS longitude],
[GPS accuracy],
[GPS vertical accuracy],
[GPS height],
[GPS speed],
[GPS heading],
[Additional data],
[Device id],
[Device name],
[Company name],
[Occurrence date],
[GPS date]);
我的尝试:
DECLARE @7daysago datetime
SELECT @7daysago = DATEADD(d, -7, GetDate());
SET IDENTITY_INSERT [activetrackarchivedb].[Data Import] ON;
WITH CTE as (
SELECT TOP 1000000 *
FROM [activetrackdb].[Data Import]
WHERE [Data Import].[Receive Date] < @7daysago)
DELETE CTE
OUTPUT DELETED.id,
DELETED.[Company id],
DELETED.[Site id],
DELETED.[Site name],
DELETED.[Receive date],
DELETED.[Detect date],
DELETED.[Type],
DELETED.[eventType],
DELETED.[Tag name],
DELETED.[Tag RFID],
DELETED.[Route id],
DELETED.[Route name],
DELETED.[Patrol id],
DELETED.[Device s/n],
DELETED.[Guard id],
DELETED.[Guard name],
DELETED.[Employee id],
DELETED.[Employee name],
DELETED.[Alarm group id],
DELETED.[Comment],
DELETED.[GPS latitude],
DELETED.[GPS longitude],
DELETED.[GPS accuracy],
DELETED.[GPS vertical accuracy],
DELETED.[GPS height],
DELETED.[GPS speed],
DELETED.[GPS heading],
DELETED.[Additional data],
DELETED.[Device id],
DELETED.[Device name],
DELETED.[Company name],
DELETED.[Occurrence date],
DELETED.[GPS date]
INTO [activetrackarchivedb].[Data Import]
(id,
[Company id],
[Site id],
[Site name],
[Receive date],
[Detect date],
[Type],
[eventType],
[Tag name],
[Tag RFID],
[Route id],
[Route name],
[Patrol id],
[Device s/n],
[Guard id],
[Guard name],
[Employee id],
[Employee name],
[Alarm group id],
[Comment],
[GPS latitude],
[GPS longitude],
[GPS accuracy],
[GPS vertical accuracy],
[GPS height],
[GPS speed],
[GPS heading],
[Additional data],
[Device id],
[Device name],
[Company name],
[Occurrence date],
[GPS date]);
如有任何帮助,我们将不胜感激。我假设分区通常会解决这个问题,但出于 $$$ 的原因,我在 SQL Server Express 上。
使用三部分对象名称 (database.owner.table
):
DECLARE @7daysago datetime
SELECT @7daysago = DATEADD(d, -7, GetDate());
SET IDENTITY_INSERT [activetrackarchivedb].dbo.[Data Import] ON;
WITH CTE as (
SELECT TOP 1000000 *
FROM [activetrackdb].dbo.[Data Import]
WHERE [activetrackdb].dbo.[Data Import].[Receive Date] < @7daysago
)
DELETE CTE
OUTPUT DELETED.id,
DELETED.[Company id],
DELETED.[Receive date],
....
INTO [activetrackarchivedb].dbo.[Data Import]
(id,
[Company id],
[Receive date],
....
);