microsoft / microsoft/sqlmanagementobjects
Severe performance degradation in SQL Server 2022 when SMO enumerates databases with ActiveConnections due to sys.sysprocesses query
Nessuno ha ancora preso questa issue.
- Lingua principale
- C#
- Stelle
- 143
- Fork
- 28
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Hello,
We've had to add a workaround for an issue that was introduced in 2022.
Please fix and we will update our codebase.
https://github.com/dataplat/dbatools/pull/9908
Description
SQL Server 2022 has severe performance degradation when SMO enumerates databases with ActiveConnections in the default init fields. The same query that takes ~1 second on SQL Server 2016 takes 20+ seconds on SQL Server 2022.
Root Cause
The generated query includes:
(select count(*) from master.dbo.sysprocesses p where dtb.database_id=p.dbid) AS [ActiveConnections]
This subquery for ActiveConnections uses the deprecated sys.sysprocesses compatibility view, which has severe performance issues on SQL Server 2022, particularly when querying hundreds of databases.
Workaround Found
Setting the legacy cardinality estimator on the master database reduces query time from 20s to 1s, suggesting this is a query optimizer regression in SQL Server 2022.
Environment
- SQL Server Version: SQL Server 2022 (VersionMajor 16)
- SMO Version: Multiple versions tested (including 17.100.23.0)
- Number of Databases: 190+ databases significantly amplifies the issue
- Query Execution: Each database enumeration includes this subquery, causing exponential slowdown
Proposed Solutions
- Short-term: Update SMO to use
sys.dm_exec_sessionsinstead ofsys.sysprocesses - Medium-term: Fix the cardinality estimation issue for
sys.sysprocessesqueries in SQL Server 2022 - Long-term: Remove
sys.sysprocessesentirely as it's been deprecated since SQL Server 2008
Reproduction
$server = New-Object Microsoft.SqlServer.Management.Smo.Server "SQL2022Instance"
$initFields = New-Object System.Collections.Specialized.StringCollection
[void]$initFields.AddRange(@('ActiveConnections'))
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database], $initFields)
# This will be very slow on SQL 2022 with 100+ databases
Measure-Command { $server.Databases | Out-Null }
Impact
- Affects all SMO-based tooling (SSMS, Azure Data Studio, dbatools, custom scripts)
- Only manifests when user has
VIEW SERVER STATEpermission - Severely impacts automation and monitoring tools
- Issue affects SQL Server 2022 RTM and remains after applying latest CUs
References
- Original issue: dataplat/dbatools#9282
- Deprecated view documentation: https://learn.microsoft.com/en-us/sql/relational-databases/system-compatibility-views/sys-sysprocesses-transact-sql
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione in PowerShell usando Server.SetDefaultInitFields per l’enumerazione di ActiveConnections e Databases, quindi traccia dove viene assemblata la query ActiveConnections generata. Confronta la query sys.sysprocesses corrente con l’alternativa proposta su SQL Server 2022 e convalida il completamento confermando che l’enumerazione dei database non mostra più il rallentamento segnalato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp, powershell, sql
- Ambito
- databases
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100