-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00A Temporal.sql
More file actions
executable file
·431 lines (396 loc) · 13.3 KB
/
Copy path00A Temporal.sql
File metadata and controls
executable file
·431 lines (396 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-------------------------------------------------------------------------------------------------------------
--------------------------------------------- PARTITION SCHEME ----------------------------------------------
drop table if exists Fact;
drop table if exists Dimension_Mutable;
drop table if exists Dimension_Immutable;
begin try drop partition scheme Yearly; end try begin catch end catch
begin try drop partition function Yearly; end try begin catch end catch
create partition function Yearly (smalldatetime)
as range right for values (
'20000101', '20010101', '20020101', '20030101', '20040101',
'20050101', '20060101', '20070101', '20080101', '20090101',
'20100101', '20110101', '20120101', '20130101', '20140101',
'20150101', '20160101', '20170101', '20180101', '20190101',
'20200101', '20210101', '20220101', '20230101', '20240101'
);
create partition scheme Yearly
as partition Yearly all to ([PRIMARY]);
-------------------------------------------------------------------------------------------------------------
-------------------------------------------- TEMPORAL DIMENSION ---------------------------------------------
create table Dimension_Immutable (
dim_ID int not null,
primary key (dim_ID asc)
);
create table Dimension_Mutable (
dim_ID int not null,
dim_ValidFrom smalldatetime not null,
dimProperty char(42) not null,
foreign key (dim_ID) references Dimension_Immutable (dim_ID),
primary key (dim_ID asc, dim_ValidFrom desc)
) on Yearly(dim_ValidFrom);
create table Fact (
dim_ID int not null,
factDate smalldatetime not null,
factMeasure smallmoney not null,
foreign key (dim_ID) references Dimension_Immutable (dim_ID),
primary key (dim_ID asc, factDate asc)
) on Yearly(factDate);
-------------------------------------------------------------------------------------------------------------
----------------------------------------- POPULATING THE DIMENSION ------------------------------------------
-- ~ 5 minutes loading time
-------------------------------------------------------------------------------------------------------------
declare @numberOfUniques int = power(2, 20);
with idGen as (
select 1 as id
union all
select id + 1 from idGen where id < @numberOfUniques
)
insert into Dimension_Immutable (dim_ID)
select id from idGen
option (MAXRECURSION 0);
-- select count(*) from Dimension_Immutable;
-- truncate table Dimension_Mutable;
declare @changeEveryNumberOfMinutes int = 5; -- every five minutes
with versions as (
select
dim_Id,
case
when dim_ID = 1 then power(2, 20)
when dim_ID between power(2, 1) and power(2, 2) then power(2, 19)
when dim_ID between power(2, 2) and power(2, 3) then power(2, 18)
when dim_ID between power(2, 3) and power(2, 4) then power(2, 17)
when dim_ID between power(2, 4) and power(2, 5) then power(2, 16)
when dim_ID between power(2, 5) and power(2, 6) then power(2, 15)
when dim_ID between power(2, 6) and power(2, 7) then power(2, 14)
when dim_ID between power(2, 7) and power(2, 8) then power(2, 13)
when dim_ID between power(2, 8) and power(2, 9) then power(2, 12)
when dim_ID between power(2, 9) and power(2, 10) then power(2, 11)
when dim_ID between power(2, 10) and power(2, 11) then power(2, 10)
when dim_ID between power(2, 11) and power(2, 12) then power(2, 9)
when dim_ID between power(2, 12) and power(2, 13) then power(2, 8)
when dim_ID between power(2, 13) and power(2, 14) then power(2, 7)
when dim_ID between power(2, 14) and power(2, 15) then power(2, 6)
when dim_ID between power(2, 15) and power(2, 16) then power(2, 5)
when dim_ID between power(2, 16) and power(2, 17) then power(2, 4)
when dim_ID between power(2, 17) and power(2, 18) then power(2, 3)
when dim_ID between power(2, 18) and power(2, 19) then power(2, 2)
when dim_ID between power(2, 19) and power(2, 20) then power(2, 1)
else 1 -- n/a
end as NumberOfVersions
from Dimension_Immutable
),
versioned_rows as (
select dim_ID, NumberOfVersions - 1 as CurrentVersion, NumberOfVersions,
dateadd(minute, -power(2, 20) * @changeEveryNumberOfMinutes, '2018-01-01') as ValidFrom
from versions
union all
select dim_ID, CurrentVersion - 1, NumberOfVersions,
dateadd(minute, (-1E0 * CurrentVersion / NumberOfVersions) * power(2, 20) * @changeEveryNumberOfMinutes, '2018-01-01') as ValidFrom
from versioned_rows where CurrentVersion > 0
)
insert into Dimension_Mutable (dim_ID, dim_ValidFrom, dimProperty)
select dim_ID, ValidFrom,
'dimProperty value ' + cast(dim_ID as varchar(10)) + ' since ' + convert(char(10), ValidFrom, 121)
from versioned_rows
option (MAXRECURSION 0);
-- select count(distinct dim_ID), count(*), min(dim_ValidFrom), max(dim_ValidFrom) from Dimension_Mutable;
-- 1 048 576 21 495 808 2008-01-13 02:40 2017-12-31 23:55
-- select datediff(minute, '2008-01-13 02:40', '2017-12-31 23:55');
-- 5 242 875
-------------------------------------------------------------------------------------------------------------
------------------------------------------- POPULATING THE FACTS --------------------------------------------
-- ~ 5 minutes loading time
-------------------------------------------------------------------------------------------------------------
-- truncate table Fact;
declare @firstDate smalldatetime = (
select min(dim_ValidFrom) from Dimension_Mutable
);
declare @lastDate smalldatetime = (
select max(dim_ValidFrom) from Dimension_Mutable
);
declare @dimSize int = (
select count(*) from Dimension_Immutable
);
with minutely as (
select @firstDate as factDate
union all
select dateadd(minute, 1, factDate) from minutely
where factDate < @lastDate
),
ten as (
select top 10 dim_ID
from Dimension_Immutable
order by dim_ID
),
veryLikelyTenRandomPerMinute as (
select distinct
-- inversely squarely proportional towards dim_ID,
-- skews distribution of facts towards dimension members with more versions
1 + cast(square(rand(checksum(newid()))) * @dimSize as int) as dim_ID,
m.factDate
from minutely m
cross apply ten t
)
insert into Fact (dim_ID, factDate, factMeasure)
select dim_ID, factDate, 1E0 * dim_ID / 100
from veryLikelyTenRandomPerMinute
option (MAXRECURSION 0);
go
-- select count(distinct dim_ID), count(*), min(factDate), max(factDate) from Fact;
-- 1 048 576 52 427 836 2008-01-13 02:40 2017-12-31 23:55
-------------------------------------------------------------------------------------------------------------
------------------------------------ REDUCE INDEX FRAGMENTATION ---------------------------------------------
ALTER INDEX ALL ON Dimension_Immutable REBUILD;
ALTER INDEX ALL ON Dimension_Mutable REBUILD;
ALTER INDEX ALL ON Fact REBUILD;
/*
SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(),
OBJECT_ID(N'Fact'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b
ON a.object_id = b.object_id AND a.index_id = b.index_id;
*/
-------------------------------------------------------------------------------------------------------------
----------------------------- CREATING A POINT-IN-TIME PARAMETRIZED VIEW ------------------------------------
drop function if exists pitDimension;
go
-- select count(*) from pitDimension('2012-01-01');
create function pitDimension (
@timepoint smalldatetime
)
returns table as return
select
dm_in_effect.dim_ID,
dm_in_effect.dim_ValidFrom,
dm_in_effect.dimProperty
from (
select
*,
ROW_NUMBER() over (partition by dim_ID order by dim_ValidFrom desc) as ReversedVersion
from
Dimension_Mutable
where
dim_ValidFrom <= @timepoint
) dm_in_effect
where
dm_in_effect.ReversedVersion = 1;
go
-------------------------------------------------------------------------------------------------------------
-------------------------------- CREATING A TWINING PARAMETRIZED VIEW ---------------------------------------
drop function if exists twineFact;
go
create function twineFact (
@fromTimepoint smalldatetime,
@toTimepoint smalldatetime
)
returns table as return
select
in_effect.dim_ID,
in_effect.factDate,
in_effect.dim_ValidFrom
from (
select
twine.dim_ID,
twine.Timepoint as factDate,
twine.Timeline,
MAX(case when Timeline = 'D' then Timepoint end) over (
partition by dim_ID order by Timepoint
) as dim_ValidFrom
from (
select
dim_ID,
factDate as Timepoint,
'F' as Timeline
from
dbo.Fact
where
factDate between @fromTimepoint and @toTimepoint
union all
select
dim_ID,
dim_ValidFrom as Timepoint,
'D' as Timeline
from
dbo.Dimension_Mutable
where
dim_ValidFrom <= @toTimepoint
) twine
) in_effect
where
in_effect.Timeline = 'F';
go
-------------------------------------------------------------------------------------------------------------
-------------------------------------------- PERFORM THE TESTING --------------------------------------------
declare @runs int = 4; -- including one run for statistics
declare @DB_ID int = DB_ID();
if OBJECT_ID('Timings') is null
begin
create table Timings (
model varchar(42) not null,
run int not null,
query char(3) not null,
executionTime int not null
);
end
declare @startingTime datetime2(7);
declare @endingTime datetime2(7);
set nocount on;
declare @updateStatistics bit = 1;
declare @model varchar(42) = 'Temporal';
delete Timings where model = @model and query in ('TIY', 'YIT', 'TOY');
while(@runs > 0)
begin
----------------------- Today is Yesterday -----------------------
-- clear all caches
DBCC FREESYSTEMCACHE('ALL');
DBCC FREESESSIONCACHE;
DBCC FREEPROCCACHE;
DBCC FLUSHPROCINDB(@DB_ID);
CHECKPOINT;
DBCC DROPCLEANBUFFERS;
drop table if exists #result_tiy;
set @startingTime = SYSDATETIME();
select
in_effect.dimProperty,
f.numberOfFacts,
f.avgMeasure
into
#result_tiy
from (
select
dim_Id,
count(*) as numberOfFacts,
avg(factMeasure) as avgMeasure
from
Fact
where
factDate between '2014-01-01' and '2014-12-31'
group by
dim_ID
) f
join
pitDimension('2018-01-01') in_effect
on
in_effect.dim_ID = f.dim_ID;
set @endingTime = SYSDATETIME();
drop table if exists #result_tiy;
insert into Timings (model, run, query, executionTime)
select @model, @runs, 'TIY' , datediff(ms, @startingTime, @endingTime)
where @updateStatistics = 0;
----------------------- Yesterday is Today -----------------------
-- clear all caches
DBCC FREESYSTEMCACHE('ALL');
DBCC FREESESSIONCACHE;
DBCC FREEPROCCACHE;
DBCC FLUSHPROCINDB(@DB_ID);
CHECKPOINT;
DBCC DROPCLEANBUFFERS;
drop table if exists #result_yit;
set @startingTime = SYSDATETIME();
select
in_effect.dimProperty,
f.numberOfFacts,
f.avgMeasure
into
#result_yit
from (
select
dim_Id,
count(*) as numberOfFacts,
avg(factMeasure) as avgMeasure
from
Fact
where
factDate between '2014-01-01' and '2014-12-31'
group by
dim_ID
) f
join
pitDimension('2014-01-01') in_effect
on
in_effect.dim_ID = f.dim_ID;
set @endingTime = SYSDATETIME();
drop table if exists #result_yit;
insert into Timings (model, run, query, executionTime)
select @model, @runs, 'YIT' , datediff(ms, @startingTime, @endingTime)
where @updateStatistics = 0;
----------------------- Today or Yesterday -----------------------
-- clear all caches
DBCC FREESYSTEMCACHE('ALL');
DBCC FREESESSIONCACHE;
DBCC FREEPROCCACHE;
DBCC FLUSHPROCINDB(@DB_ID);
CHECKPOINT;
DBCC DROPCLEANBUFFERS;
drop table if exists #result_toy;
set @startingTime = SYSDATETIME();
select
dm.dimProperty,
count(*) as numberOfFacts,
avg(f.factMeasure) as avgMeasure
into
#result_toy
from
twineFact('2014-01-01', '2014-12-31') in_effect
join
Fact f
on
f.dim_ID = in_effect.dim_ID
and
f.factDate = in_effect.factDate
join
Dimension_Mutable dm
on
dm.dim_ID = in_effect.dim_ID
and
dm.dim_ValidFrom = in_effect.dim_ValidFrom
group by
dm.dimProperty;
set @endingTime = SYSDATETIME();
drop table if exists #result_toy;
insert into Timings (model, run, query, executionTime)
select @model, @runs, 'TOY' , datediff(ms, @startingTime, @endingTime)
where @updateStatistics = 0;
if @updateStatistics = 1
begin
update statistics Dimension_Immutable with FULLSCAN;
update statistics Dimension_Mutable with FULLSCAN;
update statistics Fact with FULLSCAN;
set @updateStatistics = 0;
end
set @runs = @runs - 1;
end
-- select * from Timings;
select
rm.model,
rm.query,
round(rm.Median, 0) as Median,
round(1.96*ro.Deviation/sqrt(10), 0) as MarginOfError,
round(ro.Average, 0) as Average,
ro.Minimum,
ro.Maximum
from (
select distinct
model,
query,
PERCENTILE_CONT(0.5) within group
(order by executionTime) over (partition by model, query) as Median
from Timings
) rm
join (
select
model,
query,
avg(executionTime) as Average,
min(executionTime) as Minimum,
max(executionTime) as Maximum,
stdevp(executionTime) as Deviation
from Timings
group by model, query
) ro
on
ro.model = rm.model
and
ro.query = rm.query
order by
1, 2;