IT story

MS SQL Server 2005에서 열린 / 활성 연결의 총 수를 확인하는 방법

hot-time 2020. 9. 10. 19:01
반응형

MS SQL Server 2005에서 열린 / 활성 연결의 총 수를 확인하는 방법


내 PHP / MS Sql Server 2005 / win 2003 응용 프로그램이 때때로 매우 응답하지 않고 메모리 / cpu 사용량이 급증하지 않습니다. SQL Management Studio에서 새 연결을 열려고하면 연결 열기 대화 상자에서 중단됩니다. 총 활성 연결 수를 결정하는 방법 ms SQL Server 2005


이는 각 DB 당 연결 수를 보여줍니다.

SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NumberOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
    dbid > 0
GROUP BY 
    dbid, loginame

그리고 이것은 총계를 제공합니다.

SELECT 
    COUNT(dbid) as TotalConnections
FROM
    sys.sysprocesses
WHERE 
    dbid > 0

더 자세한 정보가 필요하면 다음을 실행하십시오.

sp_who2 'Active'

참고 : 사용 되는 SQL Server 계정에는 'sysadmin'역할이 필요합니다 (그렇지 않으면 결과로 단일 행과 1의 개수 만 표시됨).


@jwalkerjr가 언급했듯이 코드에서 연결을 폐기해야합니다 (연결 풀링이 활성화 된 경우 연결 풀로 반환 됨). 이를 수행하는 규정 된 방법은 ' using'문을 사용하는 것입니다 .

// Execute stored proc to read data from repository
using (SqlConnection conn = new SqlConnection(this.connectionString))
{
    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "LoadFromRepository";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", fileID);

        conn.Open();
        using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            if (rdr.Read())
            {
                filename = SaveToFileSystem(rdr, folderfilepath);
            }
        }
    }
}

나는 이것이 오래되었다는 것을 알고 있지만 업데이트하는 것이 좋은 생각이라고 생각했습니다. 정확한 개수가 필요한 경우 ECID 열도 필터링해야합니다. 병렬 스레드가있는 SPID는 sysprocesses에서 여러 번 표시 될 수 있으며 ECID = 0을 필터링하면 각 SPID에 대한 기본 스레드가 반환됩니다.

SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NumberOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses with (nolock)
WHERE 
    dbid > 0
    and ecid=0
GROUP BY 
    dbid, loginame

이를 사용하여 각 연결 풀에 대한 정확한 개수를 가져옵니다 (각 사용자 / 호스트 프로세스가 동일한 연결 문자열을 사용한다고 가정).

SELECT 
DB_NAME(dbid) as DBName, 
COUNT(dbid) as NumberOfConnections,
loginame as LoginName, hostname, hostprocess
FROM
sys.sysprocesses with (nolock)
WHERE 
dbid > 0
GROUP BY 
dbid, loginame, hostname, hostprocess

If your PHP app is holding open many SQL Server connections, then, as you may know, you have a problem with your app's database code. It should be releasing/disposing those connections after use and using connection pooling. Have a look here for a decent article on the topic...

http://www.c-sharpcorner.com/UploadFile/dsdaf/ConnPooling07262006093645AM/ConnPooling.aspx


see sp_who it gives you more details than just seeing the number of connections

in your case i would do something like this

 DECLARE @temp TABLE(spid int , ecid int, status varchar(50),
                     loginname varchar(50),   
                     hostname varchar(50),
blk varchar(50), dbname varchar(50), cmd varchar(50), request_id int) 
INSERT INTO @temp  

EXEC sp_who

SELECT COUNT(*) FROM @temp WHERE dbname = 'DB NAME'

MS SQL knowledge based - How to know open SQL database connection(s) and occupied on which host.

Using below query you will find list database, Host name and total number of open connection count, based on that you will have idea, which host has occupied SQL connection.

SELECT DB_NAME(dbid) as DBName, hostname ,COUNT(dbid) as NumberOfConnections
FROM sys.sysprocesses with (nolock) 
WHERE dbid > 0 
and len(hostname) > 0 
--and DB_NAME(dbid)='master' /* Open this line to filter Database by Name */
Group by DB_NAME(dbid),hostname
order by DBName

참고URL : https://stackoverflow.com/questions/216007/how-to-determine-total-number-of-open-active-connections-in-ms-sql-server-2005

반응형