一个用来自动管理大容量表的Sql脚本
它的功能是自动检查表的容量,超过一定的限制把数据导出到历史表中
使得用户都能最快速的访问这个大容量的表
脚本片段,下面有下载地址:
1use BaiheCRMLogging
2go
3
4--创建计算 @formalRange 的函数
5CREATE FUNCTION GetFormalRange()
6RETURNS nvarchar(10)
7WITH EXECUTE AS CALLER
8AS
9BEGIN
10 declare @formalRange nvarchar(10)
11 declare @now datetime
12 set @now = getdate()
13 set @formalRange = Convert(nvarchar(10),Convert(nvarchar(4),Year(@now))+'-'+Convert(nvarchar(2),Month(@now)-1)+'-01')
14 RETURN(@formalRange)
15END;
16GO
17
18--创建计算 @nextRange 的函数
19CREATE FUNCTION GetNextRange()
20RETURNS nvarchar(10)
21WITH EXECUTE AS CALLER
22AS
23BEGIN
24 declare @nextRange nvarchar(10)
25 declare @now datetime
26 set @now = getdate()
27 set @nextRange = Convert(nvarchar(10),Convert(nvarchar(4),Year(@now))+'-'+Convert(nvarchar(2),Month(@now)+1)+'-01')
28 RETURN(@nextRange)
29END;
30GO
31
32--创建计算@currentRange的函数
33CREATE FUNCTION GetCurrentRange()
34RETURNS nvarchar(10)
35WITH EXECUTE AS CALLER
36AS
37BEGIN
38 declare @currentRange nvarchar(10)
39 declare @now datetime
40 set @now = getdate()
41 set @currentRange = Convert(nvarchar(10),Convert(nvarchar(4),Year(@now))+'-'+Convert(nvarchar(2),Month(@now))+'-01')
42 RETURN(@currentRange)
43END;
44GO
45
46--日志分区函数
47create partition function LogHistoryPF1(datetime)
48as range left for values(dbo.GetCurrentRange());
49go
50
51--日志分区架构
52CREATE PARTITION SCHEME [LogHistoryPS1]
53AS PARTITION [LogHistoryPF1]
54TO ([PRIMARY],[PRIMARY]);
55go
56
57--日志分区表
58create table LogHistory
59(
60 LogId bigint not null Identity(1,1),
61 Operator nvarchar(36) not null,
62 OperateType tinyint not null,
63 OperateTime datetime not null,
64 EntityName nvarchar(30) null,
65 EntityGuid uniqueidentifier null,
66 OriginalData nvarchar(max) null,
67 ModifiedData nvarchar(max) null
68) on LogHistoryPS1(OperateTime);
69
70go
71
72--归档分区函数
73create partition function LogArchivePF1(datetime)
74as range left for values(dbo.GetCurrentRange());
75go
76
77--归档分区架构
78CREATE PARTITION SCHEME [LogArchivePS1]
79AS PARTITION [LogArchivePF1]
80TO ([PRIMARY],[PRIMARY]);
81GO
82
83--归档分区表
84create table LogArchive
85(
86 LogId bigint not null Identity(1,1),
87 Operator nvarchar(36) not null,
88 OperateType tinyint not null,
89 OperateTime datetime not null,
90 EntityName nvarchar(30) null,
91 EntityGuid uniqueidentifier null,
92 OriginalData nvarchar(max) null,
93 ModifiedData nvarchar(max) null
94) on LogArchivePS1(OperateTime);
95
96go
97
98--增加主键约束
99
100ALTER TABLE [LogHistory] WITH CHECK ADD
101 CONSTRAINT [PK_LogHistory_LogID] PRIMARY KEY CLUSTERED
102 (
103 [OperateTime],
104 [LogID]
105 ) ON [LogHistoryPS1] (OperateTime);
106GO
107
108ALTER TABLE [LogArchive] WITH CHECK ADD
109 CONSTRAINT [PK_LogArchive_LogID] PRIMARY KEY CLUSTERED
110 (
111 [OperateTime],
112 [LogID]
113 ) ON [LogArchivePS1] (OperateTime);
114GO
115
116--创建索引
117CREATE INDEX [IX_LogHistory_Operator] ON [LogHistory]([Operator]) ON [LogHistoryPS1] (OperateTime);
118GO
119CREATE INDEX [IX_LogArchive_Operator] ON [LogArchive]([Operator]) ON [LogArchivePS1] (OperateTime);
120GO
121
122--添加日志
123Create proc usp_LogHistory_Create
124(
125@Operator nvarchar(36),
126@OperateType tinyint,
127@OperateTime datetime,
128@EntityName nvarchar(30),
129@EntityGuid uniqueidentifier,
130@OriginalDataString nvarchar(max),
131@ModifiedDataString nvarchar(max)
132)
133as
134insert into LogHistory(Operator,OperateType,OperateTime,EntityName,EntityGuid,OriginalData,ModifiedData)
135values(@Operator,@OperateType,@OperateTime,@EntityName,@EntityGuid,@OriginalDataString,@ModifiedDataString)
136go
137
138--select * from LogHistory
139
140--truncate table LogHistory
2go
3
4--创建计算 @formalRange 的函数
5CREATE FUNCTION GetFormalRange()
6RETURNS nvarchar(10)
7WITH EXECUTE AS CALLER
8AS
9BEGIN
10 declare @formalRange nvarchar(10)
11 declare @now datetime
12 set @now = getdate()
13 set @formalRange = Convert(nvarchar(10),Convert(nvarchar(4),Year(@now))+'-'+Convert(nvarchar(2),Month(@now)-1)+'-01')
14 RETURN(@formalRange)
15END;
16GO
17
18--创建计算 @nextRange 的函数
19CREATE FUNCTION GetNextRange()
20RETURNS nvarchar(10)
21WITH EXECUTE AS CALLER
22AS
23BEGIN
24 declare @nextRange nvarchar(10)
25 declare @now datetime
26 set @now = getdate()
27 set @nextRange = Convert(nvarchar(10),Convert(nvarchar(4),Year(@now))+'-'+Convert(nvarchar(2),Month(@now)+1)+'-01')
28 RETURN(@nextRange)
29END;
30GO
31
32--创建计算@currentRange的函数
33CREATE FUNCTION GetCurrentRange()
34RETURNS nvarchar(10)
35WITH EXECUTE AS CALLER
36AS
37BEGIN
38 declare @currentRange nvarchar(10)
39 declare @now datetime
40 set @now = getdate()
41 set @currentRange = Convert(nvarchar(10),Convert(nvarchar(4),Year(@now))+'-'+Convert(nvarchar(2),Month(@now))+'-01')
42 RETURN(@currentRange)
43END;
44GO
45
46--日志分区函数
47create partition function LogHistoryPF1(datetime)
48as range left for values(dbo.GetCurrentRange());
49go
50
51--日志分区架构
52CREATE PARTITION SCHEME [LogHistoryPS1]
53AS PARTITION [LogHistoryPF1]
54TO ([PRIMARY],[PRIMARY]);
55go
56
57--日志分区表
58create table LogHistory
59(
60 LogId bigint not null Identity(1,1),
61 Operator nvarchar(36) not null,
62 OperateType tinyint not null,
63 OperateTime datetime not null,
64 EntityName nvarchar(30) null,
65 EntityGuid uniqueidentifier null,
66 OriginalData nvarchar(max) null,
67 ModifiedData nvarchar(max) null
68) on LogHistoryPS1(OperateTime);
69
70go
71
72--归档分区函数
73create partition function LogArchivePF1(datetime)
74as range left for values(dbo.GetCurrentRange());
75go
76
77--归档分区架构
78CREATE PARTITION SCHEME [LogArchivePS1]
79AS PARTITION [LogArchivePF1]
80TO ([PRIMARY],[PRIMARY]);
81GO
82
83--归档分区表
84create table LogArchive
85(
86 LogId bigint not null Identity(1,1),
87 Operator nvarchar(36) not null,
88 OperateType tinyint not null,
89 OperateTime datetime not null,
90 EntityName nvarchar(30) null,
91 EntityGuid uniqueidentifier null,
92 OriginalData nvarchar(max) null,
93 ModifiedData nvarchar(max) null
94) on LogArchivePS1(OperateTime);
95
96go
97
98--增加主键约束
99
100ALTER TABLE [LogHistory] WITH CHECK ADD
101 CONSTRAINT [PK_LogHistory_LogID] PRIMARY KEY CLUSTERED
102 (
103 [OperateTime],
104 [LogID]
105 ) ON [LogHistoryPS1] (OperateTime);
106GO
107
108ALTER TABLE [LogArchive] WITH CHECK ADD
109 CONSTRAINT [PK_LogArchive_LogID] PRIMARY KEY CLUSTERED
110 (
111 [OperateTime],
112 [LogID]
113 ) ON [LogArchivePS1] (OperateTime);
114GO
115
116--创建索引
117CREATE INDEX [IX_LogHistory_Operator] ON [LogHistory]([Operator]) ON [LogHistoryPS1] (OperateTime);
118GO
119CREATE INDEX [IX_LogArchive_Operator] ON [LogArchive]([Operator]) ON [LogArchivePS1] (OperateTime);
120GO
121
122--添加日志
123Create proc usp_LogHistory_Create
124(
125@Operator nvarchar(36),
126@OperateType tinyint,
127@OperateTime datetime,
128@EntityName nvarchar(30),
129@EntityGuid uniqueidentifier,
130@OriginalDataString nvarchar(max),
131@ModifiedDataString nvarchar(max)
132)
133as
134insert into LogHistory(Operator,OperateType,OperateTime,EntityName,EntityGuid,OriginalData,ModifiedData)
135values(@Operator,@OperateType,@OperateTime,@EntityName,@EntityGuid,@OriginalDataString,@ModifiedDataString)
136go
137
138--select * from LogHistory
139
140--truncate table LogHistory
下载地址:https://files.cnblogs.com/goody9807/Sql.rar