C# DB query app causes the DB server to stop responding: What tools can I use on the DB server to identify and study the choke point? 数据库服务器停止响应

C# DB query app causes the DB server to stop responding: What tools can I use on the DB server to identify and study the choke point?

我根据 Throttling asynchronous tasks 的公认答案编写了一个程序。请参阅下面的代码。我的程序本质上是一个临时数据库 "text search" 工具。它在 Win 7 客户端计算机上运行。该程序组成了大量 SELECT 语句(成百上千)并将它们发送到远程数据库服务器(Win 7 客户端和数据库服务器在同一个 A.D. 域中。搜索到的数据库服务器仅是 Always On Availability Group 中的 read-only secondary 服务器。 Download 方法的每次调用都会为一个数据库 table 创建一对多 SELECT 查询(每个 table 列一个 SELECT 被搜索)。 Download 方法被调用一到千次(每 table 被搜索一次)。

TPL Dataflow 只要我将搜索限制在不超过 60-70 table 秒就可以正常工作。不仅如此,它还会阻塞(我认为)SQL Server Database and/or 数据库服务器机器。我试过各种 MaxDegreeOfParallelismBoundedCapacity 值以试图控制客户端。使用 MaxDegreeOfParallelism = 8(我的客户端机器上的处理器数量,我可以在数据库服务器机器上看到 CPU 和 DISK peg。使用 MaxDegreeOfParallelism = 1BoundedCapacity = 1,CPU 和 DISK 在数据库服务器机器上正常,但在提交查询的过程中,我的数据库读取代码:

SqlDataReader sqlDataReader = await sqlCommand.ExecuteReaderAsync();
dataTable_TableDataFromFieldQuery.Load(sqlDataReader);

最终抛出异常"server has become unresponsive"

我可以在数据库服务器上使用什么工具来识别阻塞点?即使假设我需要改进我的查询,我要寻找什么以及在哪里寻找?

来自另一个 S.O 的代码。为我的使用修改了问题

var downloader = new TransformBlock<string, DataTable>(tableName => Download(tableName), new ExecutionDataflowBlockOptions {MaxDegreeOfParallelism={various values 1-5, BoundedCapacity={various values 1-5} } );

var buffer = new BufferBlock<DataTable>();
downloader.LinkTo(buffer);

foreach(var tableName in tableNames)
    await downloader.SendAsync(tableName);

downloader.Complete();
await downloader.Completion;

IList<DataTable> responses;

if (buffer.TryReceiveAll(out responses))
{
    //process all DataTables
}

这听起来有点乱,我不确定你是否知道这些工具,但我认为你应该对它们非常熟悉。

第一个是SQL Server Profiler

Microsoft SQL Server Profiler is a graphical user interface to SQL Trace for monitoring an instance of the Database Engine or Analysis Services. You can capture and save data about each event to a file or table to analyze later. For example, you can monitor a production environment to see which stored procedures are affecting performance by executing too slowly

还有 Database Engine Tuning Advisor

The Microsoft Database Engine Tuning Advisor (DTA) analyzes databases and makes recommendations that you can use to optimize query performance. You can use the Database Engine Tuning Advisor to select and create an optimal set of indexes, indexed views, or table partitions without having an expert understanding of the database structure or the internals of SQL Server

Profiler,如果有策略地使用应该能够帮助缩小问题范围。 Tuning Adivsor 应该能够松散地帮助编写更好的查询。

虽然不知道你在做什么以及为什么很难给你更好的答案。但是,我的直觉可能是 locking/blocking、内存或线程过多。说实话,一个精心规划和设计的数据库应该能够承担这个工作量而不会跳过一个节拍。

最后,(不知道你的专业水平)我会在索引、查询性能、table 形式化和锁定提示方面做大量研究