Tuesday, February 28, 2012

How To Shrink The TEMPDB Database in SQL SERVER ?

Occasionally, Every DBA will bump across this issue, Tempdb database is getting full, Tempdb Drive has exceeded the disk threshold and need to take action to reduce the file size.

DBAs having little experience would think of shrinking the database using the GUI, Right Click on the Database > Tasks > Shrink > Database / Files and then shrink the database accordingly.

DBAs with some experience would think of running the command DBCC Shrinkfile ('Filename', target_in_MB)

However, with both the Solutions / Steps,  files / database doesn't shrink  and you can see that the file size is not having any impact.

So, here  is a couple of solutions you can try to shrink the Tempdb database and reduce the size on the drive accordingly.

1.  Try running the following command :-

USE TEMPDB
GO
DBCC FREEPROCCACHE
GO
DBCC FREESESSIONCACHE
GO
DBCC FREESYSTEMCACHE ('ALL')
GO

and then run the command.

DBCC SHRINKFILE ('FILENAME', TARGET_SIZE_IN_MB)
GO

If there are more than one Tempdb data ( Tempdev )  file , you can use those files seperately to run the DBCC Shrinkfile command to shrink the file size after clearing the DBCC System , Session and Proc Caches

These are the DBCC commands to free up some of the SQL SERVER Memory Caches.

DBCC FREESYSTEMCACHE ('ALL')
Clears all unused cache entries from all the caches.

DBCC FREESESSIONCACHE
Flushes all the distributed query connections' cache that are used against the instance of Microsoft SQL Server.

DBCC FREEPROCCACHE
Clears all elements from the procedure cache.
Freeing the procedure cache would cause, an ad-hoc SQL statement to be recompiled rather than reused from the cache.


2.  Here's another solution which can help to shrink the tempdb database.

Add a few MB to the data file and then try shrinking the file., it will release some space to the OS.

For Example:- when you find the tempdb is growing and has a couple of hundreds of MBs to reach the threshold. You can try shrinking the database normally. If there's any bulk transaction running against the instance or If there's a replication set up on the database.
Then, you can increase the tempdb data file to a few MBs and then try shrinking the file using the DBCC command to claim more space.

-Cheers

Thursday, February 9, 2012

Default Location for Backup & Restore database in SQL Server

"Hello World, "
Off course, even if we start writing or printing a program, the first output program we write is printf "Hello World".
So, here i go with the same trend.
This is my first blog publish .. I have been answering questions and doubts in many forums and blogs.., i thought, its time i start writing blogs and publish articles at my own blogspot. so, here we go..

Off lately, I have been interviewing candidates for a DBA position in our company. What i could infer from the answers is that, they fail to understand the basics of how SQL Server engine works.
First and the foremost issue i would like to discuss here is that, 
If there's a backup database command issued in the management studio / Query Analyzer without providing the appropriate backup location. what is the output ?

For Ex :- 
Backup database <database name> to disk = 'databasename_date_time.bak' 
what happens ??
Obviously, we have a common answer that the query returns error asking to provide a proper location. 
WRONG !!





This query works absolutely fine and the database will be backed up accordingly. 
Now, where will the database backup be residing ? how will it be backed up ? how can we check about the backup location ?

There's a default location mentioned for every database while installing. As, the other folders are created like the Data, log and Binn, SQL Server creates a folder called Backup in the root directory.


we can check the root directory location here :- 
Right click on the Database Instance > Properties. Under the General properties you have the Root Directory.
If we check in this location, we can find a folder called Backup to which the database file will be backed up.


Even while restoring the database, if we issue a command without providing the appropriate location. what does the SQL Server do ?

It restores the database successfully. Again, when the restore command is issued.. First, it checks if there's any location provided in the query, if not, SQL Server tries to find the backup file in the default location 


"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup"  to restore the database accordingly.


You must have noticed. if you use the GUI to restore any database and when you click on the browse button to navigate to the backup file path. First, it takes you to the default location and then lets you choose the proper location.

There's a lot more to come along the way.


-Cheers

Multiple Linear Regression

Multiple Linear Regression is a process that uses multiple explanatory variables to predict the outcome of a response variable . The pu...