This blog is created to add all the information and experiences that I have learnt while working on MS sql server as a DBA. The blog is also updated frequently with the real world problem that I encounter hands on at work and the resolutions to resolve them. I hope this blog will be of some use to you and you will revisit. Thank you for stopping by and you are welcome to leave comments.
Friday, May 1, 2015
Find database lock
IF EXISTS(SELECT request_session_id FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID('Model'))
PRINT 'Model Database being used by some other session'
ELSE
PRINT 'Model Database not used by other session'
---to see what is locking up the database
SELECT request_session_id FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID('Model')
Labels:
Administration,
create database,
data,
database lock,
locks,
model,
transaction lock
Thursday, March 19, 2015
Logging the performance counter
The following script creates a table PerfMondata in a SQLMonitor database and logs the counter in the interval of 30 seconds
use [SQLMonitor]
GO
SET NOCOUNT ON ;
DECLARE @PerfCounters TABLE (
[Counter] nvarchar(770),
[CounterType] int,
[FirstValue] decimal (38,2),
[FirstDateTime] datetime,
[SecondValue] decimal(38,2),
[SecondDateTime] datetime ,
[ValueDiff] AS ([SecondValue] - [FirstValue]) ,
[timeDiff] AS (DateDiff(ss,[FirstDateTime],[SecondDateTime])),
[CounterValue] decimal(38,2)
) ;
INSERT INTO @PerfCounters (
[Counter],
[CounterType],
[FirstValue],
[FirstDateTime]
)
SELECT RTRIM([OBJECT_NAME]) + N' : ' + RTRIM([counter_name]) + N':' +RTRIM([instance_name]),[cntr_type],[cntr_value],GETDATE()
from sys.dm_os_performance_counters
WHERE [counter_name] IN ('Page life expectancy','Lazy writes/sec', 'Page reads/sec', 'Page writes/sec ','Page lookups/sec',
'Free list stalls/sec','User Connections','Lock Waits/sec','Number of Deadlocks/sec',
'Transactions/sec ', 'Forwarded Records/sec','Index Searches/sec','Batch Requests/sec','SQL Compilations/sec','SQL Re-Compilations/sec',
'Target Server Memory (KB)', 'Latch Waits/sec' ,'Buffer cache hit ratio'
)
order by [OBJECT_NAME] + N' : ' + [counter_name]+ N':' +RTRIM([instance_name])
Waitfor delay '00:00:30';
Update @PerfCounters
SET [SecondValue]= [cntr_value],
[SecondDateTime] = GETDATE()
from sys.dm_os_performance_counters
where [Counter] = RTRIM([OBJECT_NAME]) + N' : ' + RTRIM([counter_name]) + N':' +RTRIM([instance_name])
AND [counter_name] IN ('Page life expectancy','Lazy writes/sec', 'Page reads/sec', 'Page writes/sec ','Page lookups/sec',
'Free list stalls/sec','User Connections','Lock Waits/sec','Number of Deadlocks/sec',
'Transactions/sec ', 'Forwarded Records/sec','Index Searches/sec','Batch Requests/sec','SQL Compilations/sec','SQL Re-Compilations/sec',
'Target Server Memory (KB)', 'Latch Waits/sec','Buffer cache hit ratio' )
--for per sec counters
Update @PerfCounters
SET CounterValue = ([SecondValue]-[FirstValue])/(DateDiff(ss,[FirstDateTime],[SecondDateTime]))
where [counterType] = 272696576;
--for non per sec counters
Update @PerfCounters
SET CounterValue = [SecondValue]
where [counterType] <> 272696576;
select * from @PerfCounters
INSERT INTO PerfMondata (perfCounter,Value,CAptureDate)
SELECT [counter],[CounterValue],[SecondDateTime] from @PerfCounters
--select * from PerfMondata
--following stored procedure takes in a counter as the parameter and displays the report on a specific counter
use [SQLMonitor]
GO
if OBJECTPROPERTY(OBJECT_ID('proc_sqlAdmin_PerfMonReport'),'IsProcedure')= 1
DROP PROCEDURE proc_sqlAdmin_PerfMonReport
GO
CREATE PROCEDURE dbo.proc_sqlAdmin_PerfMonReport
(
@Counter nvarchar(128)= '%'
)
AS
BEGIN ;
SELECT [Perfcounter],value,captureDate
from perfMondata
WHERE [Perfcounter] like @Counter
ORDER BY [Perfcounter],[captureDate]
END ;
/*
exec dbo.proc_sqlAdmin_PerfMonReport '%Page life expectancy%'
exec dbo.proc_sqlAdmin_PerfMonReport '%Batch Requests/sec%'
exec dbo.proc_sqlAdmin_PerfMonReport '%Page Writes/sec%'
*/
use [SQLMonitor]
GO
SET NOCOUNT ON ;
DECLARE @PerfCounters TABLE (
[Counter] nvarchar(770),
[CounterType] int,
[FirstValue] decimal (38,2),
[FirstDateTime] datetime,
[SecondValue] decimal(38,2),
[SecondDateTime] datetime ,
[ValueDiff] AS ([SecondValue] - [FirstValue]) ,
[timeDiff] AS (DateDiff(ss,[FirstDateTime],[SecondDateTime])),
[CounterValue] decimal(38,2)
) ;
INSERT INTO @PerfCounters (
[Counter],
[CounterType],
[FirstValue],
[FirstDateTime]
)
SELECT RTRIM([OBJECT_NAME]) + N' : ' + RTRIM([counter_name]) + N':' +RTRIM([instance_name]),[cntr_type],[cntr_value],GETDATE()
from sys.dm_os_performance_counters
WHERE [counter_name] IN ('Page life expectancy','Lazy writes/sec', 'Page reads/sec', 'Page writes/sec ','Page lookups/sec',
'Free list stalls/sec','User Connections','Lock Waits/sec','Number of Deadlocks/sec',
'Transactions/sec ', 'Forwarded Records/sec','Index Searches/sec','Batch Requests/sec','SQL Compilations/sec','SQL Re-Compilations/sec',
'Target Server Memory (KB)', 'Latch Waits/sec' ,'Buffer cache hit ratio'
)
order by [OBJECT_NAME] + N' : ' + [counter_name]+ N':' +RTRIM([instance_name])
Waitfor delay '00:00:30';
Update @PerfCounters
SET [SecondValue]= [cntr_value],
[SecondDateTime] = GETDATE()
from sys.dm_os_performance_counters
where [Counter] = RTRIM([OBJECT_NAME]) + N' : ' + RTRIM([counter_name]) + N':' +RTRIM([instance_name])
AND [counter_name] IN ('Page life expectancy','Lazy writes/sec', 'Page reads/sec', 'Page writes/sec ','Page lookups/sec',
'Free list stalls/sec','User Connections','Lock Waits/sec','Number of Deadlocks/sec',
'Transactions/sec ', 'Forwarded Records/sec','Index Searches/sec','Batch Requests/sec','SQL Compilations/sec','SQL Re-Compilations/sec',
'Target Server Memory (KB)', 'Latch Waits/sec','Buffer cache hit ratio' )
--for per sec counters
Update @PerfCounters
SET CounterValue = ([SecondValue]-[FirstValue])/(DateDiff(ss,[FirstDateTime],[SecondDateTime]))
where [counterType] = 272696576;
--for non per sec counters
Update @PerfCounters
SET CounterValue = [SecondValue]
where [counterType] <> 272696576;
select * from @PerfCounters
INSERT INTO PerfMondata (perfCounter,Value,CAptureDate)
SELECT [counter],[CounterValue],[SecondDateTime] from @PerfCounters
--select * from PerfMondata
--following stored procedure takes in a counter as the parameter and displays the report on a specific counter
use [SQLMonitor]
GO
if OBJECTPROPERTY(OBJECT_ID('proc_sqlAdmin_PerfMonReport'),'IsProcedure')= 1
DROP PROCEDURE proc_sqlAdmin_PerfMonReport
GO
CREATE PROCEDURE dbo.proc_sqlAdmin_PerfMonReport
(
@Counter nvarchar(128)= '%'
)
AS
BEGIN ;
SELECT [Perfcounter],value,captureDate
from perfMondata
WHERE [Perfcounter] like @Counter
ORDER BY [Perfcounter],[captureDate]
END ;
/*
exec dbo.proc_sqlAdmin_PerfMonReport '%Page life expectancy%'
exec dbo.proc_sqlAdmin_PerfMonReport '%Batch Requests/sec%'
exec dbo.proc_sqlAdmin_PerfMonReport '%Page Writes/sec%'
*/
Thursday, March 12, 2015
Dynamic sql : Getting result of dynamic SQL into a variable
DECLARE @sqlCommand nvarchar(1000)
DECLARE @city varchar(75)
declare @counts int
SET @city = 'New York'
SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city' EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75),@cnt int OUTPUT', @city = @city, @cnt=@counts OUTPUT select @counts as Counts
DECLARE @city varchar(75)
declare @counts int
SET @city = 'New York'
SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city' EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75),@cnt int OUTPUT', @city = @city, @cnt=@counts OUTPUT select @counts as Counts
Monday, March 9, 2015
I had a situation where I had to loop through a list of database connections and if one of the database connection was down the flow had to continue to the next database.
This is how I achieved it -
1. The list of database connections ( the result returned by the Execute SQL task : Retrieve hospital list ) was assigned to the user variable @HospitalList ( type Object) and the list was assigned as the enumerator for the Foreach loop show in the following diagram.
The Script editor had the following code.
public void Main()
{
// TODO: Add your code here
bool failure = false;
bool fireAgain = true;
foreach (var ConnMgr in Dts.Connections)
{
Dts.Events.FireInformation(1, "", String.Format("ConnectionManager='{0}', ConnectionString='{1}'",
ConnMgr.Name, ConnMgr.ConnectionString), "", 0, ref fireAgain);
try
{
ConnMgr.AcquireConnection(null);
Dts.Events.FireInformation(1, "", String.Format("Connection acquired successfully on '{0}'",
ConnMgr.Name), "", 0, ref fireAgain);
}
catch (Exception ex)
{
Dts.Events.FireError(-1, "", String.Format("Failed to acquire connection to '{0}'. Error Message='{1}'",
ConnMgr.Name, ex.Message),
"", 0);
failure = true;
}
}
if (failure)
Dts.TaskResult = (int)ScriptResults.Failure;
else
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
The Precedence Constraint Editor for the success flow was set as follows
This is how I achieved it -
1. The list of database connections ( the result returned by the Execute SQL task : Retrieve hospital list ) was assigned to the user variable @HospitalList ( type Object) and the list was assigned as the enumerator for the Foreach loop show in the following diagram.
The Script editor had the following code.
public void Main()
{
// TODO: Add your code here
bool failure = false;
bool fireAgain = true;
foreach (var ConnMgr in Dts.Connections)
{
Dts.Events.FireInformation(1, "", String.Format("ConnectionManager='{0}', ConnectionString='{1}'",
ConnMgr.Name, ConnMgr.ConnectionString), "", 0, ref fireAgain);
try
{
ConnMgr.AcquireConnection(null);
Dts.Events.FireInformation(1, "", String.Format("Connection acquired successfully on '{0}'",
ConnMgr.Name), "", 0, ref fireAgain);
}
catch (Exception ex)
{
Dts.Events.FireError(-1, "", String.Format("Failed to acquire connection to '{0}'. Error Message='{1}'",
ConnMgr.Name, ex.Message),
"", 0);
failure = true;
}
}
if (failure)
Dts.TaskResult = (int)ScriptResults.Failure;
else
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
The Precedence Constraint Editor for the success flow was set as follows
The Precedence Constraint Editor for the failure was set as follows.
Note: The connection string of the database was dynamically created . The connection string was stored in a database table which was read by the Execute SQL task : Retrieve hospital list
Wednesday, February 25, 2015
Script:Code to Find the Size and Growth Percentage of the tempdb Data and Log Files
Code to Find the Size and Growth Percentage of the tempdb Data and Log Files
SELECT
name AS FileName,
size*1.0/128 AS FileSizeinMB,
CASE max_size
WHEN 0 THEN 'Autogrowth is off.'
WHEN -1 THEN 'Autogrowth is on.'
ELSE 'Log file will grow to a maximum size of 2 TB.'
END,
growth AS 'GrowthValue',
'GrowthIncrement' =
CASE
WHEN growth = 0 THEN 'Size is fixed and will not grow.'
WHEN growth > 0 AND is_percent_growth = 0
THEN 'Growth value is in 8-KB pages.'
ELSE 'Growth value is a percentage.'
END
FROM tempdb.sys.database_files;
GO
name AS FileName,
size*1.0/128 AS FileSizeinMB,
CASE max_size
WHEN 0 THEN 'Autogrowth is off.'
WHEN -1 THEN 'Autogrowth is on.'
ELSE 'Log file will grow to a maximum size of 2 TB.'
END,
growth AS 'GrowthValue',
'GrowthIncrement' =
CASE
WHEN growth = 0 THEN 'Size is fixed and will not grow.'
WHEN growth > 0 AND is_percent_growth = 0
THEN 'Growth value is in 8-KB pages.'
ELSE 'Growth value is a percentage.'
END
FROM tempdb.sys.database_files;
GO
Labels:
Administration,
autogrowth,
datafile,
file growth,
sysfiles,
tempdb
Wednesday, February 18, 2015
Srcript: Analysing Performance monitor data
After capturing the data in the tables ( link here - http://sql-dba-online.blogspot.com/2015/02/performance-monitor-how-to-run-relog-to.html) The following script will help to analyze the data.
/*show data collected for specific collector set, ordered by counter and time */
SELECT * from counterData cd
JOIN DisplayTOId di
ON cd.[GUID] = di.[GUID]
JOIN counterdetails cdt
ON cdt.[counterID] = cd.[counterID]
WHERE di.[DisplayString] = 'ProdSample.blg'
ORDER BY cdt.[ObjectName],
cdt.[CounterName],cd.[RecordIndex]
/* List max,min,avg,stdev for each counter for a specific collector*/
SELECT CONVERT(VARCHAR(10),cd.counterDateTime, 101) AS "collection" ,
RTRIM(cdt.objectName) + '\' + rtrim(cdt.CounterName) +
Case
WHEN cdt.InstanceName IS NULL then ''
WHEN cdt.instanceName is not null then '_' + rtrim(cdt.InstanceName)
END as "Counter",
CAST (MIN(cd.CounterValue) AS Decimal(6,0)) AS "Minimum",
CAST (MAX(cd.CounterValue) AS Decimal(6,0)) AS "Maximum",
CAST (AVG(cd.CounterValue) AS Decimal(6,0)) AS "Average",
CAST (STDEV(cd.CounterValue) AS Decimal(6,0)) AS "StDev"
FROM counterData cd
JOIN DisplayTOId di
ON cd.[GUID] = di.[GUID]
JOIN counterdetails cdt
ON cdt.[counterID] = cd.[counterID]
WHERE di.[DisplayString] = 'ProdSample.blg'
GROUP BY CONVERT(VARCHAR(10),cd.counterDateTime, 101),cdt.objectName,cdt.CounterName,cdt.instanceName
/*show data collected for specific collector set, ordered by counter and time */
SELECT * from counterData cd
JOIN DisplayTOId di
ON cd.[GUID] = di.[GUID]
JOIN counterdetails cdt
ON cdt.[counterID] = cd.[counterID]
WHERE di.[DisplayString] = 'ProdSample.blg'
ORDER BY cdt.[ObjectName],
cdt.[CounterName],cd.[RecordIndex]
/* List max,min,avg,stdev for each counter for a specific collector*/
SELECT CONVERT(VARCHAR(10),cd.counterDateTime, 101) AS "collection" ,
RTRIM(cdt.objectName) + '\' + rtrim(cdt.CounterName) +
Case
WHEN cdt.InstanceName IS NULL then ''
WHEN cdt.instanceName is not null then '_' + rtrim(cdt.InstanceName)
END as "Counter",
CAST (MIN(cd.CounterValue) AS Decimal(6,0)) AS "Minimum",
CAST (MAX(cd.CounterValue) AS Decimal(6,0)) AS "Maximum",
CAST (AVG(cd.CounterValue) AS Decimal(6,0)) AS "Average",
CAST (STDEV(cd.CounterValue) AS Decimal(6,0)) AS "StDev"
FROM counterData cd
JOIN DisplayTOId di
ON cd.[GUID] = di.[GUID]
JOIN counterdetails cdt
ON cdt.[counterID] = cd.[counterID]
WHERE di.[DisplayString] = 'ProdSample.blg'
GROUP BY CONVERT(VARCHAR(10),cd.counterDateTime, 101),cdt.objectName,cdt.CounterName,cdt.instanceName
Labels:
Administration,
datacollector,
performance monitor,
script
Performance Monitor : How to run relog to process performance monitor data
Run relog from the directory where the datacollector file is
Type the following in the command prompt
c:\cd PerfLogs\Admin\FromTemplate\000001
( the above is the path where my file is)
running relog
c:\cd PerfLogs\Admin\FromTemplate\000001 > relog ProdSample.blg -f SQL -o SQL:BaselineData!ProdSample.blg
ProdSample is my perflog file
-f is the format in which you want
-o is the output
SQL:BaselineData ( it is the ODBC connection that I had created pointing to the database where I wanted the data to be dumped)
Once I ran the above command relog transferred the data and it created the following tables in the database connected trough ODBC . BaseLinedata in this case.
DisplayToID
CounterData
CounterDetails
Type the following in the command prompt
c:\cd PerfLogs\Admin\FromTemplate\000001
( the above is the path where my file is)
running relog
c:\cd PerfLogs\Admin\FromTemplate\000001 > relog ProdSample.blg -f SQL -o SQL:BaselineData!ProdSample.blg
ProdSample is my perflog file
-f is the format in which you want
-o is the output
SQL:BaselineData ( it is the ODBC connection that I had created pointing to the database where I wanted the data to be dumped)
Once I ran the above command relog transferred the data and it created the following tables in the database connected trough ODBC . BaseLinedata in this case.
DisplayToID
CounterData
CounterDetails
Subscribe to:
Posts (Atom)