1 | USE [HeuristicLab.Hive-3.4]
|
---|
2 | /* this script is supposed to be executed after the plain DB is generated by the linq-to-sql schema */
|
---|
3 | /* adds default values */
|
---|
4 | /* creates delete and update cascades */
|
---|
5 | /* creates indices */
|
---|
6 | /* creates views */
|
---|
7 | /* creates triggers */
|
---|
8 |
|
---|
9 |
|
---|
10 | ALTER TABLE dbo.AssignedResources ALTER COLUMN AssignedRessourcesId ADD ROWGUIDCOL;
|
---|
11 | ALTER TABLE dbo.AssignedResources WITH NOCHECK ADD CONSTRAINT [DF_AssignedResources_AssignedRessourcesId] DEFAULT (NEWSEQUENTIALID()) FOR AssignedRessourcesId;
|
---|
12 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Job_AssignedResource]
|
---|
13 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Job_AssignedResource] FOREIGN KEY([JobId])
|
---|
14 | REFERENCES [dbo].[Job] ([JobId])
|
---|
15 | ON UPDATE CASCADE
|
---|
16 | ON DELETE CASCADE
|
---|
17 | GO
|
---|
18 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Resource_AssignedResource]
|
---|
19 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Resource_AssignedResource] FOREIGN KEY([ResourceId])
|
---|
20 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
21 | ON UPDATE CASCADE
|
---|
22 | ON DELETE CASCADE
|
---|
23 | GO
|
---|
24 |
|
---|
25 | ALTER TABLE dbo.Job ALTER COLUMN JobId ADD ROWGUIDCOL;
|
---|
26 | ALTER TABLE dbo.Job WITH NOCHECK ADD CONSTRAINT [DF_Job_JobId] DEFAULT (NEWSEQUENTIALID()) FOR JobId;
|
---|
27 | GO
|
---|
28 |
|
---|
29 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Job_StateLog]
|
---|
30 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Job_StateLog] FOREIGN KEY([JobId])
|
---|
31 | REFERENCES [dbo].[Job] ([JobId])
|
---|
32 | ON UPDATE CASCADE
|
---|
33 | ON DELETE CASCADE
|
---|
34 | GO
|
---|
35 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Resource_StateLog]
|
---|
36 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Resource_StateLog] FOREIGN KEY([SlaveId])
|
---|
37 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
38 | ON UPDATE CASCADE
|
---|
39 | ON DELETE SET NULL
|
---|
40 | GO
|
---|
41 |
|
---|
42 | ALTER TABLE dbo.Plugin ALTER COLUMN PluginId ADD ROWGUIDCOL;
|
---|
43 | ALTER TABLE dbo.Plugin WITH NOCHECK ADD CONSTRAINT [DF_Plugin_PluginId] DEFAULT (NEWSEQUENTIALID()) FOR PluginId;
|
---|
44 |
|
---|
45 | ALTER TABLE dbo.PluginData ALTER COLUMN PluginDataId ADD ROWGUIDCOL;
|
---|
46 | ALTER TABLE dbo.PluginData WITH NOCHECK ADD CONSTRAINT [DF_PluginData_PluginDataId] DEFAULT (NEWSEQUENTIALID()) FOR PluginDataId;
|
---|
47 |
|
---|
48 | ALTER TABLE [dbo].[PluginData] DROP CONSTRAINT [Plugin_PluginData]
|
---|
49 | ALTER TABLE [dbo].[PluginData] WITH CHECK ADD CONSTRAINT [Plugin_PluginData] FOREIGN KEY([PluginId])
|
---|
50 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
51 | ON UPDATE CASCADE
|
---|
52 | ON DELETE CASCADE
|
---|
53 | GO
|
---|
54 |
|
---|
55 | ALTER TABLE dbo.RequiredPlugins ALTER COLUMN RequiredPluginId ADD ROWGUIDCOL;
|
---|
56 | ALTER TABLE dbo.RequiredPlugins WITH NOCHECK ADD CONSTRAINT [DF_RequiredPlugins_RequiredPluginId] DEFAULT (NEWSEQUENTIALID()) FOR RequiredPluginId;
|
---|
57 |
|
---|
58 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Job_RequiredPlugin]
|
---|
59 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Job_RequiredPlugin] FOREIGN KEY([JobId])
|
---|
60 | REFERENCES [dbo].[Job] ([JobId])
|
---|
61 | ON UPDATE CASCADE
|
---|
62 | ON DELETE CASCADE
|
---|
63 | GO
|
---|
64 |
|
---|
65 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Plugin_RequiredPlugin]
|
---|
66 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Plugin_RequiredPlugin] FOREIGN KEY([PluginId])
|
---|
67 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
68 | ON UPDATE CASCADE
|
---|
69 | ON DELETE CASCADE
|
---|
70 | GO
|
---|
71 |
|
---|
72 | ALTER TABLE dbo.Resource ALTER COLUMN ResourceId ADD ROWGUIDCOL;
|
---|
73 | ALTER TABLE dbo.Resource WITH NOCHECK ADD CONSTRAINT [DF_Resource_ResourceId] DEFAULT (NEWSEQUENTIALID()) FOR ResourceId;
|
---|
74 |
|
---|
75 | ALTER TABLE dbo.UptimeCalendar ALTER COLUMN UptimeCalendarId ADD ROWGUIDCOL;
|
---|
76 | ALTER TABLE dbo.UptimeCalendar WITH NOCHECK ADD CONSTRAINT [DF_UptimeCalendar_UptimeCalendarId] DEFAULT (NEWSEQUENTIALID()) FOR UptimeCalendarId;
|
---|
77 |
|
---|
78 | ALTER TABLE dbo.HiveExperiment ALTER COLUMN HiveExperimentId ADD ROWGUIDCOL;
|
---|
79 | ALTER TABLE dbo.HiveExperiment WITH NOCHECK ADD CONSTRAINT [DF_HiveExperiment_HiveExperimentId] DEFAULT (NEWSEQUENTIALID()) FOR HiveExperimentId;
|
---|
80 |
|
---|
81 | ALTER TABLE dbo.StateLog ALTER COLUMN StateLogId ADD ROWGUIDCOL;
|
---|
82 | ALTER TABLE dbo.StateLog WITH NOCHECK ADD CONSTRAINT [DF_StateLog_StateLogId] DEFAULT (NEWSEQUENTIALID()) FOR StateLogId;
|
---|
83 |
|
---|
84 | ALTER TABLE [dbo].[HiveExperimentPermission] DROP CONSTRAINT [HiveExperiment_HiveExperimentPermission]
|
---|
85 | ALTER TABLE [dbo].[HiveExperimentPermission] WITH CHECK ADD CONSTRAINT [HiveExperiment_HiveExperimentPermission] FOREIGN KEY([HiveExperimentId])
|
---|
86 | REFERENCES [dbo].[HiveExperiment] ([HiveExperimentId])
|
---|
87 | ON UPDATE CASCADE
|
---|
88 | ON DELETE CASCADE
|
---|
89 | GO
|
---|
90 |
|
---|
91 | ALTER TABLE [dbo].[Statistics] ALTER COLUMN StatisticsId ADD ROWGUIDCOL;
|
---|
92 | ALTER TABLE [dbo].[Statistics] WITH NOCHECK ADD CONSTRAINT [DF_Statistics_StatisticsId] DEFAULT (NEWSEQUENTIALID()) FOR StatisticsId;
|
---|
93 | GO
|
---|
94 |
|
---|
95 | ALTER TABLE [dbo].[DeletedJobStatistics] ALTER COLUMN DeletedJobStatisticsId ADD ROWGUIDCOL;
|
---|
96 | ALTER TABLE [dbo].[DeletedJobStatistics] WITH NOCHECK ADD CONSTRAINT [DF_DeletedJobStatistics_DeletedJobStatisticsId] DEFAULT (NEWSEQUENTIALID()) FOR DeletedJobStatisticsId;
|
---|
97 | GO
|
---|
98 |
|
---|
99 | ALTER TABLE [dbo].[SlaveStatistics] DROP CONSTRAINT [Statistics_SlaveStatistics]
|
---|
100 | ALTER TABLE [dbo].[SlaveStatistics] WITH CHECK ADD CONSTRAINT [Statistics_SlaveStatistics] FOREIGN KEY([StatisticsId])
|
---|
101 | REFERENCES [dbo].[Statistics] ([StatisticsId])
|
---|
102 | ON UPDATE CASCADE
|
---|
103 | ON DELETE CASCADE
|
---|
104 | GO
|
---|
105 |
|
---|
106 | ALTER TABLE [dbo].[UserStatistics] DROP CONSTRAINT [Statistics_UserStatistics]
|
---|
107 | ALTER TABLE [dbo].[UserStatistics] WITH CHECK ADD CONSTRAINT [Statistics_UserStatistics] FOREIGN KEY([StatisticsId])
|
---|
108 | REFERENCES [dbo].[Statistics] ([StatisticsId])
|
---|
109 | ON UPDATE CASCADE
|
---|
110 | ON DELETE CASCADE
|
---|
111 | GO
|
---|
112 |
|
---|
113 | /* create indices */
|
---|
114 | CREATE INDEX Index_RequiredPlugins_JobId ON RequiredPlugins(JobId);
|
---|
115 | GO
|
---|
116 |
|
---|
117 | /* views */
|
---|
118 | -- =============================================
|
---|
119 | -- Author: cneumuel
|
---|
120 | -- Description: Returns the first StateLog entry for each job
|
---|
121 | -- =============================================
|
---|
122 | CREATE VIEW [dbo].[view_FirstState]
|
---|
123 | AS
|
---|
124 | SELECT sl.JobId, sl.DateTime, sl.State
|
---|
125 | FROM dbo.StateLog AS sl INNER JOIN
|
---|
126 | (SELECT JobId, MIN(DateTime) AS DateTime
|
---|
127 | FROM dbo.StateLog
|
---|
128 | GROUP BY JobId) AS minDate ON sl.DateTime = minDate.DateTime AND sl.JobId = minDate.JobId
|
---|
129 |
|
---|
130 | GO
|
---|
131 |
|
---|
132 | -- =============================================
|
---|
133 | -- Author: cneumuel
|
---|
134 | -- Description: Returns the last StateLog entry for each job
|
---|
135 | -- =============================================
|
---|
136 | CREATE VIEW [dbo].[view_LastState]
|
---|
137 | AS
|
---|
138 | SELECT sl.JobId, sl.DateTime, sl.State
|
---|
139 | FROM dbo.StateLog AS sl INNER JOIN
|
---|
140 | (SELECT JobId, MAX(DateTime) AS DateTime
|
---|
141 | FROM dbo.StateLog
|
---|
142 | GROUP BY JobId) AS minDate ON sl.DateTime = minDate.DateTime AND sl.JobId = minDate.JobId
|
---|
143 | GO
|
---|
144 |
|
---|
145 | -- =============================================
|
---|
146 | -- Author: cneumuel
|
---|
147 | -- Description: returns aggregates statistic information for every minute
|
---|
148 | -- =============================================
|
---|
149 | CREATE VIEW [dbo].[view_Statistics]
|
---|
150 | AS
|
---|
151 | SELECT CONVERT(VARCHAR(19), MIN(s.Timestamp), 120) AS DateTime, SUM(ss.Cores) AS Cores, SUM(ss.FreeCores) AS FreeCores,
|
---|
152 | AVG(ss.CpuUtilization) AS CpuUtilization, SUM(ss.Memory) AS Memory, SUM(ss.FreeMemory) AS FreeMemory, x.exSum AS ExecutionTimeHours,
|
---|
153 | x.exFinishedSum AS ExecutionTimeFinished, x.exStartToEndSum AS StartToEndTimeFinished
|
---|
154 | FROM dbo.SlaveStatistics AS ss INNER JOIN
|
---|
155 | dbo.[Statistics] AS s ON ss.StatisticsId = s.StatisticsId INNER JOIN
|
---|
156 | dbo.Resource AS r ON ss.SlaveId = r.ResourceId INNER JOIN
|
---|
157 | (SELECT StatisticsId, SUM(ExecutionTimeMs) / 1000 / 60 / 60 AS exSum, SUM(ExecutionTimeMsFinishedJobs) / 1000 / 60 / 60 AS exFinishedSum,
|
---|
158 | SUM(StartToEndTimeMs) / 1000 / 60 / 60 AS exStartToEndSum
|
---|
159 | FROM dbo.UserStatistics AS us
|
---|
160 | GROUP BY StatisticsId) AS x ON s.StatisticsId = x.StatisticsId
|
---|
161 | GROUP BY s.StatisticsId, x.exSum, x.exFinishedSum, x.exStartToEndSum
|
---|
162 |
|
---|
163 | /* triggers */
|
---|
164 | GO
|
---|
165 | /****** Object: Trigger [dbo].[tr_HiveExperimentDeleteCascade] Script Date: 04/19/2011 16:31:53 ******/
|
---|
166 | SET ANSI_NULLS ON
|
---|
167 | GO
|
---|
168 | SET QUOTED_IDENTIFIER ON
|
---|
169 | GO
|
---|
170 | -- =============================================
|
---|
171 | -- Author: cneumuel
|
---|
172 | -- Create date: 19.04.2011
|
---|
173 | -- Description: (1) Writes the execution times of deleted jobs into DeletedJobStats to ensure correct statistics
|
---|
174 | -- (2) Deletes all associated jobs. This cannot be done with cascading delete,
|
---|
175 | -- because the job table defines a INSTEAD OF DELETE trigger itself, which
|
---|
176 | -- is not compatible with cascading deletes.
|
---|
177 | -- =============================================
|
---|
178 | CREATE TRIGGER [dbo].[tr_HiveExperimentDeleteCascade] ON [dbo].[HiveExperiment] INSTEAD OF DELETE AS
|
---|
179 | BEGIN
|
---|
180 | DELETE Job FROM deleted, Job WHERE deleted.HiveExperimentId = Job.HiveExperimentId
|
---|
181 | DELETE HiveExperiment FROM deleted, HiveExperiment WHERE deleted.HiveExperimentId = HiveExperiment.HiveExperimentId
|
---|
182 | END
|
---|
183 | GO
|
---|
184 |
|
---|
185 | -- =============================================
|
---|
186 | -- Author: cneumuel
|
---|
187 | -- Create date: 11.11.2010
|
---|
188 | -- Description: Recursively deletes all child-jobs of a job when it is deleted. (Source: http://devio.wordpress.com/2008/05/23/recursive-delete-in-sql-server/)
|
---|
189 | -- =============================================
|
---|
190 | CREATE TRIGGER [dbo].[tr_JobDeleteCascade] ON [dbo].[Job] INSTEAD OF DELETE AS
|
---|
191 | BEGIN
|
---|
192 | -- add statistics
|
---|
193 | INSERT INTO dbo.DeletedJobStatistics (UserId, ExecutionTimeMs, ExecutionTimeMsFinishedJobs, StartToEndTimeMs)
|
---|
194 | SELECT
|
---|
195 | he.OwnerUserId AS UserId,
|
---|
196 | SUM(j.ExecutionTimeMs) AS ExecutionTimeMs,
|
---|
197 | ISNULL(SUM(CASE ls.State WHEN 'Finished' THEN j.ExecutionTimeMs END), 0) AS ExecutionTimeMsFinishedJobs,
|
---|
198 | ISNULL(SUM(CASE ls.State WHEN 'Finished' THEN DATEDIFF(MS,fs.DateTime,ls.DateTime) ELSE 0 END), 0) AS StartToEndMs
|
---|
199 | FROM
|
---|
200 | deleted j,
|
---|
201 | HiveExperiment he,
|
---|
202 | view_FirstState fs,
|
---|
203 | view_LastState ls
|
---|
204 | WHERE
|
---|
205 | he.HiveExperimentId = j.HiveExperimentId AND
|
---|
206 | fs.JobId = j.JobId AND
|
---|
207 | ls.JobId = j.JobId
|
---|
208 | GROUP BY he.OwnerUserId
|
---|
209 |
|
---|
210 | -- recursively delete jobs
|
---|
211 | CREATE TABLE #Table(
|
---|
212 | JobId uniqueidentifier
|
---|
213 | )
|
---|
214 | INSERT INTO #Table (JobId)
|
---|
215 | SELECT JobId FROM deleted
|
---|
216 |
|
---|
217 | DECLARE @c INT
|
---|
218 | SET @c = 0
|
---|
219 |
|
---|
220 | WHILE @c <> (SELECT COUNT(JobId) FROM #Table) BEGIN
|
---|
221 | SELECT @c = COUNT(JobId) FROM #Table
|
---|
222 |
|
---|
223 | INSERT INTO #Table (JobId)
|
---|
224 | SELECT Job.JobId
|
---|
225 | FROM Job
|
---|
226 | LEFT OUTER JOIN #Table ON Job.JobId = #Table.JobId
|
---|
227 | WHERE Job.ParentJobId IN (SELECT JobId FROM #Table)
|
---|
228 | AND #Table.JobId IS NULL
|
---|
229 | END
|
---|
230 |
|
---|
231 | DELETE JobData FROM JobData INNER JOIN #Table ON JobData.JobId = #Table.JobId
|
---|
232 | DELETE Job FROM Job INNER JOIN #Table ON Job.JobId = #Table.JobId
|
---|
233 | END
|
---|
234 | GO
|
---|
235 |
|
---|
236 | -- =============================================
|
---|
237 | -- Author: cneumuel
|
---|
238 | -- Create date: 17.05.2011
|
---|
239 | -- Description: Writes the execution times of deleted jobs into DeletedJobStats to ensure correct statistics
|
---|
240 | -- =============================================
|
---|
241 | --CREATE TRIGGER [dbo].[tr_DeletedJobStats] ON [dbo].[Job] AFTER DELETE AS
|
---|
242 | --BEGIN
|
---|
243 |
|
---|
244 |
|
---|
245 | --END
|
---|
246 | --GO
|
---|