[6983] | 1 | USE [HeuristicLab.Hive-3.3]
|
---|
| 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] DROP CONSTRAINT [Task_AssignedResource]
|
---|
| 11 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Task_AssignedResource] FOREIGN KEY([TaskId])
|
---|
| 12 | REFERENCES [dbo].[Task] ([TaskId])
|
---|
| 13 | ON UPDATE CASCADE
|
---|
| 14 | ON DELETE CASCADE
|
---|
| 15 | GO
|
---|
| 16 | ALTER TABLE [dbo].[AssignedResources] DROP CONSTRAINT [Resource_AssignedResource]
|
---|
| 17 | ALTER TABLE [dbo].[AssignedResources] WITH CHECK ADD CONSTRAINT [Resource_AssignedResource] FOREIGN KEY([ResourceId])
|
---|
| 18 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
| 19 | ON UPDATE CASCADE
|
---|
| 20 | ON DELETE CASCADE
|
---|
| 21 | GO
|
---|
| 22 |
|
---|
| 23 | ALTER TABLE dbo.Task ALTER COLUMN TaskId ADD ROWGUIDCOL;
|
---|
| 24 | ALTER TABLE dbo.Task WITH NOCHECK ADD CONSTRAINT [DF_Task_TaskId] DEFAULT (NEWSEQUENTIALID()) FOR TaskId;
|
---|
| 25 | GO
|
---|
| 26 |
|
---|
| 27 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Task_StateLog]
|
---|
| 28 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Task_StateLog] FOREIGN KEY([TaskId])
|
---|
| 29 | REFERENCES [dbo].[Task] ([TaskId])
|
---|
| 30 | ON UPDATE CASCADE
|
---|
| 31 | ON DELETE CASCADE
|
---|
| 32 | GO
|
---|
| 33 | ALTER TABLE [dbo].[StateLog] DROP CONSTRAINT [Resource_StateLog]
|
---|
| 34 | ALTER TABLE [dbo].[StateLog] WITH CHECK ADD CONSTRAINT [Resource_StateLog] FOREIGN KEY([SlaveId])
|
---|
| 35 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
| 36 | ON UPDATE CASCADE
|
---|
| 37 | ON DELETE SET NULL
|
---|
| 38 | GO
|
---|
| 39 |
|
---|
| 40 | ALTER TABLE dbo.Plugin ALTER COLUMN PluginId ADD ROWGUIDCOL;
|
---|
| 41 | ALTER TABLE dbo.Plugin WITH NOCHECK ADD CONSTRAINT [DF_Plugin_PluginId] DEFAULT (NEWSEQUENTIALID()) FOR PluginId;
|
---|
| 42 |
|
---|
| 43 | ALTER TABLE dbo.PluginData WITH NOCHECK ADD CONSTRAINT [DF_PluginData_PluginDataId] DEFAULT (NEWSEQUENTIALID()) FOR PluginDataId;
|
---|
| 44 |
|
---|
| 45 | ALTER TABLE [dbo].[PluginData] DROP CONSTRAINT [Plugin_PluginData]
|
---|
| 46 | ALTER TABLE [dbo].[PluginData] WITH CHECK ADD CONSTRAINT [Plugin_PluginData] FOREIGN KEY([PluginId])
|
---|
| 47 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
| 48 | ON UPDATE CASCADE
|
---|
| 49 | ON DELETE CASCADE
|
---|
| 50 | GO
|
---|
| 51 |
|
---|
| 52 | ALTER TABLE dbo.RequiredPlugins ALTER COLUMN RequiredPluginId ADD ROWGUIDCOL;
|
---|
| 53 | ALTER TABLE dbo.RequiredPlugins WITH NOCHECK ADD CONSTRAINT [DF_RequiredPlugins_RequiredPluginId] DEFAULT (NEWSEQUENTIALID()) FOR RequiredPluginId;
|
---|
| 54 |
|
---|
| 55 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Task_RequiredPlugin]
|
---|
| 56 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Task_RequiredPlugin] FOREIGN KEY([TaskId])
|
---|
| 57 | REFERENCES [dbo].[Task] ([TaskId])
|
---|
| 58 | ON UPDATE CASCADE
|
---|
| 59 | ON DELETE CASCADE
|
---|
| 60 | GO
|
---|
| 61 |
|
---|
| 62 | ALTER TABLE [dbo].[RequiredPlugins] DROP CONSTRAINT [Plugin_RequiredPlugin]
|
---|
| 63 | ALTER TABLE [dbo].[RequiredPlugins] WITH CHECK ADD CONSTRAINT [Plugin_RequiredPlugin] FOREIGN KEY([PluginId])
|
---|
| 64 | REFERENCES [dbo].[Plugin] ([PluginId])
|
---|
| 65 | ON UPDATE CASCADE
|
---|
| 66 | ON DELETE CASCADE
|
---|
| 67 | GO
|
---|
| 68 |
|
---|
| 69 | ALTER TABLE dbo.Resource ALTER COLUMN ResourceId ADD ROWGUIDCOL;
|
---|
| 70 | ALTER TABLE dbo.Resource WITH NOCHECK ADD CONSTRAINT [DF_Resource_ResourceId] DEFAULT (NEWSEQUENTIALID()) FOR ResourceId;
|
---|
| 71 |
|
---|
| 72 | ALTER TABLE dbo.Downtime ALTER COLUMN DowntimeId ADD ROWGUIDCOL;
|
---|
| 73 | ALTER TABLE dbo.Downtime WITH NOCHECK ADD CONSTRAINT [DF_Downtime_DowntimeId] DEFAULT (NEWSEQUENTIALID()) FOR DowntimeId;
|
---|
| 74 |
|
---|
| 75 | ALTER TABLE dbo.Job ALTER COLUMN JobId ADD ROWGUIDCOL;
|
---|
| 76 | ALTER TABLE dbo.Job WITH NOCHECK ADD CONSTRAINT [DF_Job_JobId] DEFAULT (NEWSEQUENTIALID()) FOR JobId;
|
---|
| 77 |
|
---|
| 78 | ALTER TABLE dbo.StateLog ALTER COLUMN StateLogId ADD ROWGUIDCOL;
|
---|
| 79 | ALTER TABLE dbo.StateLog WITH NOCHECK ADD CONSTRAINT [DF_StateLog_StateLogId] DEFAULT (NEWSEQUENTIALID()) FOR StateLogId;
|
---|
| 80 |
|
---|
| 81 | ALTER TABLE [dbo].[JobPermission] DROP CONSTRAINT [Job_JobPermission]
|
---|
| 82 | ALTER TABLE [dbo].[JobPermission] WITH CHECK ADD CONSTRAINT [Job_JobPermission] FOREIGN KEY([JobId])
|
---|
| 83 | REFERENCES [dbo].[Job] ([JobId])
|
---|
| 84 | ON UPDATE CASCADE
|
---|
| 85 | ON DELETE CASCADE
|
---|
| 86 | GO
|
---|
| 87 |
|
---|
[7916] | 88 | ALTER TABLE [dbo].[ResourcePermission] DROP CONSTRAINT [Resource_ResourcePermission]
|
---|
| 89 | ALTER TABLE [dbo].[ResourcePermission] WITH CHECK ADD CONSTRAINT [Resource_ResourcePermission] FOREIGN KEY([ResourceId])
|
---|
| 90 | REFERENCES [dbo].[Resource] ([ResourceId])
|
---|
| 91 | ON UPDATE CASCADE
|
---|
| 92 | ON DELETE CASCADE
|
---|
| 93 | GO
|
---|
| 94 |
|
---|
[6983] | 95 | ALTER TABLE [dbo].[Statistics] ALTER COLUMN StatisticsId ADD ROWGUIDCOL;
|
---|
| 96 | ALTER TABLE [dbo].[Statistics] WITH NOCHECK ADD CONSTRAINT [DF_Statistics_StatisticsId] DEFAULT (NEWSEQUENTIALID()) FOR StatisticsId;
|
---|
| 97 | GO
|
---|
| 98 |
|
---|
| 99 | ALTER TABLE [dbo].[DeletedJobStatistics] ALTER COLUMN DeletedJobStatisticsId ADD ROWGUIDCOL;
|
---|
| 100 | ALTER TABLE [dbo].[DeletedJobStatistics] WITH NOCHECK ADD CONSTRAINT [DF_DeletedJobStatistics_DeletedJobStatisticsId] DEFAULT (NEWSEQUENTIALID()) FOR DeletedJobStatisticsId;
|
---|
| 101 | GO
|
---|
| 102 |
|
---|
| 103 | ALTER TABLE [dbo].[SlaveStatistics] DROP CONSTRAINT [Statistics_SlaveStatistics]
|
---|
| 104 | ALTER TABLE [dbo].[SlaveStatistics] WITH CHECK ADD CONSTRAINT [Statistics_SlaveStatistics] FOREIGN KEY([StatisticsId])
|
---|
| 105 | REFERENCES [dbo].[Statistics] ([StatisticsId])
|
---|
| 106 | ON UPDATE CASCADE
|
---|
| 107 | ON DELETE CASCADE
|
---|
| 108 | GO
|
---|
| 109 |
|
---|
| 110 | ALTER TABLE [dbo].[UserStatistics] DROP CONSTRAINT [Statistics_UserStatistics]
|
---|
| 111 | ALTER TABLE [dbo].[UserStatistics] WITH CHECK ADD CONSTRAINT [Statistics_UserStatistics] FOREIGN KEY([StatisticsId])
|
---|
| 112 | REFERENCES [dbo].[Statistics] ([StatisticsId])
|
---|
| 113 | ON UPDATE CASCADE
|
---|
| 114 | ON DELETE CASCADE
|
---|
| 115 | GO
|
---|
| 116 |
|
---|
| 117 | /* create indices */
|
---|
| 118 | CREATE INDEX Index_RequiredPlugins_TaskId ON RequiredPlugins(TaskId);
|
---|
| 119 | GO
|
---|
| 120 |
|
---|
| 121 | /* views */
|
---|
| 122 | -- =============================================
|
---|
| 123 | -- Author: cneumuel
|
---|
| 124 | -- Description: Returns the first StateLog entry for each job
|
---|
| 125 | -- =============================================
|
---|
| 126 | CREATE VIEW [dbo].[view_FirstState]
|
---|
| 127 | AS
|
---|
| 128 | SELECT sl.TaskId, sl.DateTime, sl.State
|
---|
| 129 | FROM dbo.StateLog AS sl INNER JOIN
|
---|
| 130 | (SELECT TaskId, MIN(DateTime) AS DateTime
|
---|
| 131 | FROM dbo.StateLog
|
---|
| 132 | GROUP BY TaskId) AS minDate ON sl.DateTime = minDate.DateTime AND sl.TaskId = minDate.TaskId
|
---|
| 133 |
|
---|
| 134 | GO
|
---|
| 135 |
|
---|
| 136 | -- =============================================
|
---|
| 137 | -- Author: cneumuel
|
---|
| 138 | -- Description: Returns the last StateLog entry for each job
|
---|
| 139 | -- =============================================
|
---|
| 140 | CREATE VIEW [dbo].[view_LastState]
|
---|
| 141 | AS
|
---|
| 142 | SELECT sl.TaskId, sl.DateTime, sl.State
|
---|
| 143 | FROM dbo.StateLog AS sl INNER JOIN
|
---|
| 144 | (SELECT TaskId, MAX(DateTime) AS DateTime
|
---|
| 145 | FROM dbo.StateLog
|
---|
| 146 | GROUP BY TaskId) AS minDate ON sl.DateTime = minDate.DateTime AND sl.TaskId = minDate.TaskId
|
---|
| 147 | GO
|
---|
| 148 |
|
---|
| 149 | -- =============================================
|
---|
| 150 | -- Author: cneumuel
|
---|
| 151 | -- Description: returns aggregates statistic information for every minute
|
---|
| 152 | -- =============================================
|
---|
| 153 | CREATE VIEW [dbo].[view_Statistics]
|
---|
| 154 | AS
|
---|
| 155 | SELECT CONVERT(VARCHAR(19), MIN(s.Timestamp), 120) AS DateTime, SUM(ss.Cores) AS Cores, SUM(ss.FreeCores) AS FreeCores,
|
---|
| 156 | AVG(ss.CpuUtilization) AS CpuUtilization, SUM(ss.Memory) AS Memory, SUM(ss.FreeMemory) AS FreeMemory, x.exSum AS ExecutionTimeHours,
|
---|
| 157 | x.exFinishedSum AS ExecutionTimeFinished, x.exStartToEndSum AS StartToEndTimeFinished
|
---|
| 158 | FROM dbo.SlaveStatistics AS ss INNER JOIN
|
---|
| 159 | dbo.[Statistics] AS s ON ss.StatisticsId = s.StatisticsId INNER JOIN
|
---|
| 160 | dbo.Resource AS r ON ss.SlaveId = r.ResourceId INNER JOIN
|
---|
| 161 | (SELECT StatisticsId, SUM(ExecutionTimeMs) / 1000 / 60 / 60 AS exSum, SUM(ExecutionTimeMsFinishedJobs) / 1000 / 60 / 60 AS exFinishedSum,
|
---|
| 162 | SUM(StartToEndTimeMs) / 1000 / 60 / 60 AS exStartToEndSum
|
---|
| 163 | FROM dbo.UserStatistics AS us
|
---|
| 164 | GROUP BY StatisticsId) AS x ON s.StatisticsId = x.StatisticsId
|
---|
| 165 | GROUP BY s.StatisticsId, x.exSum, x.exFinishedSum, x.exStartToEndSum
|
---|
| 166 |
|
---|
| 167 | /* triggers */
|
---|
| 168 | GO
|
---|
| 169 | /****** Object: Trigger [dbo].[tr_JobDeleteCascade] Script Date: 04/19/2011 16:31:53 ******/
|
---|
| 170 | SET ANSI_NULLS ON
|
---|
| 171 | GO
|
---|
| 172 | SET QUOTED_IDENTIFIER ON
|
---|
| 173 | GO
|
---|
| 174 | -- =============================================
|
---|
| 175 | -- Author: cneumuel
|
---|
| 176 | -- Create date: 19.04.2011
|
---|
| 177 | -- Description: (1) Writes the execution times of deleted jobs into DeletedJobStats to ensure correct statistics
|
---|
| 178 | -- (2) Deletes all associated jobs. This cannot be done with cascading delete,
|
---|
| 179 | -- because the job table defines a INSTEAD OF DELETE trigger itself, which
|
---|
| 180 | -- is not compatible with cascading deletes.
|
---|
| 181 | -- =============================================
|
---|
| 182 | CREATE TRIGGER [dbo].[tr_JobDeleteCascade] ON [dbo].[Job] INSTEAD OF DELETE AS
|
---|
| 183 | BEGIN
|
---|
| 184 | DELETE Task FROM deleted, Task WHERE deleted.JobId = Task.JobId
|
---|
| 185 | DELETE Job FROM deleted, Job WHERE deleted.JobId = Job.JobId
|
---|
| 186 | END
|
---|
| 187 | GO
|
---|
| 188 |
|
---|
| 189 | -- =============================================
|
---|
| 190 | -- Author: cneumuel
|
---|
| 191 | -- Create date: 11.11.2010
|
---|
| 192 | -- 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/)
|
---|
| 193 | -- =============================================
|
---|
| 194 | CREATE TRIGGER [dbo].[tr_TaskDeleteCascade] ON [dbo].[Task] INSTEAD OF DELETE AS
|
---|
| 195 | BEGIN
|
---|
| 196 | -- add statistics
|
---|
| 197 | INSERT INTO dbo.DeletedJobStatistics (UserId, ExecutionTimeS, ExecutionTimeSFinishedJobs, StartToEndTimeS)
|
---|
| 198 | SELECT
|
---|
| 199 | he.OwnerUserId AS UserId,
|
---|
| 200 | ROUND(SUM(j.ExecutionTimeMs) / 1000, 0) AS ExecutionTimeS,
|
---|
| 201 | ROUND(ISNULL(SUM(CASE ls.State WHEN 'Finished' THEN j.ExecutionTimeMs END), 0) / 1000, 0) AS ExecutionTimeSFinishedJobs,
|
---|
| 202 | ISNULL(SUM(CASE ls.State WHEN 'Finished' THEN DATEDIFF(s, fs.DateTime, ls.DateTime) ELSE 0 END), 0) AS StartToEndTimeS
|
---|
| 203 | FROM
|
---|
| 204 | deleted j,
|
---|
| 205 | Job he,
|
---|
| 206 | view_FirstState fs,
|
---|
| 207 | view_LastState ls
|
---|
| 208 | WHERE
|
---|
| 209 | he.JobId = j.JobId AND
|
---|
| 210 | fs.TaskId = j.TaskId AND
|
---|
| 211 | ls.TaskId = j.TaskId
|
---|
| 212 | GROUP BY he.OwnerUserId
|
---|
| 213 |
|
---|
| 214 | -- recursively delete jobs
|
---|
| 215 | CREATE TABLE #Table(
|
---|
| 216 | TaskId uniqueidentifier
|
---|
| 217 | )
|
---|
| 218 | INSERT INTO #Table (TaskId)
|
---|
| 219 | SELECT TaskId FROM deleted
|
---|
| 220 |
|
---|
| 221 | DECLARE @c INT
|
---|
| 222 | SET @c = 0
|
---|
| 223 |
|
---|
| 224 | WHILE @c <> (SELECT COUNT(TaskId) FROM #Table) BEGIN
|
---|
| 225 | SELECT @c = COUNT(TaskId) FROM #Table
|
---|
| 226 |
|
---|
| 227 | INSERT INTO #Table (TaskId)
|
---|
| 228 | SELECT Task.TaskId
|
---|
| 229 | FROM Task
|
---|
| 230 | LEFT OUTER JOIN #Table ON Task.TaskId = #Table.TaskId
|
---|
| 231 | WHERE Task.ParentTaskId IN (SELECT TaskId FROM #Table)
|
---|
| 232 | AND #Table.TaskId IS NULL
|
---|
| 233 | END
|
---|
| 234 |
|
---|
| 235 | DELETE TaskData FROM TaskData INNER JOIN #Table ON TaskData.TaskId = #Table.TaskId
|
---|
| 236 | DELETE Task FROM Task INNER JOIN #Table ON Task.TaskId = #Table.TaskId
|
---|
| 237 | END
|
---|
| 238 | GO
|
---|
| 239 |
|
---|
[7185] | 240 |
|
---|
[7814] | 241 | CREATE TRIGGER [dbo].[tr_StatisticsDeleteCascade] ON [dbo].[Statistics] INSTEAD OF DELETE AS
|
---|
[7185] | 242 | BEGIN
|
---|
| 243 | DELETE SlaveStatistics FROM deleted, SlaveStatistics WHERE deleted.StatisticsId = SlaveStatistics.StatisticsId
|
---|
| 244 | -- should also remove UserStatistics here
|
---|
| 245 | DELETE [Statistics] FROM deleted, [Statistics] WHERE deleted.StatisticsId = [Statistics].StatisticsId
|
---|
| 246 | END
|
---|
| 247 | GO
|
---|
| 248 |
|
---|
| 249 |
|
---|
[6983] | 250 | -- ============================================================
|
---|
| 251 | -- Description: Create indices to speed up execution of queries
|
---|
| 252 | -- ============================================================
|
---|
| 253 |
|
---|
| 254 | -- speed up joins between Job and Task
|
---|
| 255 | CREATE NONCLUSTERED INDEX [TaskJobIdIndex]
|
---|
| 256 | ON [dbo].[Task] ([JobId])
|
---|
| 257 | INCLUDE ([TaskId],[TaskState],[ExecutionTimeMs],[LastHeartbeat],[ParentTaskId],[Priority],[CoresNeeded],[MemoryNeeded],[IsParentTask],[FinishWhenChildJobsFinished],[Command],[IsPrivileged])
|
---|
| 258 | GO
|
---|
| 259 |
|
---|
| 260 | -- this is an index to speed up the GetWaitingTasks() method
|
---|
| 261 | CREATE NONCLUSTERED INDEX [TaskGetWaitingTasksIndex]
|
---|
| 262 | ON [dbo].[Task] ([TaskState],[IsParentTask],[FinishWhenChildJobsFinished],[CoresNeeded],[MemoryNeeded])
|
---|
| 263 | INCLUDE ([TaskId],[ExecutionTimeMs],[LastHeartbeat],[ParentTaskId],[Priority],[Command],[JobId],[IsPrivileged])
|
---|
| 264 | GO
|
---|