Moving Tempdb AND changing Initial size at the same time.

Mark Gordon 916 Reputation points
2021-07-25T17:47:42.17+00:00

Fellow DBa's

I need to move tempdb and reset the inital size - my growth is fine. Obviously both of these require a restart.

My questions are:

1 - Can I do these all in the same batch of commands, like below, and do one restart. Or do one set (location change), restart and then another set (initial size) and restart.
2 - Does it matter which goes first: init and location move or location move and then init.

Was planning to to the below in this exact order with just one reboot for all of it.

ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'M:\MSSQL\DATA\tempdb.mdf');
GO

ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'M:\MSSQL\DATA\templog.ldf');
GO

ALTER DATABASE tempdb MODIFY FILE
(NAME = 'tempdev', SIZE = 1024)

GO

ALTER DATABASE tempdb MODIFY FILE
(NAME = 'templog', SIZE = 1024)

GO

thx MG

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,822 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mark Gordon 916 Reputation points
    2021-07-26T15:53:53.357+00:00

    Fellow DBAs
    I have completed the test for the tempdb scenario.

    yes, for TempDB, you can run your size change script in the same code along with the location change script. Then restart. It works fine.

    But I did uncover a scenario that explains why some say that when you change the tempdb initial size you have to restart sql and others say you do not.

    I was testing with SQL 2016

    1 - You want to increase your tempdb mdf/ndf files to be consistent. You change the size with the guid to be the same. Look at the script. It is simply using Alter Database...... size... ). That did NOT need a restart.

    2 - But, if you want to reduce the intial size, like in my situation, it is different. Lets say your multi tempdb files were never aligned right - some where 5 gb, some where 1 gb and you saw lots of free space on the larger ones. Thus you might choose to take initial size down. I wanted 1024 mb. If you do this in the gui, and then generate the sql script, you will find a shrinkfile command with your new size. That is fine but if tempdb is in use, you likely will not be able to get the size down. That is also what happended to me in the beginning. Actually some of the files did shrink a tad but no where near enough. In that scenario, you have to use the Alter Database ..... Size.... command. Then do a restart.

    Thx

    MG


4 additional answers

Sort by: Most helpful
  1. AmeliaGu-MSFT 13,976 Reputation points Microsoft Vendor
    2021-07-26T06:23:53.523+00:00

    Hi MarkGordon-2676,

    Some things got out of limits and I am taking the size down across my tempdbs.

    Could you please check the used space of tempdb?

    USE tempdb;  
    Go  
    SELECT Name , Size/128.0 AS Size , FILEPROPERTY(Name , 'SpaceUsed') /128.0 AS UsedSpaceMB,  
    size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB   
    FROM sys.database_files;   
    

    Please check if the initial size you want to set is greater than the used size when the sql server is not restarted.

    Please refer to this method from this article which might help:
    117863-image.png

    Best Regards,
    Amelia


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Andreas Baumgarten 109.6K Reputation points MVP
    2021-07-25T17:56:38.743+00:00

    Hi @Mark Gordon ,

    as far as I know increasing the temp db size does not require a restart of the SQL service.
    Other resources in the internet saying you need to restart the SQL service after resizing the tempdb.

    I would recommend to resize the tempdb first, move the tempdb and restart SQL service at the end.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. Erland Sommarskog 111.5K Reputation points MVP
    2021-07-25T20:21:51.187+00:00

    I would find a throwaway instance somewhere and test. VMs are great for this. Create a snapshot before you test, so that you can roll back if things go south unexpectedly.

    My gut feeling is that you should be able to modify all in one go, but test before you take it to production.


  4. Andreas Baumgarten 109.6K Reputation points MVP
    2021-07-25T20:29:12.83+00:00

    Hi @Mark Gordon ,

    reducing/increasing the size of the tempdb should be the same way.

    The approach of Erland is the best option to give it a try.
    Resize tempdb, move tempdb and restart SQL service on a test SQL instance

    If this works:

    Create a snapshot before doing anything with the production SQL Server
    Resize, move and restart SQL service on a production SQL instance

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.