How to Move TempDB

In order to move TempDB, use the alter database command with “modify file” and specify a new path. [cc lang=”sql”] use master go Alter database tempdb modify file (name = tempdev, filename = ‘e:tempdbtempdb.mdf’) go Alter database tempdb modify file (name = templog, filename = ‘e:tempdbtemplog.ldf’) go [/cc] Make sure the folder path already exist otherwise you will get an error. Once complete, an informative message will appear stating that tempdb will be moved the next time SQL is started. [code] The file “tempdev” has been modified in the system catalog. The new path will be used the next time the database is started. The file “templog” has been modified in the system catalog. The new path will be used the next time the database is started. [/code] This will require a restart of the SQL Server Database Engine service. Continue reading ...

What is Parallelism?

Parallelism is a feature in SQL Server which allows expensive queries to utilize more threads in order to complete quicker. The query optimizer makes the determination of how expensive a query is based upon the Cost Threshold for Parallelism setting set at the SQL Server Instance level. This cost threshold for parallelism is an estimate (roughly defined in seconds) that the query optimizer has determined a statement will take based on an estimated (or cached) execution plan. Generally the queries that qualify for parallelism are high IO queries. In a normal process, a source is read using a single SPID (Server Process ID) and it output using the same SPID. In parallelism, multiple SPIDs are used to read a source (this is known as distributing streams), then an operation may be performed in the streams, then the streams are gathered. Below is an illustration of this: Although parallelism may seem like a good idea, often times it is not. For OLTP systems that facilitate a lot of user requests parallelism is usually an indication of poorly written queries and/or queries that are in need of indexes. It can also cause issues on servers that have a lot of processors that also have disk IO contention. The reason for this is because parallelism will by default spawn as many SPIDs as their are processors. In other words, if you have a rogue query joining a hundred million records that does not qualify for parallelism, then by itself, only one process will […]

SQL Current Date

To get the current date in SQL Server use the GETDATE() function. Here is an example: [cc lang=”sql”] SELECT CurrentDate = GETDATE() [/cc] GetDate() returns a datetime data type value. There are also other methods in SQL Server that will return the current date. These are less commonly used. In fact, I have never personally had a need to use any of them except GETDATE(). [cc lang=”sql”] SELECT SYSDATETIME() SELECT SYSDATETIMEOFFSET() SELECT SYSUTCDATETIME() SELECT CURRENT_TIMESTAMP SELECT GETDATE() SELECT GETUTCDATE() [/cc] Continue reading ...

KEEPFIXED PLAN Query Hint

The KEEPFIXED PLAN query hint is used in order to retain the original execution plan used to initially compile a statement. It is typically used to disallow a recompile, especially in regards to a statement that utilizes temp table or a table that changes it’s percentage of records frequently resulting in triggering a recompile for a statement. To use, specify OPTION (KEEPFIXED PLAN) immediately after the statement that you do not want to recompile. [cc lang=”sql”] SELECT * FROM #Temp OPTION (KEEPFIXED PLAN) [/cc] Temp tables frequently recompile due to the change in statistics. When using temp tables within stored procedures, this can be a disadvantage. To get around the recompile, either use table variables (indexed with constraints) or use the KEEPFIXED PLAN query hint. Recompiles typically happen when the percentage of a tables (or temp tables) rows change by 500 and the cardinality (or uniqueness) changes by 20%.

MAXDOP Query Hint

The MAXDOP query hint is used to implicitly specify the maximum degree of parallelism to use for a specific statement.  The degree of parallelism is roughly defined as how many separate processors to utilize for a single query.  To set this, use the following convention: [cc lang=”sql”] SELECT * FROM Sales.SalesOrderDetail OPTION (MAXDOP 1) [/cc] This will force only one instance of the SPID to be spawned. To allow more instances of a SPID to be spawned replace the 1 with the maximum number. This needs to be specified after each statement. It is not specific to the statement. As with all query hints, it is recommended that you know internally what you are controlling by specifying this hint. While this specific hint is relatively harmless if you specify “1” it is still recommended you understand it’s effects. Continue reading ...

Featured Articles

 Site Author

  • Thanks for visiting!
css.php