自動(dòng)管理分區(qū)

表分區(qū)的一個(gè)好處:能夠避免Deadlock,分區(qū)之間是相互獨(dú)立的,對(duì)一個(gè)分區(qū)加X(jué)鎖,不會(huì)對(duì)其他分區(qū)產(chǎn)生contention。

創(chuàng)新互聯(lián)主營(yíng)鄱陽(yáng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開發(fā),鄱陽(yáng)h5小程序制作搭建,鄱陽(yáng)網(wǎng)站營(yíng)銷推廣歡迎鄱陽(yáng)等地區(qū)企業(yè)咨詢

在項(xiàng)目中,有如下 Partition Function 和 Partition Scheme

自動(dòng)管理分區(qū)

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)

自動(dòng)管理分區(qū)

查看ETL的execution log,有時(shí)會(huì)發(fā)現(xiàn) Deadlock Issue,對(duì)相關(guān)package Troubleshooting發(fā)現(xiàn),發(fā)生deadlock的root cause是:同時(shí)更新表的兩條語(yǔ)句產(chǎn)生contention,導(dǎo)致deadlock。仔細(xì)check代碼,更新的兩條查詢語(yǔ)句都使用Partition Column(DataSourceID) 作為過(guò)濾條件。我推測(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。

自動(dòng)管理分區(qū)

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

自動(dòng)管理分區(qū)

本例將分區(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)

  1. 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.

  2. SET QUOTED_IDENTIFIER must be ON when you are creating a filtered index.

  3. SET QUOTED_IDENTIFIER must be ON when you invoke XML data type methods.

分享題目:自動(dòng)管理分區(qū)
瀏覽路徑:http://bm7419.com/article40/iihhho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)公司、企業(yè)建站、云服務(wù)器網(wǎng)站導(dǎo)航、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管