Isolation levels

  • I am trying to understand the below script to find out the long/slow running sql statement.

    http://www.sqlservercentral.com/articles/DMV/64425/

    BEGIN

    -- Do not lock anything, and do not get held up by any locks.

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    -- What SQL Statements Are Currently Running?

    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, 2

    END

    Now my question is:- is there any impact on production database, because we are using read uncommited isolation level.

  • No, the READ UNCOMMITTED is used so the query doesn't have an impact. It doesn't care about locks and places none of its own

  • BaddaBing (12/17/2009)


    No, the READ UNCOMMITTED is used so the query doesn't have an impact. It doesn't care about locks and places none of its own

    Thanks for your reply. So setting isolation level to read uncommited is for only this query?

    or do we need to change Isolation level to read commited again after executing this query

  • Depends on what you're doing. Yes, the isolation level set is just for that session / connection until you specify otherwise, it does not have an affect on other queries running in other sessions etc. The question is, are you doing anything else in that session after you run this query that needs to change the isolation level? If so, then do another SET ISOLATION LEVEL...to a level that fulfills the requirement.

  • Here is some more info of your interest.

    Query with TEXT and average CPU time for the top five queries.

    SELECT TOP 5 total_worker_time/execution_count AS [Avg CPU Time],

    SUBSTRING(st.text, (qs.statement_start_offset/2)+1,

    ((CASE qs.statement_end_offset

    WHEN -1 THEN DATALENGTH(st.text)

    ELSE qs.statement_end_offset

    END - qs.statement_start_offset)/2) + 1) AS statement_text

    FROM sys.dm_exec_query_stats AS qs

    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st

    ORDER BY total_worker_time/execution_count DESC;

    This was taken from http://msdn.microsoft.com/en-us/library/ms181929.aspx

    or a simple version such as this one to find the text of all queries with latest Query on top.

    Select s.text,* from sys.sysprocesses p

    Cross apply sys.dm_exec_sql_text (p.sql_handle) s

    where p.spid >50

    and p.spid not in (select @@spid)

    Order by p.last_batch desc

    Or

    DECLARE @spid int

    SET @spid = (SELECT top 1 SPID from master..sysprocesses order by last_batch desc)

    DBCC INPUTBUFFER (@spid)

    OR

    DECLARE @spid int

    DECLARE @handle binary(20)

    SET @spid = (SELECT top 1 SPID from master..sysprocesses order by last_batch desc)

    SET @handle = (SELECT sql_handle from master..sysprocesses where spid = @spid)

    SELECT * FROM ::fn_get_sql(@Handle)

    for more infor about the ISOLATION and Lock HINTS http://dbanation.com/?p=114

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply