Tempgb log - not able to shrink....none of the queries are helping

  • I have gone through many many different forums in trying to identify my tempdb issues. None of the queries are giving me the data i want. Basically at the moment my tempdb log file is 38 gb, after i run all the queries from different forums i see ONLY 300 MB being used in tempdb. I am still lost and have these questions:

    i) Do the DMV's consider log file usage/capacity at all or is it just data file?

    ii) Can someone please provide me with a query which will tell me which spid has used up all the tempdb data and log file.

    Thanks

  • sp_who2 should tell you which SPIDs are active in tempdb. Can use the Management Studio activity monitor (right-click the server name in the object explorer if you haven't use that tool before), but that can be a pain since it updates constantly.

    DBCC SQLPERF(LOGSPACE) should tell you how much log space is being used.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (10/30/2012)


    sp_who2 should tell you which SPIDs are active in tempdb. Can use the Management Studio activity monitor (right-click the server name in the object explorer if you haven't use that tool before), but that can be a pain since it updates constantly.

    DBCC SQLPERF(LOGSPACE) should tell you how much log space is being used.

    I see that ONLY 25% is being used. I did try to take with backup with truncate_only option and then shrink,still log file shows 38 gb.?

  • TempDB is in simple recovery model, Backup log with truncate_only does nothing to a DB in simple recovery.

    When shrinking a log file, you can't shrink past the active portion of the log. If that's right at the end of the file, you'll need to wait until it cycles around to shrink the log.

    p.s. Why are you shrinking the log anyway?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (10/30/2012)


    TempDB is in simple recovery model, Backup log with truncate_only does nothing to a DB in simple recovery.

    When shrinking a log file, you can't shrink past the active portion of the log. If that's right at the end of the file, you'll need to wait until it cycles around to shrink the log.

    p.s. Why are you shrinking the log anyway?

    Thanks. What do u mean by " If that's right at the end of the file, you'll need to wait until it cycles around to shrink the log."

    I am curious to see that why is my tempdblog 40gb, basically my end goal is to find the process which is causing it to grow such big

  • The log is a circular file, SQL writes log records until it reaches the end of the file, then cycles round and starts reusing the file from the beginning.

    Managing Transaction Logs[/url]

    http://www.sqlservercentral.com/articles/Transaction+Log/72488/

    If you want to see what's using the log, put some monitoring in place, don't shrink it and force it to grow again.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (10/30/2012)


    The log is a circular file, SQL writes log records until it reaches the end of the file, then cycles round and starts reusing the file from the beginning.

    Managing Transaction Logs[/url]

    http://www.sqlservercentral.com/articles/Transaction+Log/72488/

    If you want to see what's using the log, put some monitoring in place, don't shrink it and force it to grow again.

    I guess at the moment i wanted to know why can't i shrink my logfile, what is holding up. Any query that can tell me this?

  • DBCC LOGINFO

    If the active portion of the log is at the end of the file (or near the end) you will not be able to shrink because nothing can move log records and shrink can only remove space that's after the active portion of the log

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • you can restrict the tempdb log file size ,so that it will stop growing and log file will get reuse .

    and then you can try to shrink .

    also restarting the sql server service can help you ...but I will never recommend for it 😉

    -----------------------------------------------------------------------------
    संकेत कोकणे

  • sanket kokane (10/31/2012)


    you can restrict the tempdb log file size ,so that it will stop growing and log file will get reuse .

    and then you can try to shrink .

    also restarting the sql server service can help you ...but I will never recommend for it 😉

    restarting sql server will help and how about closing/droping all the temp objects after its use is over.

    but restricting tempdb size might also cause the query related to tempdb to wait for resources incase other resource still want the tempdb space.

    Regards
    Durai Nagarajan

  • i dont think so...

    he is asking about tempdb log file not data file.

    by restricting log file it will get reuse if there any inactive portion at the start of the log file.

    also you can add extra log file to the tempdb

    -----------------------------------------------------------------------------
    संकेत कोकणे

  • yes, my miss.

    Regards
    Durai Nagarajan

  • sqldba_newbie (10/30/2012)


    I have gone through many many different forums in trying to identify my tempdb issues. None of the queries are giving me the data i want. Basically at the moment my tempdb log file is 38 gb, after i run all the queries from different forums i see ONLY 300 MB being used in tempdb. I am still lost and have these questions:

    i) Do the DMV's consider log file usage/capacity at all or is it just data file?

    ii) Can someone please provide me with a query which will tell me which spid has used up all the tempdb data and log file.

    Thanks

    As Gail mentioned .. why are you shrinking TEMPDB ? what is causing it to bloat ?

    Below is some T-SQL that will help you.

    -- monitor the space for tempdb

    SELECT

    SUM (user_object_reserved_page_count)*8 as usr_obj_kb,

    SUM (internal_object_reserved_page_count)*8 as internal_obj_kb,

    SUM (version_store_reserved_page_count)*8 as version_store_kb,

    SUM (unallocated_extent_page_count)*8 as freespace_kb,

    SUM (mixed_extent_page_count)*8 as mixedextent_kb,

    SUM (User_object_reserved_page_count +

    internal_object_reserved_page_count +

    version_store_reserved_page_count +

    unallocated_extent_page_count +

    mixed_extent_page_count) * 8 as total_space

    FROM sys.dm_db_file_space_usage

    --shrink tempdb

    USE [tempdb]

    GO

    DBCC FREEPROCCACHE -- CAUTION: this will free up all cache plans

    GO

    USE [tempdb]

    GO

    DBCC SHRINKFILE (N'templog' , 10000) -- will shrink the ldf file to 10GB

    GO

    Set nocount on

    --This script displays what's going on behind the scenes of TempDB database

    --This script is compatible with SQL Server 2005 only.

    If Exists (Select Name from SysObjects where Name='#Tmp_TempData')

    Begin

    Drop Table #Tmp_TempData

    End

    If Exists (Select Name from SysObjects where Name='#Tmp_Ops_TempDATA')

    Begin

    Drop Table #Tmp_Ops_TempDATA

    End

    --Dump all SQL Handles from TempDB into #Tmp_Ops_TempDATA

    SELECT

    t3.sql_handle as 'SQLHandle',

    t3.statement_start_offset as 'Statement_Start_OffSET',

    t3.statement_end_offset as 'Statement_End_OffSET'

    Into #Tmp_Ops_TempDATA

    from sys.dm_db_session_space_usage as t1, sys.dm_exec_requests t3,

    (select session_id, sum(internal_objects_alloc_page_count) as task_alloc,

    sum (internal_objects_dealloc_page_count) as task_dealloc

    from sys.dm_db_task_space_usage group by session_id) as t2

    where t1.session_id = t2.session_id and t1.session_id >50

    and t1.database_id = 2 --- tempdb is database_id=2

    and t1.session_id = t3.session_id

    --Go thru each handle and display the results

    Declare @SQLHandle VarBinary(64),@StartOffSet int,@EndOffSet int

    Declare CheckOps_TempDATA Cursor For Select [SQLHandle],Statement_Start_OffSET,Statement_End_OffSET from #Tmp_Ops_TempDATA

    Open CheckOps_TempDATA

    Fetch Next from CheckOps_TempDATA into @SQLHandle,@StartOffSet,@EndOffSet

    While (@@Fetch_Status=0)

    Begin

    --Insert into #Tmp_TempData

    Select

    DB_Name(qt.dbid) as 'Database_Name',

    substring(qt.text,s.statement_start_offset/2,

    (case when s.statement_end_offset = -1

    then len(convert(nvarchar(max), qt.text)) * 2

    else s.statement_end_offset end -s.statement_start_offset)/2)

    as 'SQL_Statement'

    ,s.statement_start_offset as 'Start_OffSET'

    ,s.statement_end_offset as 'End_OffSET'

    ,qt.text as 'TSQL_Batch'

    ,qt.objectid as 'Object_ID'

    ,s.execution_count as 'Execution_Count'

    ,s.total_physical_reads as 'Total_Physical_Reads'

    ,s.total_logical_writes as 'Total_Logical_Writes'

    from sys.dm_exec_query_stats s

    cross apply sys.dm_exec_sql_text(s.sql_handle) as qt

    where s.sql_handle = @SQLHANDLE

    and s.statement_start_offset = @StartOffSET

    and s.statement_end_offset = @EndOffSET

    Fetch Next from CheckOps_TempDATA into @SQLHandle,@StartOffSet,@EndOffSet

    End

    Close CheckOps_TempDATA

    Deallocate CheckOps_TempDATA

    ______________________________________________________________________________________________________________________________________________________________________________________
    HTH !
    Kin
    MCTS : 2005, 2008
    Active SQL Server Community Contributor 🙂

  • SQLQuest29 (11/2/2012)


    sqldba_newbie (10/30/2012)


    I have gone through many many different forums in trying to identify my tempdb issues. None of the queries are giving me the data i want. Basically at the moment my tempdb log file is 38 gb, after i run all the queries from different forums i see ONLY 300 MB being used in tempdb. I am still lost and have these questions:

    i) Do the DMV's consider log file usage/capacity at all or is it just data file?

    ii) Can someone please provide me with a query which will tell me which spid has used up all the tempdb data and log file.

    Thanks

    As Gail mentioned .. why are you shrinking TEMPDB ? what is causing it to bloat ?

    Below is some T-SQL that will help you.

    -- monitor the space for tempdb

    SELECT

    SUM (user_object_reserved_page_count)*8 as usr_obj_kb,

    SUM (internal_object_reserved_page_count)*8 as internal_obj_kb,

    SUM (version_store_reserved_page_count)*8 as version_store_kb,

    SUM (unallocated_extent_page_count)*8 as freespace_kb,

    SUM (mixed_extent_page_count)*8 as mixedextent_kb,

    SUM (User_object_reserved_page_count +

    internal_object_reserved_page_count +

    version_store_reserved_page_count +

    unallocated_extent_page_count +

    mixed_extent_page_count) * 8 as total_space

    FROM sys.dm_db_file_space_usage

    --shrink tempdb

    USE [tempdb]

    GO

    DBCC FREEPROCCACHE -- CAUTION: this will free up all cache plans

    GO

    USE [tempdb]

    GO

    DBCC SHRINKFILE (N'templog' , 10000) -- will shrink the ldf file to 10GB

    GO

    Set nocount on

    --This script displays what's going on behind the scenes of TempDB database

    --This script is compatible with SQL Server 2005 only.

    If Exists (Select Name from SysObjects where Name='#Tmp_TempData')

    Begin

    Drop Table #Tmp_TempData

    End

    If Exists (Select Name from SysObjects where Name='#Tmp_Ops_TempDATA')

    Begin

    Drop Table #Tmp_Ops_TempDATA

    End

    --Dump all SQL Handles from TempDB into #Tmp_Ops_TempDATA

    SELECT

    t3.sql_handle as 'SQLHandle',

    t3.statement_start_offset as 'Statement_Start_OffSET',

    t3.statement_end_offset as 'Statement_End_OffSET'

    Into #Tmp_Ops_TempDATA

    from sys.dm_db_session_space_usage as t1, sys.dm_exec_requests t3,

    (select session_id, sum(internal_objects_alloc_page_count) as task_alloc,

    sum (internal_objects_dealloc_page_count) as task_dealloc

    from sys.dm_db_task_space_usage group by session_id) as t2

    where t1.session_id = t2.session_id and t1.session_id >50

    and t1.database_id = 2 --- tempdb is database_id=2

    and t1.session_id = t3.session_id

    --Go thru each handle and display the results

    Declare @SQLHandle VarBinary(64),@StartOffSet int,@EndOffSet int

    Declare CheckOps_TempDATA Cursor For Select [SQLHandle],Statement_Start_OffSET,Statement_End_OffSET from #Tmp_Ops_TempDATA

    Open CheckOps_TempDATA

    Fetch Next from CheckOps_TempDATA into @SQLHandle,@StartOffSet,@EndOffSet

    While (@@Fetch_Status=0)

    Begin

    --Insert into #Tmp_TempData

    Select

    DB_Name(qt.dbid) as 'Database_Name',

    substring(qt.text,s.statement_start_offset/2,

    (case when s.statement_end_offset = -1

    then len(convert(nvarchar(max), qt.text)) * 2

    else s.statement_end_offset end -s.statement_start_offset)/2)

    as 'SQL_Statement'

    ,s.statement_start_offset as 'Start_OffSET'

    ,s.statement_end_offset as 'End_OffSET'

    ,qt.text as 'TSQL_Batch'

    ,qt.objectid as 'Object_ID'

    ,s.execution_count as 'Execution_Count'

    ,s.total_physical_reads as 'Total_Physical_Reads'

    ,s.total_logical_writes as 'Total_Logical_Writes'

    from sys.dm_exec_query_stats s

    cross apply sys.dm_exec_sql_text(s.sql_handle) as qt

    where s.sql_handle = @SQLHANDLE

    and s.statement_start_offset = @StartOffSET

    and s.statement_end_offset = @EndOffSET

    Fetch Next from CheckOps_TempDATA into @SQLHandle,@StartOffSet,@EndOffSet

    End

    Close CheckOps_TempDATA

    Deallocate CheckOps_TempDATA

    Thanks for the info. I think my post took a different angle now :(. My goal here is to find the queries which are filling up my tempdb. In your first query it ONLY tells how data file, what about log file? How do i tell freespace and total space in logfile? Why do i need to clear cache to shrink tempdb? In last query i won't be able to find the query using most space.

Viewing 14 posts - 1 through 13 (of 13 total)

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