Check VLF Counts for Every SQL Server Database

VLF (virtual log file) count per database using sys.dm_db_log_info, which replaces DBCC LOGINFO from SQL Server 2016 SP2. No temp tables or sp_MSforeachdb needed.

-- High VLF counts can affect write performance
-- and they can make full database restores and crash recovery take much longer.
-- Try to keep VLF counts under 200 in most cases (depending on log file size).
-- sys.dm_db_log_info needs SQL Server 2016 SP2 or later; no DBCC LOGINFO, no temp tables.
SELECT d.name   AS DatabaseName,
       COUNT(*) AS VLFCount
FROM sys.databases AS d
CROSS APPLY sys.dm_db_log_info(d.database_id) AS li
WHERE d.state_desc = 'ONLINE'
GROUP BY d.name
HAVING COUNT(*) >= 50 -- over 200 VLFs is bad
ORDER BY VLFCount DESC;