mysql 表分割槽指令碼 自動生成資料庫表分割槽指令碼

2021-10-17 19:22:11 字數 4083 閱讀 8946

--按時間(日期)動態生成sql server資料庫分割槽指令碼,分好區後,手動和表建立關聯即可--生成分割槽指令碼

declare @databasename nvarchar(50)--資料庫名稱

declare @tablename nvarchar(50)--表名稱

declare @columnname nvarchar(50)--欄位名稱

declare @partnumber int--需要分多少個區

declare @location nvarchar(50)--儲存分割槽檔案的路徑

declare @size nvarchar(50)--分割槽初始化大小

declare @filegrowth nvarchar(50)--分割槽檔案增量--declare @funvalue int--分割槽分段值

declare @i int

declare @y int      --起始年份

declare @m int      --起始月份

declare @d int      --起始天

declare @daygap int --分割槽分段值 天數

declare @partnumberstr nvarchar(50)declare @sql nvarchar(max)declare @valuestart nvarchar(50)--設定下面變數

set @databasename = 'loreal_mscrm'

set @tablename = 'tsr_transactionbase'

set @columnname = 'tsr_transactiondate'

set @partnumber = 150

set @location = 'f:\data\'

set @size = '80mb'

set @filegrowth = '30%'

--2007-03-02 13:51:06.147

set @y = 2007

set @m = 3

set @d = 2

set @daygap = 30

--1.建立檔案組

set @i = 1

print '--1.建立檔案組'

while @i <= @partnumber

beginset @partnumberstr =  right('000' + convert(nvarchar,@i),4)set @sql = 'alter database ['+@databasename +']

add filegroup [fg_'+@tablename+'_'+@columnname+'_'+@partnumberstr+']'print @sql + char(13)set @i=@i+1

end--2.建立檔案

set @i = 1

print char(13)+'--2.建立檔案'

while @i <= @partnumber

beginset @partnumberstr =  right('000' + convert(nvarchar,@i),4)set @sql = 'alter database ['+@databasename +']

add file

(name = n''fg_'+@tablename+'_'+@columnname+'_'+@partnumberstr+'_data'',filename = n'''+@location+'fg_'+@tablename+'_'+@columnname+'_'+@partnumberstr+'_data.ndf'',size ='+@size+', filegrowth ='+@filegrowth+')

to filegroup [fg_'+@tablename+'_'+@columnname+'_'+@partnumberstr+'];'print @sql + char(13)set @i=@i+1

end--3.建立分割槽函式

print char(13)+'--3.建立分割槽函式'

declare @funvaluestr nvarchar(max)--set @funvaluestr = substring(@funvaluestr,1,len(@funvaluestr)-1)

set @sql = 'create partition function

fun_'+@tablename+'_'+@columnname+'(datetime) as

range right

for values('

print @sql

set @i = 1

set @funvaluestr = ''

while @i < @partnumber

beginset @funvaluestr = @funvaluestr + '''' + right(convert(nvarchar, @y),4) + '-' + right('0' + convert(nvarchar, @m),2) + '-' + right('0' + convert(nvarchar, @d),2) + '00:00:00.000'','set @d = @d + @daygap

if (@d > 30 or (@m = 2 and @d > 28))  --二月需要特殊處理一下

beginset @d = 1set @m = @m + 1if (@m > 12)beginset @m = 1set @y = @y + 1endendif (@i = @partnumber - 1)begin

set @funvaluestr = substring(@funvaluestr,1,len(@funvaluestr)-1)endprint @funvaluestrset @funvaluestr = ''set @i=@i+1

endprint ')' + char(13)--4.建立分割槽方案

print char(13)+'--4.建立分割槽方案'

declare @filegroupstr nvarchar(max)--set @filegroupstr = substring(@filegroupstr,1,len(@filegroupstr)-1)

set @sql = 'create partition scheme

sch_'+@tablename+'_'+@columnname+'as

partition fun_'+@tablename+'_'+@columnname+'to('

print @sql

set @i = 1

set @filegroupstr = ''

while @i <= @partnumber

beginset @partnumberstr =  right('000' + convert(nvarchar,@i),4)set @filegroupstr = @filegroupstr + '[fg_'+@tablename+'_'+@columnname+'_'+@partnumberstr+'],'if (@i = @partnumber)begin

set @filegroupstr = substring(@filegroupstr,1,len(@filegroupstr)-1)endprint @filegroupstrset @filegroupstr = ''set @i=@i+1

endprint ')'

--5.分割槽函式的記錄數

print char(13)+'--5.分割槽函式的記錄數'

set @sql = 'select $partition.fun_'+@tablename+'_'+@columnname+'('+@columnname+') as partition_num,

min('+@columnname+') as min_value,max('+@columnname+') as max_value,count(1) as record_num

from dbo.'+@tablename+'group by $partition.fun_'+@tablename+'_'+@columnname+'('+@columnname+')

order by $partition.fun_'+@tablename+'_'+@columnname+'('+@columnname+');'

print @sql + char(13)

Mysql自動分割槽

前提 test為資料庫名 ip demo為表名 建立分割槽 alter table ip demo partition by range to days date partition p20180716 values less than to days 2018 07 17 注意 這裡date必須為...

自動生成資料庫字典

1.首先需要填寫表的每一列的說明 2.建立試圖 go object view dbo sura view dbdictionary script date 05 31 2013 17 19 19 set ansi nulls on goset quoted identifier on gocreat...

php自動生成資料字典

生成mysql資料字典 配置資料庫 dbserver 127.0.0.1 dbusername 賬號 dbpassword 密碼 database 資料庫 其他配置 title 資料字典 mysql conn mysql connect dbserver dbusername dbpassword ...