0_database_server_options.sql 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. USE [master]
  2. GO
  3. -- Limit error logs
  4. EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'NumErrorLogs', REG_DWORD, 15
  5. GO
  6. -- Set sp_configure settings
  7. EXEC sys.sp_configure N'show advanced options', N'1'
  8. RECONFIGURE WITH OVERRIDE
  9. GO
  10. EXEC sys.sp_configure N'remote admin connections', N'1'
  11. RECONFIGURE WITH OVERRIDE
  12. GO
  13. -- Use 'backup compression default' when server is NOT CPU bound
  14. IF CONVERT(int, (@@microsoftversion / 0x1000000) & 0xff) >= 10
  15. EXEC sys.sp_configure N'backup compression default', N'1'
  16. RECONFIGURE WITH OVERRIDE
  17. GO
  18. -- Use 'optimize for ad hoc workloads' for OLTP workloads ONLY
  19. IF CONVERT(int, (@@microsoftversion / 0x1000000) & 0xff) >= 10
  20. EXEC sys.sp_configure N'optimize for ad hoc workloads', N'1'
  21. RECONFIGURE WITH OVERRIDE
  22. GO
  23. EXEC sys.sp_configure N'show advanced options', N'0'
  24. RECONFIGURE WITH OVERRIDE
  25. GO
  26. USE [master]
  27. GO
  28. -- Set model defaults
  29. ALTER DATABASE [model] MODIFY FILE ( NAME = N'modeldev', FILEGROWTH = 102400KB )
  30. GO
  31. ALTER DATABASE [model] MODIFY FILE ( NAME = N'modellog', FILEGROWTH = 102400KB )
  32. GO
  33. -- Set database option defaults (ignore errors on tempdb and read-only databases)
  34. USE [master]
  35. GO
  36. EXEC master.dbo.sp_MSforeachdb @command1='USE master; ALTER DATABASE [?] SET AUTO_CLOSE OFF WITH NO_WAIT'
  37. EXEC master.dbo.sp_MSforeachdb @command1='USE master; ALTER DATABASE [?] SET AUTO_SHRINK OFF WITH NO_WAIT'
  38. EXEC master.dbo.sp_MSforeachdb @command1='USE master; ALTER DATABASE [?] SET PAGE_VERIFY CHECKSUM WITH NO_WAIT'
  39. --EXEC master.dbo.sp_MSforeachdb @command1='USE master; ALTER DATABASE [?] SET AUTO_CREATE_STATISTICS ON'
  40. --EXEC master.dbo.sp_MSforeachdb @command1='USE master; ALTER DATABASE [?] SET AUTO_UPDATE_STATISTICS ON'
  41. GO
  42. --SET proper MaxDOP
  43. DECLARE @cpucount int, @numa int, @affined_cpus int, @sqlcmd NVARCHAR(255)
  44. SELECT @affined_cpus = COUNT(cpu_id) FROM sys.dm_os_schedulers WHERE is_online = 1 AND scheduler_id < 255 AND parent_node_id < 64;
  45. SELECT @cpucount = COUNT(cpu_id) FROM sys.dm_os_schedulers WHERE scheduler_id < 255 AND parent_node_id < 64
  46. SELECT @numa = COUNT(DISTINCT parent_node_id) FROM sys.dm_os_schedulers WHERE scheduler_id < 255 AND parent_node_id < 64;
  47. SELECT @sqlcmd = 'sp_configure ''max degree of parallelism'', ' + CONVERT(NVARCHAR(255), CASE WHEN [value] > @affined_cpus THEN @affined_cpus
  48. WHEN @numa = 1 AND @affined_cpus > 8 AND ([value] = 0 OR [value] > 8) THEN 8
  49. WHEN @numa > 1 AND (@cpucount/@numa) < 8 AND ([value] = 0 OR [value] > (@cpucount/@numa)) THEN @cpucount/@numa
  50. WHEN @numa > 1 AND (@cpucount/@numa) >= 8 AND ([value] = 0 OR [value] > 8 OR [value] > (@cpucount/@numa)) THEN 8
  51. ELSE 0
  52. END)
  53. FROM sys.configurations (NOLOCK) WHERE name = 'max degree of parallelism';
  54. EXECUTE sp_executesql @sqlcmd;
  55. GO
  56. EXEC sys.sp_configure N'show advanced options', N'0'
  57. RECONFIGURE WITH OVERRIDE
  58. GO