Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « "The What, Why, and How of Filegroups" - I-380 PASS MaterialsSQL Advent 2012 Day 11: Cursors and loops »
    comments

    This is day twelve of the SQL Advent 2012 series of blog posts. Today we are going to look at SQL Server proactive notifications. In the SQL Server Maintenance post from yesterday I touched upon proactive notifications a little, today I want to dive a little deeper into this subject.

    The last thing you want as a DBA is to hear any of the following things from the end users

    • The transaction log is full
    • The database is very slow
    • The latest backup we have is 9 days old
    • The table that was created has 2 extra columns this morning
    • Everything is locked up can't get any results back from a query
    • Deadlocks are occurring

    What you really want to have at your shop is a tool like Quest Foglight, Confio Ignite, Red Gate SQL Monitor or similar. The benefit of these tools is that there is a central location where you can look at all the alerts at a glance. You get a lot of stuff out of the box and all you have to do is tell it what server to start monitoring. I would suggest to start using the trial version to see if it is something that would be beneficial for your organization.

    Of course you can roll your own solution as well, this will involve work and unless your time is worthless or you are bored out of your mind after work I wouldn't do it.

    Utilize the logs

    You need to scan the errorlog periodically to see if there are errors, you can automate this, no need to start opening log files every 5 minutes. create a SQL Agent job that runs every 5 minutes and checks if there are any errors since it last ran. You can use the xp_readerrorlog proc to read the error log from with sql server with T-SQL.

    Here is a small example of what you can do if you have this in a SQL Agent job that runs every 5 minutes or so, you can of course email yourself the results, dump the result into a table that is perhaps shown on a dashboard in the office, there are many possibilities.

    1. --This will hold the rows
    2. CREATE TABLE #ErrorLog (LogDate datetime, ProcessInfo VarChar(10), ErrorMessage VarChar(Max))
    3.  
    4. -- Dump the errorlog into the table
    5. INSERT INTO #ErrorLog
    6. EXEC master.dbo.xp_readerrorlog
    7.  
    8. -- Delete everything older than 5 minutes
    9. -- ideally you will store the max date when it ran last
    10. DELETE #ErrorLog
    11. WHERE LogDate <  DATEADD(mi,-5,GETDATE())
    12.  
    13. -- Some stuff you want to check for
    14. -- Failed backups...you want to know this
    15. SELECT * FROM #ErrorLog
    16. WHERE ErrorMessage LIKE'BACKUP failed%'
    17.  
    18. -- Why does it take so looong to grow a file, maybe rethink your settings
    19. SELECT * FROM #ErrorLog
    20. WHERE ErrorMessage LIKE'Autogrow of file%'
    21.  
    22. -- What is going on any backups or statistic updates running at this time?
    23. SELECT * FROM #ErrorLog
    24. WHERE ErrorMessage LIKE'SQL Server has encountered %occurrence(s) of I/O requests taking longer than%'
    25.  
    26. -- My mirror might not be up to date
    27. SELECT * FROM #ErrorLog
    28. WHERE ErrorMessage LIKE'The alert for ''unsent log'' has been raised%'
    29.  
    30.  
    31. DROP TABLE #ErrorLog

    Those are just small samples, you might want to look for other kind of messages from the errorlog


    The transaction log is full

    You want to make sure that you know you are running out of space before you run out of space. I covered this in the SQL Server Maintenance post Take a look at the sections Make sure that you have enough space left on the drives and Make sure that you have enough space left for the filegroups In those two section I described what to look for and also supplied code that you can then plug into your own solution


    The database is very slow

    This complaint you hear every now and then, I have seen this from time to time. There are several things that could be happening, here is a list

    Someone decided to take a backup of that 1 TB database in the middle of the day
    The update statistics job is still running
    Statistics are stale and haven't been updated in a long time
    The virus scan is running amok and nobody told it to ignore the database files
    Someone decided to query all the data all at once

    If you have a tool like Quest Foglight, Confio Ignite, Red Gate SQL Monitor or similar then you can see what query ran at what time, what it did and how long it ran.

    You can of course also use sp_who2, BlkBy column and DBCC INPUTBUFFER to see what is going on

    If you like to use Dynamic Management Views, then take a look at Glenn Berry's SQL Server 2005 Diagnostic Information Queries (Dec 2012) and SQL Server 2008 Diagnostic Information Queries (Nov 2012) posts, there is a .sql file in each post with all kind of queries to discover all kinds of stuff about your server.

    It could also be that your hardware is having issues, make sure the IOs look good and check the eventlog for any clues.


    The latest backup we have is 9 days old

    The following query will give you for all the databases the last time it was backed up or display NEVER if it wasn't backed up

    1. SELECT s.Name AS DatabaseName,'Database backup was taken on  ' +
    2. CASE WHEN MAX(b.backup_finish_date) IS NULL THEN 'NEVER!!!' ELSE
    3. CONVERT(VARCHAR(12), (MAX(b.backup_finish_date)), 101) END AS LastBackUpTime
    4. FROM sys.sysdatabases s
    5. LEFT OUTER JOIN msdb.dbo.backupset b ON b.database_name = s.name
    6. GROUP BY s.Name

    Here is what the output will look like

    DatabaseName	LastBackUpTime
    --------------  ---------------------------------------
    model	        Database backup was taken on  NEVER!!!
    msdb	        Database backup was taken on  12/10/2012
    ReportServer	Database backup was taken on  NEVER!!!

    As you can see that is not that great, all the databases should be backed up on a regular basis. Scroll up to the Utilize the logs section to see how you can check the errorlog for failed backup messages.


    Everything is locked up, you can't get any results back from a query

    Usually this indicates that there is an open transaction somewhere that has not finished or someone did the BEGIN TRAN part but never did a COMMIT or ROLLBACK.

    Some people just restart the server to 'fix' the issue, of course if you do that you will never know what the root cause is and you never know when it will happen again.

    We can easily show what happens when you have an open transaction, btw don't do this on the production server.

    In 1 query window run this, replace SomeTable with a real table name.

    1. BEGIN TRAN
    2.  
    3. SELECT TOP 1 * FROM SomeTable WITH(UPDLOCK, HOLDLOCK)

    You will get a message that the query completed successfully

    In another window run this

    1. SELECT TOP 1 * FROM SomeTable WITH(UPDLOCK, HOLDLOCK)

    That query won't return anything unless the first one is commited or rolled back
    Now run this query below, the first column should have the text AWAITING COMMAND

    1. SELECT   sys.cmd
    2.         ,sys.last_batch
    3.         ,lok.resource_type
    4.         ,lok.resource_subtype
    5.         ,DB_NAME(lok.resource_database_id)
    6.         ,lok.resource_description
    7.         ,lok.resource_associated_entity_id
    8.         ,lok.resource_lock_partition
    9.         ,lok.request_mode
    10.         ,lok.request_type
    11.         ,lok.request_status
    12.         ,lok.request_owner_type
    13.         ,lok.request_owner_id
    14.         ,lok.lock_owner_address
    15.         ,wat.waiting_task_address
    16.         ,wat.session_id
    17.         ,wat.exec_context_id
    18.         ,wat.wait_duration_ms
    19.         ,wat.wait_type
    20.         ,wat.resource_address
    21.         ,wat.blocking_task_address
    22.         ,wat.blocking_session_id
    23.         ,wat.blocking_exec_context_id
    24.         ,wat.resource_description
    25. FROM    sys.dm_tran_locks lok
    26. JOIN    sys.dm_os_waiting_tasks wat
    27. ON      lok.lock_owner_address = wat.resource_address
    28. JOIN    sys.sysprocesses sys ON wat.blocking_session_id = sys.spid

    As you can see you have a blocking_session_id and a session_id, this will tell you which session_id is being blocked. You can now verify that the transaction session_id is blocking the other id

    Go back to that first command window and execute a rollback

    The query that had that second select should now be done as well, if you run that query that checks for the waits it should be clean as well.

    Of course you could have done the same excercise by running sp_who2, looking at the BlkBy column, finding out what that session is doing by running DBCC INPUTBUFFER(session_id) with that session_id


    Deadlocks are occurring

    There is already a post written on LessThanDot explaining how you can get emailed when deadlocks occur. Ted Krueger wrote that post and it can be found here: Proactive Deadlock Notifications

    Summary

    I only touched the surface of what can be done in this post. I want you to find out if there is any monitoring being done in your shop, who gets notified? I have worked in places where the end user was the proactive notification, as long as we fixed it before the business users started to complaint life was good. Manual notifications and homebrew solutions might work for a while but when you add more and more servers and you add more people to the team this becomes laborious and error prone.

    If there is one New Year's resolution you should add next year, I would suggest proactive notifications. Get trial versions of the tools, try them out until January 1st and then decide if you think it is worth.




    That is all for day twelve of the SQL Advent 2012 series, come back tomorrow for the next one, you can also check out all the posts from last year here: SQL Advent 2011 Recap

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    3459 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)