From 2d05adebdcbab108882e7d8ad32c0c65a7740df6 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Sat, 1 Nov 2025 15:42:23 +0000 Subject: [PATCH] Connect-DbaInstance - Exclude ActiveConnections for SQL Server 2022+ to fix performance issue This change excludes the ActiveConnections property from the default init fields for SQL Server 2022 and above (VersionMajor >= 16) to prevent severe performance degradation when querying databases. The issue: On SQL Server 2022, SMO's query for ActiveConnections uses the deprecated sys.sysprocesses view which has significant performance problems. With 190+ databases, Get-DbaDatabase takes 4+ minutes instead of 12 seconds seen on SQL Server 2016. The fix: For SQL Server 2022+, ActiveConnections is not eagerly fetched during connection initialization. Users can still access this property, but it will be fetched on-demand per database rather than in bulk upfront. Impact: - SQL Server 2000-2019: No behavior change - SQL Server 2022+: Significantly faster database enumeration - Trade-off: Accessing ActiveConnections on SQL 2022+ will fetch per-database (slower if accessing for all databases, faster for single database queries) Fixes #9282 Co-authored-by: Chrissy LeMaire --- public/Connect-DbaInstance.ps1 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/public/Connect-DbaInstance.ps1 b/public/Connect-DbaInstance.ps1 index 66b05c534f25..6907c85e0b5a 100644 --- a/public/Connect-DbaInstance.ps1 +++ b/public/Connect-DbaInstance.ps1 @@ -1134,8 +1134,18 @@ function Connect-DbaInstance { # 2005 and 2008 [void]$initFieldsDb.AddRange($Fields200x_Db) [void]$initFieldsLogin.AddRange($Fields200x_Login) + } elseif ($server.VersionMajor -ge 16) { + # 2022 and above - exclude ActiveConnections due to performance issue #9282 + $fields = New-Object System.Collections.Specialized.StringCollection + foreach ($field in $Fields201x_Db) { + if ($field -ne "ActiveConnections") { + [void]$fields.Add($field) + } + } + [void]$initFieldsDb.AddRange($fields) + [void]$initFieldsLogin.AddRange($Fields201x_Login) } else { - # 2012 and above + # 2012 to 2019 [void]$initFieldsDb.AddRange($Fields201x_Db) [void]$initFieldsLogin.AddRange($Fields201x_Login) }