sql2008 計劃自動建立資料庫分區【轉】

來源:互聯網
上載者:User

標籤:

本文轉自:http://jingyan.baidu.com/article/6b97984d9a26ec1ca3b0bf77.html

sql2008 計劃自動建立資料庫分區

固定增量的資料,自動建立分區作業.

步驟一:建立分區的計劃任務
  1.  

    開啟MsSQL2008,找到作業該項,如果打不開或者SQL Server代理是未啟動狀態,請先在windows服務中啟動SQL Server代理(參考圖片),

  2.  

    右擊MsSQL2008物件總管中的作業,選擇新增作業,輸入該作業你想用的名稱,類別不用管,說明裡面是輸入一些該作業完成的功能,可不寫,請務必勾選已啟用複選框.

  3.  

    點擊新增作業表單左側的步驟項,點擊右側地區下方的建立按鈕,輸入步驟名稱,類型請選擇Transact-SQL指令碼(T-SQL),運行身份預設,資料庫請選擇要進行分區的資料庫,請不要選擇master預設的,命令文字框中輸入如下代碼:

     

    /*--------------------建立資料庫的檔案組和物理檔案------------------------*/

    declare @tableName varchar(50), @fileGroupName varchar(50), @ndfName varchar(50), @newNameStr varchar(50), @fullPath 

    varchar(50), @newDay varchar(50), @oldDay datetime, @partFunName varchar(50), @schemeName varchar(50)
    set @tableName=‘WaterNet_DaFeng‘
    set @newDay=CONVERT(varchar(100), GETDATE(), 23)--23:按天 114:按時間
    set @oldDay=cast(CONVERT(varchar(10),dateadd(day,-1,getdate()), 120 ) as datetime)
    set @newNameStr=Replace(Replace(@newDay,‘:‘,‘_‘),‘-‘,‘_‘)
    set @fileGroupName=N‘G‘[email protected]
    set @ndfName=N‘F‘[email protected]+‘‘
    set @fullPath=N‘D:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\‘[email protected]+‘.ndf‘

                                        --此處該為自己的資料檔案路徑,lui注釋2015-5-4(右擊伺服器-屬性-資料庫設定可看到)
    set @partFunName=N‘pf_Time‘
    set @schemeName=N‘ps_Time‘
    --建立檔案組
    if exists(select * from sys.filegroups where [email protected])
    begin
    print ‘檔案組存在,不需添加‘
    end
    else
    begin
    exec(‘ALTER DATABASE ‘[email protected]+‘ ADD FILEGROUP [‘[email protected]+‘]‘)
    print ‘新增檔案組‘
    if exists(select * from sys.partition_schemes where name [email protected])
    begin
    exec(‘alter partition scheme ‘[email protected]+‘ next used [‘[email protected]+‘]‘)
    print ‘修改資料分割配置‘
    end
    if exists(select * from sys.partition_range_values where function_id=(select function_id from
    sys.partition_functions where name [email protected]) and [email protected])
    begin
    exec(‘alter partition function ‘[email protected]+‘() split range(‘‘‘[email protected]+‘‘‘)‘)
    print ‘修改分區函數‘
    end
    end
    --建立NDF檔案
    if exists(select * from sys.database_files where [state]=0 and ([email protected] or [email protected]))
    begin
    print ‘ndf檔案存在,不需添加‘
    end
    else
    begin
    exec(‘ALTER DATABASE ‘[email protected]+‘ ADD FILE (NAME =‘[email protected]+‘,FILENAME = ‘‘‘[email protected]+‘‘‘)TO FILEGROUP [‘[email protected]+‘]‘)
    print ‘新建立ndf檔案‘
    end
    /*--------------------以上建立資料庫的檔案組和物理檔案------------------------*/
    --分區函數
    if exists(select * from sys.partition_functions where name [email protected])
    begin
    print ‘此處修改需要在修改分區函數之前執行‘
    end
    else
    begin
    exec(‘CREATE PARTITION FUNCTION ‘[email protected]+‘(DateTime)AS RANGE RIGHT FOR VALUES (‘‘‘[email protected]
    +‘‘‘)‘)
    print ‘新建立分區函數‘
    end
    --資料分割配置
    if exists(select * from sys.partition_schemes where name [email protected])
    begin
    print ‘此處修改需要在修改資料分割配置之前執行‘
    end
    else
    begin
    exec(‘CREATE PARTITION SCHEME ‘[email protected]+‘ AS PARTITION ‘[email protected]+‘ TO
    (‘‘PRIMARY‘‘,‘‘‘[email protected]+‘‘‘)‘)
    print ‘新建立資料分割配置‘
    end
    print ‘---------------以下是變數定義值顯示---------------------‘
    print ‘當前資料庫:‘[email protected]
    print ‘當前日期:‘[email protected]+‘(用作隨機產生的各種名稱和分區界限)‘
    print ‘合法命名方式:‘[email protected]
    print ‘檔案組名稱:‘[email protected]
    print ‘ndf物理檔案名稱:‘[email protected]
    print ‘物理檔案完整路徑:‘[email protected]
    print ‘分區函數:‘[email protected]
    print ‘資料分割配置:‘[email protected]
    /*
    --查看建立的分區函數
    select * from sys.partition_functions
    --查看分區函數的臨界值
    select * from sys.partition_range_values
    --查詢資料分割配置
    select * from sys.partition_schemes
    --查詢表資料在哪個分區中儲存,where條件查詢第一個分區中存在的資料
    select *,$partition.pf_SaveTime(分區欄位) as Patition from 表名 where $partition.pf_SaveTime(分區欄位)=1
    */

    GO

    點擊確定按鈕

     

    上述代碼中的變數名稱,路徑等均可自行修改,上述是按天為單位,以G開頭的日期作為檔案組名稱,以F開頭的日期作為物理分區檔案名稱即ndf檔案名稱

  4.  

    選擇建立分區左側的計劃項,然後點擊右側地區下方的建立按鈕,設定建立分區的時間間隔,圖中設定的是每天建立一個新的分區,使用者也可以自行修改,按月,按周,按自訂時間等

    其他的條目,通知,警報,目標可自行設定,也可不設定,至此自動建立分區的計劃任務已成功設定.

    END
步驟二:對錶應用資料分割配置和分區函數
  1.  

    右擊要分區的表,選擇儲存菜單下的建立分區,上述步驟一中建立的分區函數是按datetime類型進行的分區,所以建立分區的時候需要選擇相應類型的欄位作為分區依據,使用者也可以根據int型或其他類型的欄位進行分區,選擇下一步,使用現有分區函數下一步使用現有資料分割配置,下一步會自動按照資料分割配置執行的日期進行分區,繼續點擊下一步選擇立即執行,完成後即可完成的整體的表分區自動執行.

     

    需注意:剛設定完第一步的計劃任務,可能不會執行第一步的資料分割配置的代碼,也就意味著沒有建立分區函數和資料分割配置,第二步設定的時候使用現有分區函數和使用現有資料分割配置也就不可用,可先把第一步的代碼執行一遍即可.

  2.  

sql2008 計劃自動建立資料庫分區【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.