sqlserver內存、會話、連接查詢


1、連接查詢

select * from sysprocesses where dbid in (select dbid from sysdatabases where name='dbname')
--或者
SELECT * FROM
[Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT 
   [DBID]
FROM 
   [Master].[dbo].[SYSDATABASES]
WHERE 
   NAME='dbname'
)

 

 2、當前正在執行的sql查詢

SELECT  [Spid] = session_id ,
            ecid ,
            [Database] = DB_NAME(sp.dbid) ,
            [User] = nt_username ,
            [Status] = er.status ,
            [Wait] = wait_type ,
            [Individual Query] = SUBSTRING(qt.text,
                                           er.statement_start_offset / 2,
                                           ( CASE WHEN er.statement_end_offset = -1
                                                  THEN LEN(CONVERT(NVARCHAR(MAX), qt.text))
                                                       * 2
                                                  ELSE er.statement_end_offset
                                             END - er.statement_start_offset )
                                           / 2) ,
            [Parent Query] = qt.text ,
            Program = program_name ,
            hostname ,
            nt_domain ,
            start_time
    FROM    sys.dm_exec_requests er
            INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
            CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt
    WHERE   session_id > 50 -- Ignore system spids.
            AND session_id NOT IN ( @@SPID ) -- Ignore this current statement.
ORDER BY    1 ,

 

 

清理存儲過程預編譯等緩存、會話緩存、系統緩存、緩存區 

 DBCC FREEPROCCACHE 
 DBCC FREESESSIONCACHE 
 DBCC FREESYSTEMCACHE('All') 
 DBCC DROPCLEANBUFFERS 

 

--內存使用情況     
SELECT * FROM sys.dm_os_performance_counters 
WHERE counter_name IN ('Target Server Memory (KB)','Total Server Memory (KB)') 
 
-- 內存狀態 
DBCC MemoryStatus 
 
 
 
 
 
 
--查看最小最大內存 
SELECT 
cfg.name AS [Name], 
cfg.configuration_id AS [Number], 
cfg.minimum AS [Minimum], 
cfg.maximum AS [Maximum], 
cfg.is_dynamic AS [Dynamic], 
cfg.is_advanced AS [Advanced], 
cfg.value AS [ConfigValue], 
cfg.value_in_use AS [RunValue], 
cfg.description AS [Description] 
FROM 
sys.configurations AS cfg 
 
--設置最小最大內存 
 
 
 
sp_configure 'show advanced options', 1 
 
go 
sp_configure 'min server memory', 0 
RECONFIGURE 
GO 
 
sp_configure 'max server memory', 2147483647 
RECONFIGURE 
GO 
 
sp_configure 'max server memory', 256 
RECONFIGURE 
GO 
sp_configure 'show advanced options', 0 

 

 

CREATE proc [dbo].reclaimmemory  --強制釋放內存
    
    
as

begin
 
 DBCC FREEPROCCACHE 
 DBCC FREESESSIONCACHE 
 DBCC FREESYSTEMCACHE('All') 
 DBCC DROPCLEANBUFFERS

 

exec sp_configure 'max server memory', 256 
EXEC ('RECONFIGURE' )

WAITFOR DELAY '00:00:05'

EXEC  sp_configure 'max server memory', 2147483647 
EXEC ('RECONFIGURE' )
GO

 


end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM