为什么长时间运行的查询的连接数会不断增加?

时间:2015-12-01 09:00:27

标签: sql-server database database-administration

我正在使用此查询查看当前连接:

select  loginame , nt_username, hostprocess, program_name, cmd, status, count(*) 'Connections count'
from master..sysprocesses
where spid > 50 and spid != @@spid
group by  loginame , nt_username, dbid, hostprocess, program_name, cmd, status
order by loginame, count(*) desc;

每当我运行一个需要很长时间的查询(例如,来自大表的count(1),需要40多秒)时,连接数会不断增加。结果,最大 达到连接限制(EF默认为100),对我网站的请求超时。异常消息如下所示:

Exception Message: The underlying provider failed on Open.
InnerException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.

为什么会这样?

例如,如果我在中运行以下查询 sql management studio:

select COUNT(1)
FROM tableA ss
INNER  JOIN tableB s
ON s.Id = ss.tableAId      
WHERE s.Time < '2015-10-01';

这就是我看到运行上一个查询的内容:

Connections-count from mgmt studio

这里显示了30个连接;但它不断增加。

1 个答案:

答案 0 :(得分:0)

使用该查询,您将每个并行流计为连接,如果您只是将它们列出而没有聚合它们,您将看到许多并行查询执行,其中SPID是相同的,并且ECID&gt; 0.使用COUNT(DISTINCT spid)或将AND ECID = 0添加到查询中。