Skip to content

Commit 5cb8fbe

Browse files
Merge pull request #27968 from WilliamDAssafMSFT/20230807-tempdb
20230807 tempdb
2 parents 24ad396 + 7d029dd commit 5cb8fbe

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

docs/relational-databases/databases/tempdb-database.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: This article provides details about the configuration and use of th
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
66
ms.reviewer: randolphwest
7-
ms.date: 04/05/2023
7+
ms.date: 08/07/2023
88
ms.service: sql
99
ms.topic: conceptual
1010
ms.custom: P360
@@ -47,9 +47,9 @@ The following table lists the initial configuration values of the `tempdb` data
4747

4848
| File | Logical name | Physical name | Initial size | File growth |
4949
| --- | --- | --- | --- | --- |
50-
| Primary data | tempdev | tempdb.mdf | 8 megabytes | Autogrow by 64 MB until the disk is full |
51-
| Secondary data files | temp# | tempdb_mssql_#.ndf | 8 megabytes | Autogrow by 64 MB until the disk is full |
52-
| Log | templog | templog.ldf | 8 megabytes | Autogrow by 64 megabytes to a maximum of 2 terabytes |
50+
| Primary data | `tempdev` | `tempdb.mdf` | 8 megabytes | Autogrow by 64 MB until the disk is full |
51+
| Secondary data files | `temp#` | `tempdb_mssql_#.ndf` | 8 megabytes | Autogrow by 64 MB until the disk is full |
52+
| Log | `templog` | `templog.ldf` | 8 megabytes | Autogrow by 64 megabytes to a maximum of 2 terabytes |
5353

5454
The number of secondary data files depends on the number of (logical) processors on the machine. As a general rule, if the number of logical processors is less than or equal to eight, use the same number of data files as logical processors. If the number of logical processors is greater than eight, use eight data files. Then if contention continues, increase the number of data files by multiples of four until the contention decreases to acceptable levels, or make changes to the workload/code.
5555

@@ -101,7 +101,7 @@ For a description of these database options, see [ALTER DATABASE SET Options (Tr
101101

102102
## tempdb in Azure SQL
103103

104-
The behavior of `tempdb` in Azure SQL Database differs from the behavior SQL Server, Azure SQL Managed Instance, and SQL Server on Azure VMs.
104+
The behavior of `tempdb` in Azure SQL Database differs from the behavior SQL Server, Azure SQL Managed Instance, and SQL Server on Azure VMs.
105105

106106
### tempdb in SQL Database
107107

@@ -116,7 +116,9 @@ To learn more about `tempdb` sizes in Azure SQL Database, review:
116116

117117
### tempdb in SQL Managed Instance
118118

119-
[Azure SQL Managed Instance](/azure/azure-sql/managed-instance/sql-managed-instance-paas-overview) supports temporary objects in the same way as SQL Server, where all global temporary tables and global temporary stored procedures are accessible by all user sessions within the same managed instance. Likewise, all system databases are accessible.
119+
Azure SQL Managed Instance supports temporary objects in the same way as SQL Server, where all global temporary tables and global temporary stored procedures are accessible by all user sessions within the same managed instance. Likewise, all system databases are accessible.
120+
121+
For more information on configuring `tempdb` settings in Azure SQL Managed Instance, see [Configure tempdb settings for Azure SQL Managed Instance](/azure/azure-sql/managed-instance/tempdb-configure?view=azuresql-mi&preserve-view=true).
120122

121123
To learn more about `tempdb` sizes in Azure SQL Managed Instance, review [resource limits](/azure/azure-sql/managed-instance/resource-limits).
122124

@@ -154,7 +156,7 @@ Preallocate space for all `tempdb` files by setting the file size to a value lar
154156

155157
Data files should be of equal size within each [filegroup](../../relational-databases/databases/database-files-and-filegroups.md#filegroups), because [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] uses a proportional-fill algorithm that favors allocations in files with more free space. Dividing `tempdb` into multiple data files of equal size provides a high degree of parallel efficiency in operations that use `tempdb`.
156158

157-
Set the file growth increment to a reasonable size and set it to the same increment in all data files, to prevent the `tempdb` database files from growing by too small a value. If the file growth is too small compared to the amount of data that's being written to `tempdb`, `tempdb` might have to constantly expand. That will affect performance.
159+
Set the file growth increment to a reasonable size and set it to the same increment in all data files, to prevent the `tempdb` database files from growing by too small a value. If the file growth is too small compared to the amount of data that's being written to `tempdb`, `tempdb` might have to frequently expand via autogrowth events. Autogrowth events negatively affect performance.
158160

159161
To check current size and growth parameters for `tempdb`, use the following query:
160162

@@ -186,9 +188,12 @@ Put the `tempdb` database on a fast I/O subsystem. Use disk striping if there ar
186188

187189
Put the `tempdb` database on disks that differ from the disks that user databases use.
188190

191+
> [!NOTE]
192+
> Even though the database option `DELAYED_DURABILITY` is set to DISABLED for `tempdb`, SQL Server uses [lazy commits](../logs/control-transaction-durability.md) to flush `tempdb` log changes to disk, since `tempdb` is created at startup and doesn't need to run the recovery process.
193+
189194
## Performance improvements in tempdb for SQL Server
190195

191-
Starting with [!INCLUDE[sssql16-md](../../includes/sssql16-md.md)], `tempdb` performance is further optimized in the following ways:
196+
Starting with [!INCLUDE [sssql16-md](../../includes/sssql16-md.md)], `tempdb` performance is further optimized in the following ways:
192197

193198
- Temporary tables and table variables are cached. Caching allows operations that drop and create the temporary objects to run very quickly. Caching also reduces page allocation and metadata contention.
194199
- The allocation page latching protocol is improved to reduce the number of `UP` (update) latches that are used.
@@ -197,6 +202,7 @@ Starting with [!INCLUDE[sssql16-md](../../includes/sssql16-md.md)], `tempdb` per
197202
- When there are multiple `tempdb` data files, all files autogrow at the same time and by the same amount, depending on growth settings. [Trace flag 1117](../../t-sql/database-console-commands/dbcc-traceon-trace-flags-transact-sql.md) is no longer required.
198203
- All allocations in `tempdb` use uniform extents. [Trace flag 1118](../../t-sql/database-console-commands/dbcc-traceon-trace-flags-transact-sql.md) is no longer required.
199204
- For the primary filegroup, the `AUTOGROW_ALL_FILES` property is turned on and the property can't be modified.
205+
- Starting in [!INCLUDE [sssql19-md](../../includes/sssql19-md.md)], SQL Server does not use the `FILE_FLAG_WRITE_THROUGH` option when opening files for `tempdb` to allow for maximum disk throughput. Since `tempdb` is recreated on startup of SQL Server, these options are not needed as they are for other system databases and user databases for data consistency. For more information on `FILE_FLAG_WRITE_THROUGH`, see [Logging and data storage algorithms that extend data reliability in SQL Server](/troubleshoot/sql/database-engine/database-file-operations/logging-data-storage-algorithms#performance-impacts).
200206

201207
For more information on performance improvements in `tempdb`, see the blog article [TEMPDB - Files and Trace Flags and Updates, Oh My!](/archive/blogs/sql_server_team/tempdb-files-and-trace-flags-and-updates-oh-my).
202208

@@ -243,7 +249,7 @@ This change also requires a restart to take effect, even if memory-optimized `te
243249

244250
- Toggling the feature on and off is not dynamic. Because of the intrinsic changes that need to be made to the structure of `tempdb`, a restart is required to either enable or disable the feature.
245251

246-
- A single transaction is not allowed to access memory-optimized tables in more than one database. Any transactions that involve a memory-optimized table in a user database won't be able to access `tempdb` system views in the same transaction. If you try to access `tempdb` system views in the same transaction as a memory-optimized table in a user database, you'll receive the following error:
252+
- A single transaction is not allowed to access memory-optimized tables in more than one database. Any transactions that involve a memory-optimized table in a user database won't be able to access `tempdb` system views in the same transaction. If you try to access `tempdb` system views in the same transaction as a memory-optimized table in a user database, you receive the following error:
247253

248254
```output
249255
A user transaction that accesses memory optimized tables or natively compiled modules cannot access more than one user database or databases model and msdb, and it cannot write to master.
@@ -263,7 +269,7 @@ This change also requires a restart to take effect, even if memory-optimized `te
263269
COMMIT TRAN;
264270
```
265271

266-
- Queries against memory-optimized tables don't support locking and isolation hints, so queries against memory-optimized `tempdb` catalog views won't honor locking and isolation hints. As with other system catalog views in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], all transactions against system views will be in `READ COMMITTED` (or in this case, `READ COMMITTED SNAPSHOT`) isolation.
272+
- Queries against memory-optimized tables don't support locking and isolation hints, so queries against memory-optimized `tempdb` catalog views won't honor locking and isolation hints. As with other system catalog views in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], all transactions against system views are in `READ COMMITTED` (or in this case, `READ COMMITTED SNAPSHOT`) isolation.
267273

268274
- [Columnstore indexes](../indexes/columnstore-indexes-overview.md) can't be created on temporary tables when memory-optimized `tempdb` metadata is enabled.
269275

@@ -276,16 +282,20 @@ This change also requires a restart to take effect, even if memory-optimized `te
276282
277283
## Capacity planning for tempdb in SQL Server
278284

279-
Determining the appropriate size for `tempdb` in a [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] production environment depends on many factors. As described earlier, these factors include the existing workload and the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] features that are used. We recommend that you analyze the existing workload by performing the following tasks in a SQL Server test environment:
285+
Determining the appropriate size for `tempdb` in a [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] production environment depends on many factors. As described earlier, these factors include the existing workload and the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] features that are used.
280286

281-
- Set autogrow on for `tempdb`.
287+
We recommend that you analyze the existing workload by performing the following tasks in a SQL Server test environment:
288+
289+
- Set [autogrow on](../../t-sql/statements/alter-database-transact-sql-file-and-filegroup-options.md) for `tempdb`.
282290
- Run individual queries or workload trace files and monitor `tempdb` space use.
283291
- Execute index maintenance operations such as rebuilding indexes, and monitor `tempdb` space.
284292
- Use the space-use values from the previous steps to predict your total workload usage. Adjust this value for projected concurrent activity, and then set the size of `tempdb` accordingly.
285293

286294
## <a id="monitoring-tempdb-use"></a> Monitor tempdb use
287295

288-
Running out of disk space in `tempdb` can cause significant disruptions in the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] production environment. It can also prevent applications that are running from completing operations. You can use the [sys.dm_db_file_space_usage](../../relational-databases/system-dynamic-management-views/sys-dm-db-file-space-usage-transact-sql.md) dynamic management view to monitor the disk space that's used in the `tempdb` files:
296+
Running out of disk space in `tempdb` can cause significant disruptions in the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] production environment. It can also prevent applications that are running from completing operations. You can use the [sys.dm_db_file_space_usage](../../relational-databases/system-dynamic-management-views/sys-dm-db-file-space-usage-transact-sql.md) dynamic management view to monitor the disk space that's used in the `tempdb` files.
297+
298+
For example, the following four sample scripts find the amount of free space in `tempdb`, the amount of space used by the version store, the amount of space used by internal objects, and amount of space used by user objects:
289299

290300
```sql
291301
-- Determining the amount of free space in tempdb
@@ -311,14 +321,20 @@ FROM tempdb.sys.dm_db_file_space_usage;
311321

312322
To monitor the page allocation or deallocation activity in `tempdb` at the session or task level, you can use the [sys.dm_db_session_space_usage](../../relational-databases/system-dynamic-management-views/sys-dm-db-session-space-usage-transact-sql.md) and [sys.dm_db_task_space_usage](../../relational-databases/system-dynamic-management-views/sys-dm-db-task-space-usage-transact-sql.md) dynamic management views. These views can help you identify large queries, temporary tables, or table variables that are using lots of `tempdb` disk space. You can also use several counters to monitor the free space that's available in `tempdb` and the resources that are using `tempdb`.
313323

324+
For example, use the following script to obtaining the `tempdb` space consumed by internal objects in all currently running tasks in each session:
325+
314326
```sql
315327
-- Obtaining the space consumed by internal objects in all currently running tasks in each session
316328
SELECT session_id,
317329
SUM(internal_objects_alloc_page_count) AS task_internal_objects_alloc_page_count,
318330
SUM(internal_objects_dealloc_page_count) AS task_internal_objects_dealloc_page_count
319331
FROM sys.dm_db_task_space_usage
320332
GROUP BY session_id;
333+
```
321334

335+
Use the following script to find the `tempdb` space consumed by internal objects in the current session, for both running and completed tasks:
336+
337+
```sql
322338
-- Obtaining the space consumed by internal objects in the current session for both running and completed tasks
323339
SELECT R2.session_id,
324340
R1.internal_objects_alloc_page_count
@@ -338,3 +354,4 @@ GROUP BY R2.session_id, R1.internal_objects_alloc_page_count,
338354
- [sys.databases](../../relational-databases/system-catalog-views/sys-databases-transact-sql.md)
339355
- [sys.master_files](../../relational-databases/system-catalog-views/sys-master-files-transact-sql.md)
340356
- [Move database files](../../relational-databases/databases/move-database-files.md)
357+
-

0 commit comments

Comments
 (0)