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
Returns summary level attributes and information on transaction log files of databases. Use this information for monitoring and diagnostics of transaction log health.
Is the ID of the database. `database_id` is `int`. Valid inputs are the ID number of a database, `NULL`, or `DEFAULT`. The default is `NULL`. `NULL` and `DEFAULT` are equivalent values in the context of current database.
42
-
The built-in function [DB_ID](../../t-sql/functions/db-id-transact-sql.md) can be specified. When using `DB_ID` without specifying a database name, the compatibility level of the current database must be 90 or greater.
|database_id |**int**|Database ID. <br /><br />In [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], the values are unique within a single database or an elastic pool, but not within a logical server.|
50
-
|recovery_model |**nvarchar(60)**| Recovery model of the database. Possible values include: <br /> SIMPLE<br /> BULK_LOGGED <br /> FULL |
51
-
|log_min_lsn |**nvarchar(24)**| Current start [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch) in the transaction log.|
52
-
|log_end_lsn |**nvarchar(24)**|[log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch) of the last log record in the transaction log.|
53
-
|current_vlf_sequence_number |**bigint**| Current [virtual log file (VLF)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#physical_arch) sequence number at the time of execution.|
54
-
|current_vlf_size_mb |**float**| Current [virtual log file (VLF)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#physical_arch) size in MB.|
55
-
|total_vlf_count |**bigint**| Total number of [virtual log files (VLFs)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#physical_arch) in the transaction log. |
56
-
|total_log_size_mb |**float**| Total transaction log size in MB. |
57
-
|active_vlf_count |**bigint**| Total number of active [virtual log files (VLFs)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#physical_arch) in the transaction log.|
58
-
|active_log_size_mb |**float**| Total active transaction log size in MB.|
59
-
|log_truncation_holdup_reason |**nvarchar(60)**| Log truncation holdup reason. The value is same as `log_reuse_wait_desc` column of `sys.databases`. (For more detailed explanations of these values, see [The Transaction Log](../../relational-databases/logs/the-transaction-log-sql-server.md)). <br />Possible values include: <br />NOTHING<br />CHECKPOINT<br />LOG_BACKUP<br />ACTIVE_BACKUP_OR_RESTORE<br />ACTIVE_TRANSACTION<br />DATABASE_MIRRORING<br />REPLICATION<br />DATABASE_SNAPSHOT_CREATION<br />LOG_SCAN<br />AVAILABILITY_REPLICA<br />OLDEST_PAGE<br />XTP_CHECKPOINT<br />OTHER TRANSIENT |
60
-
|log_backup_time |**datetime**| Last transaction log backup time.|
61
-
|log_backup_lsn |**nvarchar(24)**| Last transaction log backup [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch).|
62
-
|log_since_last_log_backup_mb |**float**| Log size in MB since last transaction log backup [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch).|
63
-
|log_checkpoint_lsn |**nvarchar(24)**| Last checkpoint [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch).|
64
-
|log_since_last_checkpoint_mb |**float**| Log size in MB since last checkpoint [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch).|
65
-
|log_recovery_lsn |**nvarchar(24)**| Recovery [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch) of the database. If `log_recovery_lsn` occurs before the checkpoint LSN, `log_recovery_lsn` is the oldest active transaction LSN, otherwise `log_recovery_lsn` is the checkpoint LSN.|
66
-
|log_recovery_size_mb |**float**| Log size in MB since log recovery [log sequence number (LSN)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#Logical_Arch).|
67
-
|recovery_vlf_count |**bigint**| Total number of [virtual log files (VLFs)](../../relational-databases/sql-server-transaction-log-architecture-and-management-guide.md#physical_arch) to be recovered, if there was failover or server restart. |
The `sys.dm_db_log_stats` dynamic management function returns summary level attributes and information on transaction log files of databases. Use this information for monitoring and diagnostics of transaction log health.
The ID of the database. *database_id* is **int**. Valid inputs are the ID number of a database, `NULL`, or `DEFAULT`. The default value is `NULL`. `NULL` and `DEFAULT` are equivalent values in the context of current database.
42
+
43
+
You can also specify the built-in function [DB_ID](../../t-sql/functions/db-id-transact-sql.md) for *database_id*.
44
+
45
+
## Tables returned
46
+
47
+
| Column name | Data type | Description |
48
+
| --- | --- | --- |
49
+
|`database_id`|**int**| Database ID.<br /><br />In [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], the values are unique within a single database or an elastic pool, but not within a logical server. |
50
+
|`recovery_model`|**nvarchar(60)**| Recovery model of the database. Possible values include:<br /><br />`SIMPLE`<br />`BULK_LOGGED`<br />`FULL`|
51
+
|`log_min_lsn` <sup>1</sup> |**nvarchar(24)**| Current start log sequence number (LSN) in the transaction log. |
52
+
|`log_end_lsn` <sup>1</sup> |**nvarchar(24)**| log sequence number (LSN) of the last log record in the transaction log. |
53
+
|`current_vlf_sequence_number` <sup>2</sup> |**bigint**| Current virtual log file (VLF) sequence number at the time of execution. |
54
+
|`current_vlf_size_mb` <sup>2</sup> |**float**| Current virtual log file (VLF) size in MB. |
55
+
|`total_vlf_count` <sup>2</sup> |**bigint**| Total number of virtual log files (VLFs) in the transaction log. |
56
+
|`total_log_size_mb`|**float**| Total transaction log size in MB. |
57
+
|`active_vlf_count` <sup>2</sup> |**bigint**| Total number of active virtual log files (VLFs) in the transaction log. |
58
+
|`active_log_size_mb`|**float**| Total active transaction log size in MB. |
59
+
|`log_truncation_holdup_reason`|**nvarchar(60)**| Log truncation holdup reason. The value is same as `log_reuse_wait_desc` column of `sys.databases`. (For more detailed explanations of these values, see [The transaction log](../logs/the-transaction-log-sql-server.md)).<br />Possible values include:<br /><br />`NOTHING`<br />`CHECKPOINT`<br />`LOG_BACKUP`<br />`ACTIVE_BACKUP_OR_RESTORE`<br />`ACTIVE_TRANSACTION`<br />`DATABASE_MIRRORING`<br />`REPLICATION`<br />`DATABASE_SNAPSHOT_CREATION`<br />`LOG_SCAN`<br />`AVAILABILITY_REPLICA`<br />`OLDEST_PAGE`<br />`XTP_CHECKPOINT`<br />`OTHER TRANSIENT`|
60
+
|`log_backup_time`|**datetime**| Last transaction log backup start time. |
61
+
|`log_backup_lsn` <sup>1</sup> |**nvarchar(24)**| Last transaction log backup log sequence number (LSN). |
62
+
|`log_since_last_log_backup_mb` <sup>1</sup> |**float**| Log size in MB since last transaction log backup log sequence number (LSN). |
63
+
|`log_checkpoint_lsn` <sup>1</sup> |**nvarchar(24)**| Last checkpoint log sequence number (LSN). |
64
+
|`log_since_last_checkpoint_mb` <sup>1</sup> |**float**| Log size in MB since last checkpoint log sequence number (LSN). |
65
+
|`log_recovery_lsn` <sup>1</sup> |**nvarchar(24)**| Recovery log sequence number (LSN) of the database. If `log_recovery_lsn` occurs before the checkpoint LSN, `log_recovery_lsn` is the oldest active transaction LSN, otherwise `log_recovery_lsn` is the checkpoint LSN. |
66
+
|`log_recovery_size_mb` <sup>1</sup> |**float**| Log size in MB since log recovery log sequence number (LSN). |
67
+
|`recovery_vlf_count` <sup>2</sup> |**bigint**| Total number of virtual log files (VLFs) to be recovered, if there was failover or server restart. |
68
+
69
+
<sup>1</sup> For more information, see [log sequence number (LSN)](../sql-server-transaction-log-architecture-and-management-guide.md#transaction-log-logical-architecture).
70
+
71
+
<sup>2</sup> For more information, see [virtual log file (VLF)](../sql-server-transaction-log-architecture-and-management-guide.md#transaction-log-physical-architecture).
69
72
70
73
## Remarks
71
-
When running `sys.dm_db_log_stats` against a database that is participating in an Availability Group as a secondary replica, only a subset of the fields described above will be returned. Currently, only `database_id`, `recovery_model`, and `log_backup_time` will be returned when run against a secondary database.
72
74
73
-
## Permissions
74
-
Requires the `VIEW SERVER STATE` permission in the database.
75
-
76
-
### Permissions for SQL Server 2022 and later
75
+
When you run `sys.dm_db_log_stats` against a database that's participating in an availability group as a secondary replica, the query returns only a subset of the fields described in the previous table. Currently, the query returns only `database_id`, `recovery_model`, and `log_backup_time` when run against a secondary database.
76
+
77
+
## Permissions
77
78
78
-
Requires VIEW SERVER PERFORMANCE STATE permission on the server.
79
+
[!INCLUDE [sssql19-md](../../includes/sssql19-md.md)] and earlier versions require the `VIEW SERVER STATE` permission on the server.
79
80
80
-
## Examples
81
+
[!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and later versions require the `VIEW SERVER PERFORMANCE STATE` permission on the server.
82
+
83
+
## Examples
84
+
85
+
### A. Determine databases in a SQL Server instance with high number of VLFs
81
86
82
-
### A. Determining databases in a [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] instance with high number of VLFs
83
87
The following query returns the databases with more than 100 VLFs in the log files. Large numbers of VLFs can affect the database startup, restore, and recovery time.
84
88
85
-
```sql
86
-
SELECT name AS'Database Name', total_vlf_count AS'VLF count'
89
+
```sql
90
+
SELECT name AS'Database Name',
91
+
total_vlf_count AS'VLF count'
87
92
FROMsys.databasesAS s
88
-
CROSS APPLY sys.dm_db_log_stats(s.database_id)
89
-
WHERE total_vlf_count >100;
90
-
```
93
+
CROSS APPLY sys.dm_db_log_stats(s.database_id)
94
+
WHERE total_vlf_count >100;
95
+
```
96
+
97
+
### B. Determine databases in a SQL Server instance with transaction log backups older than 4 hours
91
98
92
-
### B. Determining databases in a [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] instance with transaction log backups older than 4 hours
93
-
The following query determines the last log backup times for the databases in the instance.
99
+
The following query determines the last log backup start times for the databases in the instance.
94
100
95
-
```sql
96
-
SELECT name AS'Database Name', log_backup_time AS'last log backup time'
101
+
```sql
102
+
SELECT name AS'Database Name',
103
+
log_backup_time AS'last log backup start time'
97
104
FROMsys.databasesAS s
98
-
CROSS APPLY sys.dm_db_log_stats(s.database_id);
105
+
CROSS APPLY sys.dm_db_log_stats(s.database_id);
99
106
```
100
107
101
-
## See Also
102
-
[Dynamic Management Views and Functions (Transact-SQL)](../../relational-databases/system-dynamic-management-views/system-dynamic-management-views.md)
103
-
[Database Related Dynamic Management Views (Transact-SQL)](../../relational-databases/system-dynamic-management-views/database-related-dynamic-management-views-transact-sql.md)
0 commit comments