Advertisement
tei219

add tempdb files for each CPUs

Jan 23rd, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.94 KB | None | 0 0
  1. set nocount on
  2.  
  3. declare @cpu smallint
  4. declare @num_tempdb_files smallint
  5. declare @product_version smallint
  6. declare @tempdb_path nvarchar(1000)
  7. declare @exist_tempdb_files smallint
  8. set @product_version = @@microsoftversion / 0x01000000
  9.  
  10. -- count @cpu
  11. if @product_version = 8 or @product_version = 9 or @product_version = 10 or @product_version = 11
  12. begin
  13.     print '-- 2000/2005/2008/2008R2/2012'
  14.     create table #xp_msver (ID int,  Name  sysname, Internal_Value int, Value nvarchar(512))
  15.     insert into #xp_msver exec master.dbo.xp_msver 'ProcessorCount'
  16.     select @cpu = Internal_Value from #xp_msver
  17.     drop table #xp_msver
  18. end
  19.  
  20. -- set up @num_tempdb_files
  21. if (@cpu <= 8)
  22. begin
  23.     set @num_tempdb_files = @cpu
  24. end
  25. else
  26. begin
  27.     set @num_tempdb_files = 8
  28.     while @num_tempdb_files + 4 <= @cpu
  29.     begin
  30.         set @num_tempdb_files = @num_tempdb_files + 4
  31.     end
  32. end
  33. print '-- needed tempdb data files:' + cast(@num_tempdb_files as varchar)
  34. select @exist_tempdb_files = count(*) from tempdb.dbo.sysfiles where groupid <> 0
  35. print '-- exist tempdb data files:' + cast(@exist_tempdb_files as varchar)
  36.  
  37. -- print modify sql
  38. if @exist_tempdb_files >= @num_tempdb_files
  39. begin
  40.     print '-- already added. nothing to do.'
  41. end
  42. else
  43. begin
  44.     print '-- do add'
  45.     select @tempdb_path = rtrim(replace(filename,'tempdb.mdf','')) from tempdb.dbo.sysfiles where fileid = 1
  46.     print '-- file path:' + @tempdb_path
  47.     print 'use [master]'
  48.     print 'alter database [tempdb] modify file (name = N''tempdev'', size = 100, filegrowth = 100)'
  49.     print 'alter database [tempdb] modify file (name = N''templog'', size = 100, filegrowth = 100)'
  50.     while @exist_tempdb_files+1 <= @num_tempdb_files
  51.     begin
  52.         set @exist_tempdb_files = @exist_tempdb_files + 1
  53.         print 'alter database [tempdb] add file (name = N''tempdev' + cast(@exist_tempdb_files as varchar) + ''', filename = N''' + @tempdb_path + 'tempdev' + cast(@exist_tempdb_files as varchar) + '.ndf'', size = 100, filegrowth = 100)'
  54.     end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement