您好,登錄后才能下訂單哦!
表分區的一個好處:能夠避免Deadlock,分區之間是相互獨立的,對一個分區加X鎖,不會對其他分區產生contention。
在項目中,有如下 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,有時會發現 Deadlock Issue,對相關package Troubleshooting發現,發生deadlock的root cause是:同時更新表的兩條語句產生contention,導致deadlock。仔細check代碼,更新的兩條查詢語句都使用Partition Column(DataSourceID) 作為過濾條件。我推測,可能是這兩個DataSourceID位于同一個Partition。
1,驗證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的值小于當前 DataSourceID的最大值,產生 contention的兩個DataSourceID 在最右邊的partition中。
隨著項目數據的增加和人員的更替,缺少合理的管理計劃,導致額外增加的DataSourceID都被分配到同一個partition中。
2,創建Job,自動分區
最佳實踐,如果一個partition是non-empty,那么split range會導致data movement,這可能是一個非常耗費IO的一個process,為了避免extensive data movement,最好是預留一個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
本例將分區全部存放在Primary FileGroup, 如果需要將不同的Partition存儲在不同的FileGroup,那么可以增加Create filegroup的代碼。
3,在Job執行時,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設置錯誤,添加下面的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.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。