您好,登錄后才能下訂單哦!
表分區(qū)的一個(gè)好處:能夠避免Deadlock,分區(qū)之間是相互獨(dú)立的,對(duì)一個(gè)分區(qū)加X鎖,不會(huì)對(duì)其他分區(qū)產(chǎn)生contention。
在項(xiàng)目中,有如下 Partition Function 和 Partition Scheme
CREATE PARTITION FUNCTION [funcPartition_int_DataSourceID](int) AS RANGE LEFT FOR VALUES (1, 2, 3)CREATE PARTITION SCHEME [schePartition_int_DataSourceID] AS PARTITION [funcPartition_DataSourceID] TO ([PRIMARY], [PRIMARY], [PRIMARY], [PRIMARY])create table dbo.dt_test ( ...More column definition DataSourceID int)on [schePartition_int_DataSourceID](DataSourceID)
查看ETL的execution log,有時(shí)會(huì)發(fā)現(xiàn) Deadlock Issue,對(duì)相關(guān)package Troubleshooting發(fā)現(xiàn),發(fā)生deadlock的root cause是:同時(shí)更新表的兩條語句產(chǎn)生contention,導(dǎo)致deadlock。仔細(xì)check代碼,更新的兩條查詢語句都使用Partition Column(DataSourceID) 作為過濾條件。我推測(cè),可能是這兩個(gè)DataSourceID位于同一個(gè)Partition。
1,驗(yàn)證boundary value
select prv.function_id,pf.name,pf.boundary_value_on_right,prv.value as BoundaryValuefrom sys.partition_range_values prvinner join sys.partition_functions pf on prv.function_id=pf.function_idwhere pf.name='funcPartition_int_DataSourceID'
BoundaryValue的值小于當(dāng)前 DataSourceID的最大值,產(chǎn)生 contention的兩個(gè)DataSourceID 在最右邊的partition中。
隨著項(xiàng)目數(shù)據(jù)的增加和人員的更替,缺少合理的管理計(jì)劃,導(dǎo)致額外增加的DataSourceID都被分配到同一個(gè)partition中。
2,創(chuàng)建Job,自動(dòng)分區(qū)
最佳實(shí)踐,如果一個(gè)partition是non-empty,那么split range會(huì)導(dǎo)致data movement,這可能是一個(gè)非常耗費(fèi)IO的一個(gè)process,為了避免extensive data movement,最好是預(yù)留一個(gè)empty partition,每次都從empty partition 中split range。
use db_studygodeclare @CurrentMaxBoundaryValue intdeclare @ExistingMaxDataSourceID intdeclare @BoudaryValue intselect @ExistingMaxDataSourceID = max(dds.DataSourceID)from dbo.dt_DataSource dds with(nolock)select @CurrentMaxBoundaryValue= max(cast(prv.value as int))from sys.partition_functions pf inner join sys.partition_range_values prv on pf.function_id=prv.function_idwhere pf.name='funcPartition_int_DataSourceID'-- add new boundary valueif @CurrentMaxBoundaryValue<@ExistingMaxDataSourceID+1begin set @BoudaryValue=@CurrentMaxBoundaryValue+1 DECLARE @SQL NVARCHAR(MAX)=N'ALTER PARTITION SCHEME [schePartition_int_DataSourceID] NEXT USED [PRIMARY] ALTER PARTITION FUNCTION [funcPartition_int_DataSourceID]() SPLIT RANGE (' declare @ExecSql nvarchar(max) set @ExecSql='' while @BoudaryValue<=@ExistingMaxDataSourceID+1 BEGIN SELECT @ExecSql = @SQL+ cast(@BoudaryValue as varchar(10))+ N')' EXEC(@ExecSql) set @BoudaryValue=@BoudaryValue+1 endend
本例將分區(qū)全部存放在Primary FileGroup, 如果需要將不同的Partition存儲(chǔ)在不同的FileGroup,那么可以增加Create filegroup的代碼。
3,在Job執(zhí)行時(shí),Issue an error
Executed as user: NT SERVICE\SQLSERVERAGENT. UNKNOWN TOKEN failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations. [SQLSTATE 42000] (Error 1934). The step failed.
QUOTED_IDENTIFIER設(shè)置錯(cuò)誤,添加下面的script,即可
SET QUOTED_IDENTIFIER ON
參考:SET QUOTED_IDENTIFIER (Transact-SQL)
SET QUOTED_IDENTIFIER must be ON when you are creating or changing indexes on computed columns or indexed views. If SET QUOTED_IDENTIFIER is OFF, CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail.
SET QUOTED_IDENTIFIER must be ON when you are creating a filtered index.
SET QUOTED_IDENTIFIER must be ON when you invoke XML data type methods.
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。