You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/relational-databases/databases/tempdb-database.md
+30-13Lines changed: 30 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: This article provides details about the configuration and use of th
4
4
author: WilliamDAssafMSFT
5
5
ms.author: wiassaf
6
6
ms.reviewer: randolphwest
7
-
ms.date: 04/05/2023
7
+
ms.date: 08/07/2023
8
8
ms.service: sql
9
9
ms.topic: conceptual
10
10
ms.custom: P360
@@ -47,9 +47,9 @@ The following table lists the initial configuration values of the `tempdb` data
47
47
48
48
| File | Logical name | Physical name | Initial size | File growth |
49
49
| --- | --- | --- | --- | --- |
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 |
53
53
54
54
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.
55
55
@@ -101,7 +101,7 @@ For a description of these database options, see [ALTER DATABASE SET Options (Tr
101
101
102
102
## tempdb in Azure SQL
103
103
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.
105
105
106
106
### tempdb in SQL Database
107
107
@@ -116,7 +116,9 @@ To learn more about `tempdb` sizes in Azure SQL Database, review:
116
116
117
117
### tempdb in SQL Managed Instance
118
118
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).
120
122
121
123
To learn more about `tempdb` sizes in Azure SQL Managed Instance, review [resource limits](/azure/azure-sql/managed-instance/resource-limits).
122
124
@@ -154,7 +156,7 @@ Preallocate space for all `tempdb` files by setting the file size to a value lar
154
156
155
157
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`.
156
158
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.
158
160
159
161
To check current size and growth parameters for `tempdb`, use the following query:
160
162
@@ -186,9 +188,12 @@ Put the `tempdb` database on a fast I/O subsystem. Use disk striping if there ar
186
188
187
189
Put the `tempdb` database on disks that differ from the disks that user databases use.
188
190
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
+
189
194
## Performance improvements in tempdb for SQL Server
190
195
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:
192
197
193
198
- 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.
194
199
- 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
197
202
- 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.
198
203
- 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.
199
204
- 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).
200
206
201
207
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).
202
208
@@ -243,7 +249,7 @@ This change also requires a restart to take effect, even if memory-optimized `te
243
249
244
250
- 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.
245
251
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:
247
253
248
254
```output
249
255
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
263
269
COMMIT TRAN;
264
270
```
265
271
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.
267
273
268
274
-[Columnstore indexes](../indexes/columnstore-indexes-overview.md) can't be created on temporary tables when memory-optimized `tempdb` metadata is enabled.
269
275
@@ -276,16 +282,20 @@ This change also requires a restart to take effect, even if memory-optimized `te
276
282
277
283
## Capacity planning for tempdb in SQL Server
278
284
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.
280
286
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`.
282
290
- Run individual queries or workload trace files and monitor `tempdb` space use.
283
291
- Execute index maintenance operations such as rebuilding indexes, and monitor `tempdb` space.
284
292
- 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.
285
293
286
294
## <aid="monitoring-tempdb-use"></a> Monitor tempdb use
287
295
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:
289
299
290
300
```sql
291
301
-- Determining the amount of free space in tempdb
@@ -311,14 +321,20 @@ FROM tempdb.sys.dm_db_file_space_usage;
311
321
312
322
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`.
313
323
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
+
314
326
```sql
315
327
-- Obtaining the space consumed by internal objects in all currently running tasks in each session
316
328
SELECT session_id,
317
329
SUM(internal_objects_alloc_page_count) AS task_internal_objects_alloc_page_count,
318
330
SUM(internal_objects_dealloc_page_count) AS task_internal_objects_dealloc_page_count
319
331
FROMsys.dm_db_task_space_usage
320
332
GROUP BY session_id;
333
+
```
321
334
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
322
338
-- Obtaining the space consumed by internal objects in the current session for both running and completed tasks
323
339
SELECTR2.session_id,
324
340
R1.internal_objects_alloc_page_count
@@ -338,3 +354,4 @@ GROUP BY R2.session_id, R1.internal_objects_alloc_page_count,
0 commit comments