@@ -139,20 +139,23 @@ select * from clustered_table where date between '2019-07-18 17:04:01.000' and '
139
139
select * from partitioned_table where date between ' 2019-12-12 03:14:01.000' and ' 2020-09-12 03:14:01.000'
140
140
select * from filepartition_table where date between ' 2019-12-12 03:14:01.000' and ' 2020-09-12 03:14:01.000'
141
141
142
+ -- whats happening under the hood
143
+ select * from sys .partitions where object_id = object_id (' flat_table' )
144
+ select * from sys .partitions where object_id = object_id (' clustered_table' )
145
+ select * from sys .partitions where object_id = object_id (' partitioned_table' )
146
+ select * from sys .partitions where object_id = object_id (' filepartition_table' )
147
+
142
148
-- Partition Switching examples
143
149
144
150
-- Create table switch existing partition to
151
+ drop TABLE [dbo].[holding_table];
145
152
CREATE TABLE [dbo].[holding_table](
146
153
[date] [datetime] PRIMARY KEY ,
147
154
[number] [int] NULL
148
155
) on [fg_years2017]
149
156
GO
150
157
151
158
152
- insert into incoming_table (date , number ) select dateadd (minute,number , ' 2017-03-01 00:00:01' ), ' 5' from numbers where number < 1000 ;
153
-
154
- select * from incoming_table;
155
-
156
159
-- switch out current partition
157
160
select * from holding_table;
158
161
alter table filepartition_table switch partition 4 to holding_table;
@@ -169,14 +172,15 @@ alter table holding_table switch to filepartition_table partition 4
169
172
select count (1 ) from filepartition_table where Date between ' 2017-01-01T00:00:00.000' and ' 2020-01-01T00:00:00.000'
170
173
171
174
-- Create table to switch in to filepartition_table
172
- drop table incoming_table
175
+ drop table [dbo].[ incoming_table];
173
176
CREATE TABLE [dbo].[incoming_table](
174
177
[date] [datetime] PRIMARY KEY ,
175
178
[number] [int] NULL ,
176
179
CONSTRAINT incomingdate CHECK (date >= ' 2017-01-01T00:00:00.000' and date < ' 2020-01-01T00:00:00.000' )
177
180
) on [fg_years2017]
178
181
GO
179
182
183
+
180
184
-- create a back up of the original partition
181
185
drop table holding_table;
182
186
CREATE TABLE [dbo].[holding_table](
190
194
insert into incoming_table (date , number ) select dateadd (second,number , ' 2017-01-01 00:00:01' ), number from numbers where number < 31536000 ;
191
195
192
196
-- take a backup of the incoming data
197
+ select count (1 ) from holding_table;
193
198
alter table filepartition_table switch partition 4 to holding_table;
194
- select * from holding_table;
199
+ select count ( 1 ) from holding_table;
195
200
-- switch in new data
196
201
alter table incoming_table switch to filepartition_table partition 4 ;
197
202
select * from filepartition_table where date between ' 2017-01-01 00:00:01' and ' 2017-01-01 00:05:01'
198
203
-- oops, wrong data, swap back over
199
204
alter table filepartition_table switch partition 4 to incoming_table;
200
205
alter table holding_table switch to filepartition_table partition 4 ;
201
-
206
+ select count (1 ) from incoming_table;
207
+ select count (1 ) from holding_table;
208
+ select * from filepartition_table where date between ' 2017-01-01 00:00:01' and ' 2017-01-01 00:05:01'
202
209
203
210
-- you can see what is in each partition directly if needed. This is handy if you're not 100% which partition holds what datae
204
211
select min (date ), max (date ) from filepartition_table where $PARTITION .filePartitionFunc(date )= 0
@@ -223,3 +230,4 @@ ORDER BY Partition ;
223
230
GO
224
231
225
232
233
+
0 commit comments