Home
%3CLINGO-SUB%20id%3D%22lingo-sub-662878%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%2389%3A%20Splitting%20a%20partition%20of%20a%20partitioned%20table%20in%20Azure%20SQL%20Database%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-662878%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Team%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20we%20discussed%20in%20previous%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-Support-Blog%2FLesson-Learned-60-Using-Partition-Option-in-Azure-SQL-Managed%2Fba-p%2F369165%22%20target%3D%22_self%22%3Eposts%3C%2FA%3E%26nbsp%3Beither%20Azure%20SQL%20Database%20and%20Azure%20SQL%20Managed%20Instance%20we%20have%20the%20partitioning%20option%20in%20both%20databases.%20In%20Azure%20SQL%20Database%20there%20is%20not%20possible%20to%20specify%20the%20filegroup%20however%20Azure%20SQL%20Managed%20Instance%20is.%20Right%20now%2C%20we%20are%20going%20to%20show%20you%20an%20example%20to%20split%20a%20partition%20that%20our%20customer%20reached%20the%20maximum%20number%20of%20partitions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe're%20going%20to%20review%20the%20article%20posted%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Farchitecture%2Fbest-practices%2Fdata-partitioning%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Falter-partition-function-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%26nbsp%3B%3C%2FA%3Ewhere%20we%20have%20the%20TSQL%20commands%20syntax%20and%20best%20practices%26nbsp%3Bthat%20we%20need%20to%20review%20before%26nbsp%3Bgoing%20to%20split%20the%20partitioned%20table.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E--------------------------------------------------------%0A--%20First%20we%20are%20going%20to%20create%20the%20partition%0A--------------------------------------------------------%0ACREATE%20PARTITION%20FUNCTION%20PF_HASH_BY_VALUE%20(BIGINT)%20AS%20RANGE%20LEFT%20%0AFOR%20VALUES%20(100000%2C%20200000%2C%20300000%2C%20400000%2C%20500000%2C%20600000%2C%20700000%2C%20800000%2C%20900000)%0A%0A----------------------------------------------------------%0A--%20Second%20we%20create%20the%20partition%20scheme%0A----------------------------------------------------------%0ACREATE%20PARTITION%20SCHEME%20PS_HASH_BY_VALUE%20%0AAS%20PARTITION%20PF_HASH_BY_VALUE%0AALL%20TO%20(%5BPRIMARY%5D)%3B%0A%0A------------------------------------------------------------------------------------%0A--%20Third%2C%20we%20are%20going%20to%20create%20the%20table%20and%20insert%20some%20data%0A------------------------------------------------------------------------------------%0ACREATE%20TABLE%20%5BTBL_PARTITION%5D%20%0A(%20%5BMY_VALUE%5D%20%5Bbigint%5D%20NOT%20NULL%2C%0A%20%20CONSTRAINT%20%5BPK_TBL_PARTITION%5D%20PRIMARY%20KEY%20CLUSTERED%20(%5BMY_VALUE%5D%20ASC)%0A)%20ON%20PS_HASH_BY_VALUE%20(%5BMY_VALUE%5D)%0A%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(100001)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(200001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(300001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(400001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(500001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(600001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(700001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(800001)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(900001)%0A%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(100002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(200002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(300002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(400002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(500002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(600002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(700002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(800002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(900002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1000002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1100002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1200002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1300002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1400002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1500002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1600002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1700002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1800002)%20%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(1900002)%0Ainsert%20into%20%5BTBL_PARTITION%5D%20(my_value)%20values(2000002)%0A%0A%3C%2FPRE%3E%0A%3CP%3ERight%20now%2C%20after%20running%20the%20previous%20process%2C%20running%20the%20next%20one%20we%20found%20the%20following%20situation%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20%0A%20%20MY_VALUE%2C%0A%20%20%24PARTITION.PF_HASH_BY_VALUE(MY_VALUE)%20AS%20HASH_IDX%0AFROM%20%0A(%20SELECT%20MY_VALUE%20FROM%20%5BTBL_PARTITION%5D%20)%20%20AS%20TEST%20(MY_VALUE)%3B%0A%3C%2FPRE%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20245px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116872i1B206954C9A68D47%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Capture100.PNG%22%20title%3D%22Capture100.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIn%20fact%2C%20we%20could%20find%20the%20distribution%20of%20the%20data%20is%20not%20correct%20if%20we%20execute%20the%20following%20TSQL%3A%3C%2FP%3E%0A%3CPRE%3ESELECT%20object_name(object_id)%2C*%20FROM%20sys.dm_db_partition_stats%20where%20object_name(object_id)%3D'TBL_PARTITION'%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116873iF1FF113E7F5BA6AA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Capture200.PNG%22%20title%3D%22Capture200.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20situation%2C%20basically%2C%20we%20need%20to%20split%20the%20data%20to%20the%20next%20value%20and%20add%20a%20new%20partition%20schema.%3C%2FP%3E%0A%3CPRE%3EALTER%20PARTITION%20FUNCTION%20PF_HASH_BY_VALUE%20()%0ASPLIT%20RANGE%20(1100000)%0A%0AALTER%20PARTITION%20SCHEME%20PS_HASH_BY_VALUE%20%0ANEXT%20USED%20%5BPrimary%5D%3C%2FPRE%3E%0A%3CP%3ENow%2C%20it%20is%20time%20to%20review%20what%20happened%20after%20the%20execution%20of%20splitting%20process%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20%0A%20%20MY_VALUE%2C%0A%20%20%24PARTITION.PF_HASH_BY_VALUE(MY_VALUE)%20AS%20HASH_IDX%0AFROM%20%0A(%20SELECT%20MY_VALUE%20FROM%20%5BTBL_PARTITION%5D%20)%20%20AS%20TEST%20(MY_VALUE)%3B%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20237px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116874iC5943518D0788BC8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Capture300.PNG%22%20title%3D%22Capture300.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-662878%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20we%20discussed%20in%20previous%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-Support-Blog%2FLesson-Learned-60-Using-Partition-Option-in-Azure-SQL-Managed%2Fba-p%2F369165%22%20target%3D%22_self%22%3Eposts%3C%2FA%3E%26nbsp%3Beither%20Azure%20SQL%20Database%20and%20Azure%20SQL%20Managed%20Instance%20we%20have%20the%20partitioning%20option%20in%20both%20databases.%20In%20Azure%20SQL%20Database%20there%20is%20not%20possible%20to%20specify%20the%20filegroup%20however%20Azure%20SQL%20Managed%20Instance%20is.%20Right%20now%2C%20we%20are%20going%20to%20show%20you%20an%20example%20to%20split%20a%20partition%20that%20our%20customer%20reached%20the%20maximum%20number%20of%20partitions.%3C%2FP%3E%3C%2FLINGO-TEASER%3E

Hello Team,

 

As we discussed in previous posts either Azure SQL Database and Azure SQL Managed Instance we have the partitioning option in both databases. In Azure SQL Database there is not possible to specify the filegroup however Azure SQL Managed Instance is. Right now, we are going to show you an example to split a partition that our customer reached the maximum number of partitions.

 

We're going to review the article posted here and here where we have the TSQL commands syntax and best practices that we need to review before going to split the partitioned table.

 

--------------------------------------------------------
-- First we are going to create the partition
--------------------------------------------------------
CREATE PARTITION FUNCTION PF_HASH_BY_VALUE (BIGINT) AS RANGE LEFT 
FOR VALUES (100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000)

----------------------------------------------------------
-- Second we create the partition scheme
----------------------------------------------------------
CREATE PARTITION SCHEME PS_HASH_BY_VALUE 
AS PARTITION PF_HASH_BY_VALUE
ALL TO ([PRIMARY]);

------------------------------------------------------------------------------------
-- Third, we are going to create the table and insert some data
------------------------------------------------------------------------------------
CREATE TABLE [TBL_PARTITION] 
( [MY_VALUE] [bigint] NOT NULL,
  CONSTRAINT [PK_TBL_PARTITION] PRIMARY KEY CLUSTERED ([MY_VALUE] ASC)
) ON PS_HASH_BY_VALUE ([MY_VALUE])

insert into [TBL_PARTITION] (my_value) values(100001)
insert into [TBL_PARTITION] (my_value) values(200001) 
insert into [TBL_PARTITION] (my_value) values(300001) 
insert into [TBL_PARTITION] (my_value) values(400001) 
insert into [TBL_PARTITION] (my_value) values(500001) 
insert into [TBL_PARTITION] (my_value) values(600001) 
insert into [TBL_PARTITION] (my_value) values(700001) 
insert into [TBL_PARTITION] (my_value) values(800001) 
insert into [TBL_PARTITION] (my_value) values(900001)

insert into [TBL_PARTITION] (my_value) values(100002)
insert into [TBL_PARTITION] (my_value) values(200002) 
insert into [TBL_PARTITION] (my_value) values(300002) 
insert into [TBL_PARTITION] (my_value) values(400002) 
insert into [TBL_PARTITION] (my_value) values(500002) 
insert into [TBL_PARTITION] (my_value) values(600002) 
insert into [TBL_PARTITION] (my_value) values(700002) 
insert into [TBL_PARTITION] (my_value) values(800002) 
insert into [TBL_PARTITION] (my_value) values(900002)
insert into [TBL_PARTITION] (my_value) values(1000002) 
insert into [TBL_PARTITION] (my_value) values(1100002)
insert into [TBL_PARTITION] (my_value) values(1200002) 
insert into [TBL_PARTITION] (my_value) values(1300002)
insert into [TBL_PARTITION] (my_value) values(1400002) 
insert into [TBL_PARTITION] (my_value) values(1500002)
insert into [TBL_PARTITION] (my_value) values(1600002) 
insert into [TBL_PARTITION] (my_value) values(1700002)
insert into [TBL_PARTITION] (my_value) values(1800002) 
insert into [TBL_PARTITION] (my_value) values(1900002)
insert into [TBL_PARTITION] (my_value) values(2000002)

Right now, after running the previous process, running the next one we found the following situation:

 

SELECT 
  MY_VALUE,
  $PARTITION.PF_HASH_BY_VALUE(MY_VALUE) AS HASH_IDX
FROM 
( SELECT MY_VALUE FROM [TBL_PARTITION] )  AS TEST (MY_VALUE);

Capture100.PNG

In fact, we could find the distribution of the data is not correct if we execute the following TSQL:

SELECT object_name(object_id),* FROM sys.dm_db_partition_stats where object_name(object_id)='TBL_PARTITION'

Capture200.PNG

 

In this situation, basically, we need to split the data to the next value and add a new partition schema.

ALTER PARTITION FUNCTION PF_HASH_BY_VALUE ()
SPLIT RANGE (1100000)

ALTER PARTITION SCHEME PS_HASH_BY_VALUE 
NEXT USED [Primary]

Now, it is time to review what happened after the execution of splitting process:

 

SELECT 
  MY_VALUE,
  $PARTITION.PF_HASH_BY_VALUE(MY_VALUE) AS HASH_IDX
FROM 
( SELECT MY_VALUE FROM [TBL_PARTITION] )  AS TEST (MY_VALUE);

Capture300.PNG

 

Enjoy!