2

As the title implies, I am going to clear the content of my temp database in SQL Server. SolarWinds is generating a critical alert and I want to clear the tempdb workfiles and worktables.

How can I do this? Can I restart the MSSQLSERVER service to clear the content?

  • Why would you like to clear it? Its contents are automatically maintained, so save for size reduction it doesn't really need much maintenance. – vonPryz Feb 16 at 8:12
  • @vonPryz I want to clear workfiles and worktables created in tempdb. In SolarWinds they are in critical status. Can I shrink temp database? – Commander Feb 16 at 8:19
  • Please add more information--exactly what are the metrics that are considered critical? A raw number of tempdb tables should not be considered "critical" unless an application has a defect that is creating a gazillion tables. – Tony Hinkle Feb 16 at 11:15
11

If you truly feel you need to clear it, then yes, restarting SQL Server will clear it. However, that will likely be a temporary solution and you'll be back in the same condition after a few days. Restarting SQL Server is the only way to clear the tempdb content from a SQL Server perspective. Another possible alternative would be to restart the application that is creating the objects in tempdb.

It's impossible for us, given the bit of information that you provide, to know what the best course of action is. Some things to consider:

  • Just because SolarWinds says something is a problem doesn't necessarily mean it's a problem. Many monitoring tools are set with defaults that will send "critical" alerts for things that are 100% expected on a given server. For example, I would expect a Dynamics AX SQL Server to trigger many of the default tempdb thresholds of most monitoring tools. If there is no significant tempdb contention and it's not running out of space, then there's probably nothing critical. Is the application that is using the SQL Server having performance issues? Is anyone complaining about performance? If not, then the SolarWinds alerts aren't reflecting reality and thresholds maybe need to be adjusted. One thing to keep in mind is that many of these applications were initially coded and tested when storage was much slower. If you have tempdb on SSDs, they can take much more beating than a couple of mirrored local SCSI disks that were probably used when SolarWinds decided what thresholds to use.

  • Do you have tempdb sized appropriately? Some application make heavy use of tempdb and will need far more than other applications typically use. If tempdb is just running out of space, you may just need larger files for tempdb

  • Do you have one tempdb database file for each processor, with a maximum of 8?

  • Do you have trace flags 1117 and 1118 enabled if you are using SQL Server 2014 or earlier? If not, research them--if you have an application that has significant tempdb usage, you'll probably want to enable these (I see you have used the sql-server-2016 tag, so this is for the benefit of other readers).

  • Is there a defect in an application that is causing it to "leak" tempdb objects? If the number of tempdb tables and usage is constantly climbing and never stabilizes, then it would be advisable to investigate this.

If you can't resist the urge to shrink it, shrink it with TRUNCATEONLY. TRUNCATEONLY may shrink it a lot, or none--it just depends on where the last used extent is. If you shrink it to a specified size, it will start reorganizing the data (i.e., fragmenting it) in order to move data from the end of the file to any free space that exists in the middle. In order to do that it will require locks and your users/application my have significant performance impact. And you never know how long it's going to take to reorganize that data.

If you have more info to add to the question about the exact metric(s) SolarWinds is alerting on that would be helpful.

  • Thank you for your complete answer, but I have a question: Why shouldn't I shrink temp db? – Commander Feb 19 at 7:57
  • 1
    I will take that back and edit the answer--you can shrink it with TRUNCATEONLY. If you shrink it to a specified size, it will start reorganizing the data (i.e., fragmenting it) in order to move data from the end of the file to any free space that exists in the middle. In order to do that it will require locks and your users/application my have significant performance impact. – Tony Hinkle Feb 19 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.