Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 February SQL Server: The TempDB is Full, Shrink it or Move it

SQL Server: The TempDB is Full, Shrink it or Move it

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a script to Shrink TempDB when it is full or occupying more hard disk space.

We know that TempDB contains temporary tables and other objects which are created by the user or system.
It also holds intermediate results that produced during query processing.
The TempDB also stores the different version of data which are generated using Snapshot Isolation Levels.

Because of above all reasons TempDB size is increasing so sometimes, we need to perform Shrink or we can move TempDB files from one location to another location.

Using below script you can find the correct size of TempDB.

1
2
3
4
5
6
7
8
9
10
11
USE TempDB
GO
 
SELECT
Name
,Physical_Name
,Type_Desc
,(Size*8) AS SizeInKB
FROM
SYS.DATABASE_FILES
GO

If we want to reset TempDB at some configured size, we should use to Shrink the TempDB.
We can also use both ALTER DATABASE and SHRINK command to reduce the size of the TempDB.

Using ALTER DATABASE:

1
2
3
4
5
6
7
ALTER DATABASE tempdb MODIFY FILE
(NAME = 'tempdev', SIZE = target_size_in_MB)
--Desired target size for the data file
 
ALTER DATABASE tempdb MODIFY FILE
(NAME = 'templog', SIZE = target_size_in_MB)
--Desired target size for the log file

Using SHRINK:

1
2
dbcc shrinkdatabase (tempdb, 'target percent')
-- This command shrinks the tempdb database

Move TempDB from one location to another location:

If we are facing size problem for a particular drive, we can also move the TempDB from one drive to another drive.

Before moving a TempDB, we should make sure that is set to autogrow and check the original size of TempDB files because we also require enough more space in the new location.

Note: We need to restart the SQL Server Restart Process, after these changes.

1
2
3
4
5
6
7
8
9
10
USE MASTER
GO
---- change the location of tempdev file
ALTER DATABASE TempDB MODIFY FILE
(NAME = tempdev, FILENAME = 'f:tempdb.mdf')
GO
---- change the location of templog file
ALTER DATABASE TempDB MODIFY FILE
(NAME = templog, FILENAME = 'f:templog.ldf')
GO

Feb 5, 2016Anvesh Patel
MySQL 5.7: Introduced EXPLAIN FOR CONNECTION to check the Execution Plan of Running ConnectionsSQL Server: Find First NOT NULL Column using a COALESCE Function
Anvesh Patel
Anvesh Patel

Database Engineer

February 5, 2016 SQL Server, SQL Server DBA ScriptAnvesh Patel, database, database research and development, dbrnd, Shrink, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TempDB, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....