I have an azure sql db with a partitioned table with a partition function on varchar datatype.
In an Etl process I will get a list of partiton values as a parameter that i will the need to:
-
check partition function if the partition values exist, if not iterate through list of value and add to partition function.
-
for the list of partition values that is supplied as a parameter, iterate through the $partition function to get the partitionids into a new variable as a comma seperated list
-
truncate the partitioned table with the command: truncate parttable with partitions (@newvariable)
What I got so far in code is:
-- Table, Partition Function, Partition Scheme
CREATE PARTITION FUNCTION PF_TEXT(varchar(20)) AS RANGE RIGHT FOR VALUES ('A', 'B', 'C', 'D', 'E')
CREATE PARTITION SCHEME PS_TEXT AS PARTITION PF_TEXT ALL TO ([PRIMARY])
CREATE TABLE [dbo].[fact_partitioned](
[Col1] [int], NOT NULL
[Col2] [int], NOT NULL
[TEXT] [varchar(20)] NOT NULL
) ON PS_TEXT ([TEXT])
---------------------------
DECLARE @cmd nvarchar(max);
DECLARE @partitions nvarchar(max);
DECLARE @partitionids nvarchar(max);
SET @partitions = 'A','B','C'
-- Code to check Partition Function for @partitions values exist?
-- For @partitions values that don't exist, iterate @partitions values
and add to partition function.
--code to iterate @partitions values over $partition.PF_TEXT(@partition)
add result to @partitionids
SET @cmd = N'TRUNCATE TABLE dbo.pt WITH (PARTITIONS (' + @partitionids + N'));';
PRINT @cmd;
--EXEC sys.sp_executesql @cmd;
Best Answer
SQL Server table partitioning is natively range partitioning. Although you can implement list partitioning (i.e. partitioning columns exactly match boundaries), take care to ensure exact boundary matches to avoid excessive data movement and logging during
SPLIT
operations. Consider creating aCHECK
constraint on the fact table matching the boundary values (e.g.TEXT IN('A','B','C', …
).Below is an example script based on the DDL and sample data in your question, which assumes (at least) a clustered index exists on the fact table. I added some additional boundaries to illustrate new boundaries between existing ones can be introduced during the ETL process that without requiring data movement, assuming partitioning column values match boundaries exactly. See inline comments for details of each step.
The move from staging to the fact table took a couple of seconds in my test. Be sure to update fact table stats afterward.