Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 
 

Display SQL Server session hogs

SQL Server Tips by Donald Burleson


This is one of the many SQL Server scripts to display SQL Server session details from the book "High Performance SQL Server DBA".

SQL Server Session Workload Analysis

It has been correctly stated that databases would perform just fine if no users were ever allowed to log into them.  Alas, this is never the case, so the SQL Server DBA will need to be able to identify the workload hogs on the server and figure out why they are using more resources than they should.  A general rule of thumb is: if any user session is found using more than 25% of any resource (CPU, I/O, etc.), their SQL and usage patterns should be examined in more detail. 

To start, it is a good idea to get a quick overview of how many sessions have been logged into SQL Server, along with counts of how many are active and inactive.  The sess_count query below will accomplish this task:

<    session_count.sql

select  active_processes =
        (select
            count(*)
        from
            master..sysprocesses
        where
            status = 'runnable'),
        inactive_processes =
        (select
            count(*)
        from
            master..sysprocesses
        where
            status <> 'runnable'),
        system_processes =
        (select
            count(*)
        from
            master..sysprocesses) -
See code depot for full script
            master..sysprocesses
           where
            (program_name is not null  and
            program_name not like 'SQLAgent%' and
            program_name <> ''))

The results will look like:

active_processes    inactive_processes     system_processes
----------------    ------------------     ----------------
1                   25                     15          

From there, the DBA should pinpoint sessions using the lion's share of resources, as it is not uncommon for 10% of the session base to be causing severe pain for the other 90% of those trying to use SQL Server.  There are two ways to view this type of data.  A good way to accomplish this is to break out session usage by percentage, making it impossible for session hogs to escape notice.  The following procedure, up_wl_session_hogs , makes this easy to accomplish:

<    up_wl_session_hogs

 

IF OBJECT_ID('up_wl_session_hogs') IS NOT NULL
BEGIN
    DROP PROCEDURE up_wl_session_hogs
    IF OBJECT_ID('up_wl_session_hogs') IS NOT NULL
        PRINT '<<< FAILED DROPPING PROCEDURE up_wl_session_hogs >>>'
    ELSE
        PRINT '<<< DROPPED PROCEDURE up_wl_session_hogs >>>'
END
go
create procedure up_wl_session_hogs
AS
    set nocount on
  
    create table #sess_hogs (loadtype varchar(50) NULL,
                            spid int NULL,
                            username sysname NULL,
                            pctused decimal(5,2) NULL)

    insert into #sess_hogs
    select
        top 1 'Top I/O Process',
        spid,
        user_name = suser_sname(sid),
        pct_io_used = convert(decimal(17,2),(100 * (convert(decimal(17,2),physical_io) /
        (select
            convert(decimal(17,2),case sum(physical_io) when 0 then 1
else sum(physical_io) end)
         from
            master..sysprocesses))))
    from
        master..sysprocesses
    order by
        4 desc
   
    insert into #sess_hogs
    select
        top 1 'Top CPU Process',
        spid,
        user_name = suser_sname(sid),
        pct_cpu_used = convert(decimal(17,2),(100 * (convert(decimal(17,2), cpu) /
        (select
            convert(decimal(17,2),case sum(cpu) when 0 then 1 else sum(cpu) end)
         from
            master..sysprocesses))))
    from
        master..sysprocesses
    order by
        4 desc
   
    insert into #sess_hogs
    select
        top 1 'Top Memory Process',
        spid,
        user_name = suser_sname(sid),
        pct_mem_used = convert(decimal(17,2),(100 * (convert(decimal(17,2),memusage) /
        (select
            convert(decimal(17,2),case sum(memusage) when 0 then 1
else sum(memusage) end)
         from
            master..sysprocesses))))
    from
        master..sysprocesses
    order by
        4 desc
   
    insert into #sess_hogs
    select
        top 1 'Top Transaction Process',
        spid,
        user_name = suser_sname(sid),
        pct_tran_used = isnull(convert(decimal(17,2),
(100 * (convert(decimal(17,2),open_tran) /
        (select
            case when convert(decimal(17,2),sum(open_tran)) = 0 then 1 end
         from
            master..sysprocesses)))),0)
    from
        master..sysprocesses
    order by
        4 desc

    select
        loadtype,
        spid,
        username,
        pctused
See code depot for full script

    drop table #sess_hogs
go
IF OBJECT_ID('up_wl_session_hogs') IS NOT NULL
    PRINT '<<< CREATED PROCEDURE up_wl_session_hogs >>>'
ELSE
    PRINT '<<< FAILED CREATING PROCEDURE up_wl_session_hogs >>>'
go

 

Figure 7.5 is a representation of the query looking for session resource hogs.

Figure 7.5:  Finding Session Hogs.

Sessions on dynamic systems having accumulated more than 25% of one resource are candidates for further examination.  Of course, large batch job processes that come and go at predefined intervals may not be a problem, but consistently connected sessions exhibiting high resource usage are another matter. 

Of course, with SQL Server, it is easy to get the standard session analysis that gives a quick overview of what each session is doing.  The sess_activity query provides everything needed to acquire more details on each process connected to SQL Server:

          session_activity

 

 
select
    spid,
    loginame = rtrim(loginame),
    windows_user = rtrim(nt_username),
    database_name = db_name(dbid),
    status = status,
    program_name = rtrim(program_name),
    mem_bytes = memusage * 8,
    cpu,
    physical_io,
    blocked =
See code depot for full script
    command = cmd,
    login_time,
    last_batch,
    hostprocess,
     net_address
from
    master..sysprocesses
order by
    1

 

This is one of the many SQL Server scripts to display SQL Server session details from the book "High Performance SQL Server DBA".


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational