Home
%3CLINGO-SUB%20id%3D%22lingo-sub-740783%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%2396%3A%20Updating%20a%20non-clustered%20index%20in%20a%20partitioned%20table.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-740783%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%26nbsp%3Bworked%20on%20a%20service%20request%20that%20our%20customer%20has%20implemented%20a%20process%20to%20rebuild%20the%20indexes%26nbsp%3Band%20after%20it%20they%20update%20the%20statistics.%20In%20this%20rebuild%20process%20I%20found%20that%20they%20have%20some%20partitioned%20table%20but%20they%20are%20rebuild%20the%20entire%20table%20instead%20of%20the%20latest%20partition.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBased%20on%20this%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Falter-index-transact-sql%3Fview%3Dsql-server-2017%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EURL%3C%2FA%3E%20our%20customer%20is%20able%20using%20the%20parameter%20PARTITION%3Dn%2C%20reducing%20considerably%20the%20time%20spent%20in%20this%20rebuild%20process.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20they%20asked%20a%20good%20question%20about%2C%20what%20happen%20with%20the%20other%20indexes%20that%20we%20have%20in%20this%20partitioned%20table%2C%20is%20it%20posible%20to%20use%20PARTITION%3Dn%20for%20them%2C%20and%20the%20answer%20is%20yes%2C%20let%20me%20explain%20with%20an%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ECREATE%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%0ACREATE%20PARTITION%20SCHEME%20PS_HASH_BY_VALUE%20%0AAS%20PARTITION%20PF_HASH_BY_VALUE%0AALL%20TO%20(%5BPRIMARY%5D)%3B%0AGO%0A%0ACREATE%20TABLE%20%5BTBL_PARTITION%5D%20%0A(%20%5BMY_VALUE%5D%20%5Bbigint%5D%20NOT%20NULL%2C%0A%20%20%5BAGE%5D%20%5Bint%5D%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)%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20created%20an%20index%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ECREATE%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EINDEX%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%20TBL_PARTITION_IX1%20%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EON%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%20TBL_PARTITION%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E(%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3EAGE%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E)%20%3C%2FFONT%3Eand%20after%20it%2C%20executing%20this%20command%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3ESELECT%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff00ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Eobject_name%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff00ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Eobject_id%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E)%2C*%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EFROM%3C%2FFONT%3E%20%3CFONT%20color%3D%22%2300ff00%22%20face%3D%22Consolas%22%20size%3D%222%22%3Esys%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E.%3C%2FFONT%3E%3CFONT%20color%3D%22%2300ff00%22%20face%3D%22Consolas%22%20size%3D%222%22%3Edm_db_partition_stats%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Ewhere%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff00ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Eobject_name%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff00ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3Eobject_id%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E)%3D%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff0000%22%20face%3D%22Consolas%22%20size%3D%222%22%3E'TBL_PARTITION'%3C%2FFONT%3E%20you%20could%20see%20that%20the%20new%20index%20create%20will%20have%20the%20same%20partition%20definition%2C%20so%2C%20basically%2C%20I%20need%20to%20execute%20this%20command%20to%20rebuilt%20just%20only%20this%20index%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EALTER%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EINDEX%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%20%5BPK_TBL_PARTITION%5D%20%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EON%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%20TBL_PARTITION%20%3C%2FFONT%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EREBUILD%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Consolas%22%20size%3D%222%22%3EPARTITION%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E%3D%3C%2FFONT%3E%3CFONT%20face%3D%22Consolas%22%20size%3D%222%22%3E%2010%3C%2FFONT%3E%3CFONT%20color%3D%22%23808080%22%20face%3D%22Consolas%22%20size%3D%222%22%3E%3B%3C%2FFONT%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%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-740783%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%26nbsp%3Bworked%20on%20a%20service%20request%20that%20our%20customer%20has%20implemented%20a%20process%20to%20rebuild%20the%20indexes%26nbsp%3Band%20after%20it%20they%20update%20the%20statistics.%20In%20this%20rebuild%20process%20I%20found%20that%20they%20have%20some%20partitioned%20table%20but%20they%20are%20rebuild%20the%20entire%20table%20instead%20of%20the%20latest%20partition.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-741439%22%20slang%3D%22en-US%22%3ERe%3A%20Lesson%20Learned%20%2396%3A%20Updating%20a%20non-clustered%20index%20in%20a%20partitioned%20table.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-741439%22%20slang%3D%22en-US%22%3E%3CP%3Ethanks%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E

Today, I worked on a service request that our customer has implemented a process to rebuild the indexes and after it they update the statistics. In this rebuild process I found that they have some partitioned table but they are rebuild the entire table instead of the latest partition.

 

Based on this URL our customer is able using the parameter PARTITION=n, reducing considerably the time spent in this rebuild process.

 

Also, they asked a good question about, what happen with the other indexes that we have in this partitioned table, is it posible to use PARTITION=n for them, and the answer is yes, let me explain with an example:

 

CREATE PARTITION FUNCTION PF_HASH_BY_VALUE (BIGINT) AS RANGE LEFT 
FOR VALUES (100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000)

CREATE PARTITION SCHEME PS_HASH_BY_VALUE 
AS PARTITION PF_HASH_BY_VALUE
ALL TO ([PRIMARY]);
GO

CREATE TABLE [TBL_PARTITION] 
( [MY_VALUE] [bigint] NOT NULL,
  [AGE] [int],
  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)

 

 

I created an index CREATE INDEX TBL_PARTITION_IX1 ON TBL_PARTITION (AGE) and after it, executing this command SELECT object_name(object_id),* FROM sys.dm_db_partition_stats where object_name(object_id)='TBL_PARTITION' you could see that the new index create will have the same partition definition, so, basically, I need to execute this command to rebuilt just only this index

ALTER INDEX [PK_TBL_PARTITION] ON TBL_PARTITION REBUILD PARTITION = 10;

 

Enjoy!

 

1 Comment
New Contributor

thanks