SQL Tempdb taking up space

Sandro Alves 41 Reputation points
2023-02-05T00:36:15.62+00:00

Hi friends,

we have two synchronous and one asynchronous sql 2016 servers with Always On.

Our tempdb are six files in one volume on one disk and another six files on another disk for a total of 12 files.

During the early morning routines, a curious scenario happened twice that called into question the concept of tempdb management.

During these routines, one of the disks occupied all its space and the DBA team reported that one of the JOBs had failed due to disk failure.

In light of this, questions arose:

We understand any activity in SQL will manage using 12 tempdb files allocating activities randomly. However, we are in doubt if for example:

Activity 1, whatever it is, did it start using tempdb_01 and at the same time it will use another tempdb? Or will it get stuck on that tempdb only?

If an activity keeps working on a tempdb, how does it know that disk space is being taken up and needs to use another one? I believe that SQL does not have this visibility.

Thanks.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,707 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Erland Sommarskog 101K Reputation points MVP
    2023-02-05T11:22:42.3533333+00:00

    There is only one single tempdb, but a database can be split up on many filegroups, and a filegroup can consist of several files. For tempdb, you typically only have a single filegroup, but it is recommended to have many files.

    When there are multiple files in a filegroup, SQL Server will allocate space in these files in a round-robin fashion. That is, if you create a temp table and fill it up with lots of data, the first extent of eight pages will be on, say, file 2. Next extent will be on file 3 etc. If you have 12 files, the 13th extent will be next to appear in file 2. This presumes that all files are of the same size, which is also is highly recommended.

    To this comes the log file. You can have multiple log files, but in difference to data files, there is little point in this, so typically you only have one log file. Since tempdb is in simple recovery, the transaction log for tempdb is frequently truncated, but if there is a long-running transaction, it can still grow and eventually eat up all available disk space.


  2. Sandro Alves 41 Reputation points
    2023-02-05T15:25:55.0833333+00:00

    Hi,

    grateful for your explanation.

    So even if I have all this division of files, if a transaction remains active it won't have time for the logs to truncate.

    In this case, as I only have one (tempdb.log), probably the file that grew was it?

    Is there any strategy to prevent this from happening?

    I remember that this happened occasionally, it doesn't happen often.

    See how our file division is structured:

    tempdb.ldf (disk 1)

    tempdb.mdf (disk 1)

    temp03.ndf (disk 1)

    temp05.ndf (disk 1)

    temp07.ndf (disk 1)

    temp09.ndf (disk 1)

    temp11.ndf (disk 1)

    temp02.ndf (disk 2)

    temp04.ndf (disk 2)

    temp06.ndf (disk 2)

    temp08.ndf (disk 2)

    temp10.ndf (disk 2)

    temp12.ndf (disk 2)

    (disk 1)

    Screenshot 2023-02-05 121200

    (disk 2)

    Screenshot 2023-02-05 121219

    Tks.

    0 comments No comments

  3. Erland Sommarskog 101K Reputation points MVP
    2023-02-05T19:08:32.0933333+00:00

    To repeat what I said in my last post: make sure that all data files for tempdb have the same size. Furthermore, remove the max size setting. It does not serve you. If you add more disk, you will still face error 1105 when you have consumed 12*8 = 96 GB of tempdb space. 96 GB of tempdb space may sound like a lot, and indeed if your actual database is only 5-10 GB it is. But if your production database is 5-10 TB, it is not.

    It would be interesting to monitor the growth and shrinkage of these files

    The only time these files shrink is when you restart SQL Server, in which case they return to their configured sizes. But once the system is running, they can only grow. SQL Server will not shrink them on its own initiative.

    When this overflow occurs or when space is close to running out, what is the correct procedure to free up disk space without losing the workload that is running?

    If you want to save the workload from crashing, all you can do is to find more disk space for data or log, depending on what is about to run out.

    Last time the DBA team ran a shrink during the routines

    That's pointless in most cases. if the file has grown to a certain size, it was because that space was needed. The required space is not going to be less if you shrink a file. All that happen is that file will grow again.

    But if the tempdb files competes with other files on the disk, it may be required to shrink files once the operation has crashed. On the other hand, if the file is dedicated to tempdb, why bother?


  4. CosmogHong-MSFT 22,941 Reputation points Microsoft Vendor
    2023-02-06T08:32:56.46+00:00

    Hi @Sandro Alves

    You can use sys.dm_db_file_space_usage to see who is using these spaces and how.

    By monitoring this, you can know which object the tempdb space is being used, whether it is a user object (user_object_reserved_page_count), a system object (internal_object_reserved_page_count), or a version store (version_store_reserved_page_count).

    Another question is how much is the best initial tempdb size? The appropriate size of tempdb in a production environment depends on a variety of factors, so there is no fixed answer to this question. These factors include existing workloads and the SQL Server features used. So, every SQL Server will be different. If a new feature of the same SQL Server uses tempdb is added, its space usage will also change. It is recommended that you analyze your existing workload by performing the following tasks in a SQL Server test environment:

    1. Set the autogrowth of tempdb.
    2. Simulate individual queries or work tasks while monitoring tempdb space usage.
    3. Simulate performing some system maintenance operations, such as rebuilding indexes, while monitoring tempdb space.
    4. Use the tempdb space usage values in the previous steps 2 and 3 to predict how much space will be used under the total workload; and adjust this value for the planned concurrency. For example, if a task uses 10 GB of tempdb space, and in a production environment there may be up to 4 such tasks running at the same time, reserve at least 40 GB of space.
    5. According to the value obtained in step 4, set the initial size of tempdb in the production environment. Autogrowth is also turned on.

    Also, the number of tempdb files and the size settings should not only meet the needs of user tasks, but also consider performance optimization.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.