Skip to content

Commit fd961d8

Browse files
committed
changes for East midlands data group 09/05/2024
1 parent 4278a8b commit fd961d8

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

.DS_Store

8 KB
Binary file not shown.

DbaFundamentalsPartitioning/PartitioningSession.sql

+15-7
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,23 @@ select * from clustered_table where date between '2019-07-18 17:04:01.000' and '
139139
select * from partitioned_table where date between '2019-12-12 03:14:01.000' and '2020-09-12 03:14:01.000'
140140
select * from filepartition_table where date between '2019-12-12 03:14:01.000' and '2020-09-12 03:14:01.000'
141141

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+
142148
--Partition Switching examples
143149

144150
--Create table switch existing partition to
151+
drop TABLE [dbo].[holding_table];
145152
CREATE TABLE [dbo].[holding_table](
146153
[date] [datetime] PRIMARY KEY,
147154
[number] [int] NULL
148155
) on [fg_years2017]
149156
GO
150157

151158

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-
156159
--switch out current partition
157160
select * from holding_table;
158161
alter table filepartition_table switch partition 4 to holding_table;
@@ -169,14 +172,15 @@ alter table holding_table switch to filepartition_table partition 4
169172
select count(1) from filepartition_table where Date between '2017-01-01T00:00:00.000' and '2020-01-01T00:00:00.000'
170173

171174
--Create table to switch in to filepartition_table
172-
drop table incoming_table
175+
drop table [dbo].[incoming_table];
173176
CREATE TABLE [dbo].[incoming_table](
174177
[date] [datetime] PRIMARY KEY,
175178
[number] [int] NULL,
176179
CONSTRAINT incomingdate CHECK (date>='2017-01-01T00:00:00.000' and date<'2020-01-01T00:00:00.000')
177180
) on [fg_years2017]
178181
GO
179182

183+
180184
--create a back up of the original partition
181185
drop table holding_table;
182186
CREATE TABLE [dbo].[holding_table](
@@ -190,15 +194,18 @@ GO
190194
insert into incoming_table (date, number) select dateadd(second,number, '2017-01-01 00:00:01'), number from numbers where number<31536000;
191195

192196
--take a backup of the incoming data
197+
select count(1) from holding_table;
193198
alter table filepartition_table switch partition 4 to holding_table;
194-
select * from holding_table;
199+
select count(1) from holding_table;
195200
--switch in new data
196201
alter table incoming_table switch to filepartition_table partition 4 ;
197202
select * from filepartition_table where date between '2017-01-01 00:00:01' and '2017-01-01 00:05:01'
198203
--oops, wrong data, swap back over
199204
alter table filepartition_table switch partition 4 to incoming_table;
200205
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'
202209

203210
--you can see what is in each partition directly if needed. This is handy if you're not 100% which partition holds what datae
204211
select min(date), max(date) from filepartition_table where $PARTITION.filePartitionFunc(date)=0
@@ -223,3 +230,4 @@ ORDER BY Partition ;
223230
GO
224231

225232

233+
279 KB
Binary file not shown.

0 commit comments

Comments
 (0)