When deploying to the cloud one of the hardest challenges is to get tempdb to scale in the same way as your virtual server.
Why is this, well as cloud machines are scalable and thus can change configurations during their lifecycle the number of tempdb files should be able to scale accordingly, so the amount of tempdb files becomes a dynamic value. Luckily for us, this parameter changes only after a shut down and restart of your virtual machine, so if we had a script that could adjust our tempdb needs every time the configuration changes we could deploy this and be more at ease.
This script does exactly that, if you place this code in a sql job that runs on startup, the script will adjust the tempdb settings accordingly every time you scale your cloud server.
I made the most important parameters completely adjustable while retaining maximum automation.
I did follow the best practices as much as possible which means that there will be an equal amount of tempdb files and cpu cores with a maximum of 8. This setting is not a parameter but can easily be adjusted when needed.
SET NOCOUNT ON;
/*
Script om tempdb te resetten op de Z:\ Drive van een (Amazon)VM
Dit is belangrijk omdat dan zowel de fileproperies als de filesettings correct geplaatst worden
*/
DECLARE @CMD nvarchar(200)
DECLARE @TempDBLocation nvarchar(200)
DECLARE @FilesizeInMB nvarchar(10)
DECLARE @FileGrowthInMB nvarchar(10)
DECLARE @TempDBPrimFile sysname
DECLARE @TempDBSecFile sysname
DECLARE @TempDBFileName varchar(2000)
DECLARE @Fileid int
DECLARE @Debug bit
DECLARE @Execute bit
DECLARE @AlwaysCleanUp bit
DECLARE @NumberOffFiles tinyint
DECLARE @NumberOffExistingFiles tinyint
SET @TempDBLocation=’Z:\’ –Location of tempdb, including the last \ sign
SET @FilesizeInMB=’1024′ — Filesize of the created files in MB
SET @FileGrowthInMB=’64’ — Filegrowth of the newly created files in MB
SET @Debug=0 –Set to 1 If you want the script to print the statements
SET @Execute=1 –Set to 1 If you want the script to execute the statements
SET @AlwaysCleanUp=0 –Set to 1 If you want the script to clean up the files on every reboot
SELECT @NumberOffFiles=CASE WHEN cpu_count < 8 THEN cpu_count ELSE 8 END FROM [sys].[dm_os_sys_info]
USE [tempdb];
Select @TempDBPrimFile=Name from sys.sysfiles where fileid=1
Select @NumberOffExistingFiles=MAX(fileid)-1,@Fileid=MAX(fileid)+1 from sys.sysfiles where groupid=1
IF SUBSTRING(@TempDBLocation,LEN(@TempDBLocation),1)<>’\’
BEGIN
PRINT ‘———————————————————-‘
PRINT ‘Error, Invallid location’
PRINT ‘———————————————————-‘
Goto Invalid
END
PRINT ‘———————————————————-‘
PRINT ‘Script Executing with the following parameters:’
PRINT ‘———————————————————-‘
PRINT ‘@TempDBLocation=’+@TempDBLocation
PRINT ‘@NumberOffFiles=’+CAST(@NumberOffFiles as varchar(5))
PRINT ‘@NumberOffExistingFiles=’+CAST(@NumberOffExistingFiles as varchar(5))
PRINT ‘@FilesizeInMB=’+@FilesizeInMB
PRINT ‘@FileGrowthInMB=’+@FileGrowthInMB
PRINT ‘@Debug=’+CAST(@Debug as char(1))
PRINT ‘@Execute=’+Cast(@Execute as char(1))
PRINT ‘@AlwaysCleanUp=’+CAST(@AlwaysCleanUp as char(1))
PRINT ‘———————————————————-‘
PRINT ‘Part 1 Resetting Tempdb to default settings on ‘+@TempDBLocation
PRINT ‘———————————————————-‘
IF (@AlwaysCleanUp=1 or @NumberOffExistingFiles<>@NumberOffFiles)
BEGIN
WHILE @NumberOffExistingFiles > 1
BEGIN
BEGIN TRY
SELECT @Fileid=MAX(fileid) from sys.sysfiles where groupid=1 and fileid < @Fileid
SELECT @TempDBSecFile=Name from sys.sysfiles where fileid=@Fileid
SET @CMD=’ALTER DATABASE [tempdb] REMOVE FILE [‘+@TempDBSecFile+’]’
IF @Execute=1 exec sp_executesql @CMD
IF @Debug=1 PRINT @CMD
END TRY BEGIN CATCH PRINT ERROR_MESSAGE() END CATCH
BEGIN TRY
SELECT @TempDBFileName=[filename] from sys.sysfiles where fileid=@Fileid
SET @CMD=’DEL ‘+@TempDBFileName+’ /Q’
IF @Execute=1 exec xp_cmdshell @CMD
IF @Debug=1 PRINT ‘EXEC xp_cmdshell ‘+@CMD
END TRY BEGIN CATCH PRINT ERROR_MESSAGE() END CATCH
set @NumberOffExistingFiles=@NumberOffExistingFiles-1
END
PRINT ‘———————————————————-‘
PRINT ‘Finished Part 2 removing residual files on ‘+@TempDBLocation
PRINT ‘———————————————————-‘
BEGIN TRY
SET @CMD = ‘USE [tempdb]; DBCC SHRINKFILE (N”’+@TempDBPrimFile+”’ , ‘+CAST(@FilesizeInMB*1024 as varchar(100))+’)’
IF @Execute=1 exec sp_executesql @CMD
IF @Debug=1 PRINT @CMD
END TRY BEGIN CATCH PRINT ERROR_MESSAGE() END CATCH
PRINT ‘———————————————————-‘
PRINT ‘Part 3 Recreating files with correct filesize on ‘+@TempDBLocation
PRINT ‘———————————————————-‘
WHILE @NumberOffFiles > 1
BEGIN
BEGIN TRY
SET @CMD=’USE [master]; ALTER DATABASE [tempdb] ADD FILE ( NAME = N”tempdev0’+CAST(@NumberOffFiles as char(1))+”’, FILENAME = N”’+@TempDBLocation+’tempdev0’+CAST(@NumberOffFiles as char(1))+’.ndf” , SIZE = ‘+@FilesizeInMB+’MB , FILEGROWTH = ‘+@FileGrowthInMB+’MB )’
IF @Execute=1 exec sp_executesql @CMD
IF @Debug=1 PRINT @CMD
END TRY BEGIN CATCH PRINT ERROR_MESSAGE() END CATCH
SET @NumberOffFiles=@NumberOffFiles-1
END
END
ELSE
PRINT ‘———————————————————-‘
PRINT ‘Had nothing to do’
Invalid:
PRINT ‘———————————————————-‘
PRINT ‘End of Script’
PRINT ‘———————————————————-‘
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |