microsoft / microsoft/sqlmanagementobjects

Severe performance degradation in SQL Server 2022 when SMO enumerates databases with ActiveConnections due to sys.sysprocesses query

Open
#210 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
143
Forks
28
PR merge metrics
No merged PRs in 30d

Description

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
  1. Short-term: Update SMO to use sys.dm_exec_sessions instead of sys.sysprocesses
  2. Medium-term: Fix the cardinality estimation issue for sys.sysprocesses queries in SQL Server 2022
  3. Long-term: Remove sys.sysprocesses entirely 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 STATE permission
  • Severely impacts automation and monitoring tools
  • Issue affects SQL Server 2022 RTM and remains after applying latest CUs
References

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the PowerShell reproduction using Server.SetDefaultInitFields for ActiveConnections and Databases enumeration, then trace where the generated ActiveConnections query is assembled. Compare the current sys.sysprocesses query with the proposed alternative on SQL Server 2022, and validate completion by confirming database enumeration no longer shows the reported slowdown.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.