-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00B Temporal Optional.sql
executable file
·263 lines (237 loc) · 6.62 KB
/
00B Temporal Optional.sql
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
-------------------------------------------------------------------------------------------------------------
--------------------------------------------- PARTITION SCHEME ----------------------------------------------
drop table if exists Fact_Optional;
-------------------------------------------------------------------------------------------------------------
-------------------------------------------- TEMPORAL DIMENSION ---------------------------------------------
create table Fact_Optional (
dim_ID int not null,
factDate smalldatetime not null,
dim_ValidFrom 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 FACTS --------------------------------------------
-- ~ 3 minutes loading time
-------------------------------------------------------------------------------------------------------------
-- truncate table Fact_Optional;
drop table if exists #lookup;
select
f.dim_ID,
f.Timepoint as factDate,
f.dim_ValidFrom
into
#lookup
from (
select
twine.*,
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
union all
select
dim_ID,
dim_ValidFrom as Timepoint,
'D' as Timeline
from
dbo.Dimension_Mutable
) twine
) f
where
f.Timeline = 'F';
-- select top 100 * from #lookup;
-- select count(*) from #lookup;
insert into Fact_Optional (dim_ID, factDate, dim_ValidFrom, factMeasure)
select
d.dim_ID,
f.factDate,
d.dim_ValidFrom,
f.factMeasure
from
#lookup l
join
Fact f
on
f.dim_ID = l.dim_ID
and
f.factDate = l.factDate
join
Dimension_Mutable d
on
d.dim_ID = l.dim_ID
and
d.dim_ValidFrom = l.dim_ValidFrom;
-------------------------------------------------------------------------------------------------------------
------------------------------------ REDUCE INDEX FRAGMENTATION ---------------------------------------------
ALTER INDEX ALL ON Fact_Optional REBUILD;
-------------------------------------------------------------------------------------------------------------
-------------------------------------------- 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) = 'Optional';
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
pit.dimProperty,
f.numberOfFacts,
f.avgMeasure
into
#result_tiy
from (
select
dim_Id,
count(*) as numberOfFacts,
avg(factMeasure) as avgMeasure
from
Fact_Optional
where
factDate between '2014-01-01' and '2014-12-31'
group by
dim_ID
) f
join
pitDimension('2018-01-01') pit
on
pit.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
pit.dimProperty,
f.numberOfFacts,
f.avgMeasure
into
#result_yit
from (
select
dim_Id,
count(*) as numberOfFacts,
avg(factMeasure) as avgMeasure
from
Fact_Optional
where
factDate between '2014-01-01' and '2014-12-31'
group by
dim_ID
) f
join
pitDimension('2014-01-01') pit
on
pit.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 Fact_Optional f
join Dimension_Mutable dm
on dm.dim_ID = f.dim_ID
and dm.dim_ValidFrom = f.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_Optional 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;