Tuesday 29 October 2013

SQL SERVER – Wait Types

When I was working with couple of frequent blocking issue in our production servers, I have noticed different types of wait resources which are very common and can be very useful but people often overlooked these while troubleshooting. I also feel that "wait stats" is one of the most under-utilized performance troubleshooting methodologies in the SQL Server.

Any time a user connection is waiting, SQL Server keep track of wait time. For example, the application requests resources such as I/O, locks, or memory and can wait for the resource to be available.

There are few system wait types which are always present and there are a few wait types which can really cause a heavy bottleneck for your system.


Types of Waits

As per BOL, there are three types of wait types, namely:

Resource waits
Resource waits occur when a worker requests access to a resource that is not available because the resource is being used by some other worker or is not yet available. Examples of resource waits are locks, latches, network and disk I/O waits. Lock and latch waits are waits on synchronization objects
Queue waits
Queue waits occur when a worker is idle, waiting for work to be assigned. Queue waits are most typically seen with system background tasks such as the deadlock monitor and deleted record cleanup tasks. These tasks will wait for work requests to be placed into a work queue. Queue waits may also periodically become active even if no new packets have been put on the queue.
External waits
External waits occur when a SQL Server worker is waiting for an external event, such as an extended stored procedure call or a linked server query, to finish. When you diagnose blocking issues, remember that external waits do not always imply that the worker is idle, because the worker may actively be running some external code.

To check the wait types for any server, just run the following simple T-SQL:
SELECT * FROM sys.dm_os_wait_stats

Along with above thoughts , it woold be good to extract useful information from below T-SQL. This will show the waits grouped together as a percentage of all waits on the system, in decreasing order. The waits to be concerned about (potentially) are those at the top of the list as this represents the majority of where SQL Server is spending it’s time waiting.

This code based on a query written by MVP Glenn Berry and modified by Paul S. Randal so credit goes to them.          
                                                                                                            ----------------------------------------------------------------

WITH [Waits] AS
    (SELECT
        [wait_type],
        [wait_time_ms] / 1000.0 AS [WaitS],
        ([wait_time_ms] - [signal_wait_time_ms]) / 1000.0 AS [ResourceS],
        [signal_wait_time_ms] / 1000.0 AS [SignalS],
        [waiting_tasks_count] AS [WaitCount],
        100.0 * [wait_time_ms] / SUM ([wait_time_ms]) OVER() AS [Percentage],
        ROW_NUMBER() OVER(ORDER BY [wait_time_ms] DESC) AS [RowNum]
    FROM sys.dm_os_wait_stats
    WHERE [wait_type] NOT IN (
        N'CLR_SEMAPHORE',    N'LAZYWRITER_SLEEP',
        N'RESOURCE_QUEUE',   N'SQLTRACE_BUFFER_FLUSH',
        N'SLEEP_TASK',       N'SLEEP_SYSTEMTASK',
        N'WAITFOR',          N'HADR_FILESTREAM_IOMGR_IOCOMPLETION',
        N'CHECKPOINT_QUEUE', N'REQUEST_FOR_DEADLOCK_SEARCH',
        N'XE_TIMER_EVENT',   N'XE_DISPATCHER_JOIN',
        N'LOGMGR_QUEUE',     N'FT_IFTS_SCHEDULER_IDLE_WAIT',
        N'BROKER_TASK_STOP', N'CLR_MANUAL_EVENT',
        N'CLR_AUTO_EVENT',   N'DISPATCHER_QUEUE_SEMAPHORE',
        N'TRACEWRITE',       N'XE_DISPATCHER_WAIT',
        N'BROKER_TO_FLUSH',  N'BROKER_EVENTHANDLER',
        N'FT_IFTSHC_MUTEX',  N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP',
        N'DIRTY_PAGE_POLL',  N'SP_SERVER_DIAGNOSTICS_SLEEP')
    )
SELECT
    [W1].[wait_type] AS [WaitType],
    CAST ([W1].[WaitS] AS DECIMAL(14, 2)) AS [Wait_S],
    CAST ([W1].[ResourceS] AS DECIMAL(14, 2)) AS [Resource_S],
    CAST ([W1].[SignalS] AS DECIMAL(14, 2)) AS [Signal_S],
    [W1].[WaitCount] AS [WaitCount],
    CAST ([W1].[Percentage] AS DECIMAL(4, 2)) AS [Percentage],
    CAST (([W1].[WaitS] / [W1].[WaitCount]) AS DECIMAL (14, 4)) AS [AvgWait_S],
    CAST (([W1].[ResourceS] / [W1].[WaitCount]) AS DECIMAL (14, 4)) AS [AvgRes_S],
    CAST (([W1].[SignalS] / [W1].[WaitCount]) AS DECIMAL (14, 4)) AS [AvgSig_S]
FROM [Waits] AS [W1]
INNER JOIN [Waits] AS [W2]
    ON [W2].[RowNum] <= [W1].[RowNum]
GROUP BY [W1].[RowNum], [W1].[wait_type], [W1].[WaitS],
    [W1].[ResourceS], [W1].[SignalS], [W1].[WaitCount], [W1].[Percentage]
HAVING SUM ([W2].[Percentage]) - [W1].[Percentage] < 95; -- percentage threshold
GO

Output: Will be something like as mentioned below:



Brgds,

Chhavinath Mishra
Sr. Database Administrator

Microsoft Certified IT Professional (MCITP)

Tuesday 8 October 2013

How to Fix a failed In-Place Upgrade/Install from SQL Server 2008/R2 to SQL Server 2008 R2/2012 -- - MSSQLSERVER.INACTIVE

While working on a project ,I've come across situations where I was trying to upgrade SQL Server 2008 R2 to SQL Server 2012. The upgrade failed with error message below. It doesn't matter what version of SQL you're on, upgrading to etc...this issue can pretty much be resolved using this process

--------------------------------------------------------------------------------

The Instance ID ‘MSSQLSERVER’ is already in use by SQL Server instance ‘MSSQLSERVER.INACTIVE’. To continue, specify a unique Instance ID.

--------------------------------------------------------------------------------

Problem/Sympton: I went to upgrade a perfectly working SQL 2008 R2 standard server (which was using a Default Instance) to SQL 2012. The Setup failed halfway through with a random error connecting to SQL Analysis Services (your failure could be different)...then it indicated all I had to do was fix the problem and re-run my SQL 2012 Upgrade...this wasn't true :-)

Brief Background of upgrade process:

Here we have an orphaned SQL instance named MSSQLServer.Inactive. Let’s understand the upgrade process here. During your upgrade, there are two stages:

Pre-PointOfNoReturn

Post- PointOfNoReturn


Point of No Return is a point at which 2008 R2 will take over the instance name and SQL Server 2008 will be then uninstalled.

The REAL Resolution Steps:

Search your machine for files called Datastore_Discovery.xml (depending on how much you've got or done, you might see many of these...just open the most recent one and you'll be fine

Open Datastore_Discovery.xml in an editor...given the formatting, what worked for me was in Visual Studio 2008/2010 and clicking Edit, Advanced, Format Document (this arranges the XML nicely)

What you're looking for is all places where MSSQLServer.Inactive exists in the document as the Instance ID...below is a sample of how the line starts:
<Instance Urn="Machine[@ID='IWE']/Product[@ID='SQLVNEXT']/Instance[@ID='MSSQLSERVER.INACTIVE']" ID="MSSQLSERVER.INACTIVE" Name="MSSQLSERVER.INACTIVE" ........

Scroll along the lines around the above one and find the following value field: ProductCode="{9FFAE13C-6160-4DD0-A67A-DAC5994F81BD}"

There might be multiple ProductCodes, depending on how many times the <Instance URN...> line lists MSSQLServer.Inactive, so make sure to find all ProductCodes...BUT BE CAREFUL that you ignore any VALID instances of SQL. Only look for the ones under MSSQLServer.Inactive entries.

Now open a command line

For each of the ProductCodes that you found for the Inactive SQL Instances, type the following:
msiexec /x {9FFAE13C-6160-4DD0-A67A-DAC5994F81BD}

Do this for each Product Code that relates to an Inactive Instance

Once complete, go back to your SQL installation media and run Setup

Once the main splash screen launches, click on Tools, then Installed SQL Server features discovery report

This will launch a web page listing the SQL Instances and all features per instance

If you still see any MSSQLSERVER.INACTIVE there, you missed one of the ProductCodes and you need to double check. Otherwise, all of the orphaned SQL Instances should now be gone, and you can either Add Features or do a clean Default Instance reinstall

Note : /x means uninstall

I hope this will help you for sure!


Brgds,

Chhavinath Mishra
Sr. Database Administrator

Microsoft Certified IT Professional (MCITP)

Thursday 3 October 2013

xp_readerrorlog V/S sp_readerrorlog parameters in SQL Server



Difference between xp_readerrorlog & sp_readerrorlog

sp_readerrorlog is a stored procedure in the master database that checks that the user is part of the securityadmin group, then calls xp_readerrorlog, passing parameters if available.  Any differences between the two procedures does not affect the output, which means the information returned is the same.

The parameters syntax has been changed in SQL Server 2012 but not documented yet.
sp_readerrorlog 0,1,’node’
xp_readerrorlog 0,1, ‘node’ -- Not supported
sp_readerrorlog 0,1, "node"
xp_readerrorlog 0,1,"node"
Parameters defined
* First Parameter (type int) :- is the number of the log file you want to read, default is "0" for current log.
* Second Parameter (type int), value of 1 reads SQL error logs, value of 2 reads SQL Server Agent logs, with a default value of 1
* Third Parameter varchar (255), is a search string for the log entry, with a default value of NULL
* Forth Parameter varchar (255), is another search string for the log entry, with a default value of NULL

expanding the number of SQL Server error logs

To limit the size of the SQL Server error log, the sp_cycle_errorlog system stored procedure can be issued to start a new error log.

Finally, to address not loosing the historical SQL Server error log, the number of logs should be expanded beyond the default of 7.  The maximum number of logs is 99 for the SQL Server error log.  When it comes to expanding the number of SQL Server error logs, follow these steps:
  • Open Management Studio
  • Navigate to root | Management folder | SQL Server Logs folder
  • Right click on the SQL Server Logs folder and select the 'Configure' option
  • Select the 'Limit the number of error log files before they are recycled' check box
    • Reference the screen shot below as a point of reference
  • Enter the desired number of error logs in the 'Maximum number of error log files'
    • Reference the screen shot below as a point of reference   
  • Press the 'OK' button to save the configuration

Hope you find this interesting.

Brgds,

Chhavinath Mishra
Sr. Database Administrator
Microsoft Certified IT Professional (MCITP)