microsoft / microsoft/sqlmanagementobjects

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

Đang mở
#210 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
C#
Star
143
Fork
28
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc tái hiện trong PowerShell sử dụng Server.SetDefaultInitFields cho việc liệt kê ActiveConnections và Databases, sau đó truy vết nơi truy vấn ActiveConnections được tạo ra được lắp ráp. So sánh truy vấn sys.sysprocesses hiện tại với phương án thay thế được đề xuất trên SQL Server 2022, và xác nhận hoàn tất bằng cách kiểm tra rằng việc liệt kê cơ sở dữ liệu không còn cho thấy tình trạng chậm đã được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp, powershell, sql
Lĩnh vực
databases
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.