20/05/2016
Temp database in SQL SERVER:
-----------------------------------------
Temp database is a global resource for all users connected to an instance. We are not using tempdb directly, but we are using tempdb while creating temp tables, cursors, rebuilding indexes etc.
Tempdb is mostly like a user database in physical level i.e. it contains two files ldf and Mdf.Tempdb is in simple recovery model, which means it is minimally logged.
Tempdb is created each time when SQL Server instance is restarted. So there is no need for moving tempdb to another server. We can change the location of the tempdb files such as ldf and mdf to new location using the command
USE master;
GO
ALTER DATABASE tempdb
MODIFYFILE(NAME = tempdev, FILENAME = 'C:Programfiles\SQL Server \SQL Files\tempdb.mdf');
GO
ALTER DATABASE tempdb
MODIFYFILE(NAME = templog, FILENAME = 'C:Programfiles\SQL Server \SQL Files\templog.ldf');
GO
Tempdb consists of :
* When we are creating temporary tables, global temporary tables, cursors.
* Tempdb is used by the SQL Server database engine, for creating work tables for doing sorting operations such as Group by, Union etc.
* Row versioning that are generated by data modifications transactions in a database.
* Row versioning that are generated by data modification such as online index operations, Multiple Active result sets and After triggers.
Sizing the tempdb :
----------------------
Default installation of any SQL Server edition will create a tempdb database with an 8MB data file and a 1MB transaction log file. For a lot of SQL Server installations these file sizes won’t be enough, but they are configured to autogrow by 10% as needed. You can see the properties window for tempdb on a default installation of SQL Server 2012 Developer Edition
Although the autogrow feature enables a more hands-off approach to maintaining many SQL Server installations, it’s not necessarily desirable because the files cannot be used while they are autogrowing, and it can lead to fragmentation of the files on the hard disk, leading to poor performance.
This is a recommendation that would apply to any SQL Server database, but for tempdb it’s even more relevant. When you restart your SQL Server instance, tempdb is re-created (files will be reused if they already exist) and sized to the value specified in the database properties, which as you’ve just seen is only 8MB for the data file and 1MB for the log file by default.
The next time SQL Server is restarted, tempdb will be just 8MB and will have to start autogrowing all over again
In this case, you can see the size of the initial files, which the DBA has set to 200MB and 50MB. The workload running against SQL Server has then caused the tempdb files to autogrow to 2450MB and 560MB.
SQL Server is then restarted and tempdb returns to 200MB and 50MB, as set by the DBA, and would have to autogrow again to fulfill the workload.
if you can give tempdb its own disk, then configure it to almost fill the drive. If nothing else will ever be on the drive, then you’re better off setting it to be larger than you’ll ever nee, and you’ll never have to worry about autogrow again.
If you’ve configured multiple data files and you want to allow autogrow, consider enabling trace flag 1117, which will force all data files to grow uniformly so you don’t break the load balancing between files.
For transaction log files, however, you need to be a lot more conservative and use a figure that balances the time it takes to autogrow and the usefulness of the extra space. Autogrowing by 1MB, for example, is quick, but you might need to do it so often that it becomes a bottleneck. Autogrowing by at least 10MB for the transaction log is a good place to start, but you may need it to be higher to provide enough space to avoid autogrowing again quickly. The best option is to avoid autogrowing in the first place by correctly sizing the files.
Still sizing tempdb requires lot more in depth information about the applications hitting the database and queries running. Regular monitoring the performance of database related to temp db helps in sizing and setting its growth perfectly.
Good Luck !!!