Skip to content

Instantly share code, notes, and snippets.

View bartread's full-sized avatar
😀
Learning

Bart Read bartread

😀
Learning
View GitHub Profile
@bartread
bartread / getiolatenciesfordatabases.sql
Last active January 5, 2017 10:42
Get I/O latencies for SQL Server databases
-- Originally from http://www.sqlskills.com/blogs/paul/how-to-examine-io-subsystem-latencies-from-within-sql-server/
USE [master];
GO
SELECT
[vfs].[num_of_reads] AS TotalReads,
[ReadLatency / ms] =
CASE WHEN [num_of_reads] = 0
THEN 0 ELSE ([io_stall_read_ms] / [num_of_reads]) END,
@bartread
bartread / eventsbytypepersecond.sql
Last active January 6, 2017 12:41
SQL Server Profiler trace - events by type per second
SELECT [TimeSlice] =
RIGHT(N'0' + CAST(DATEPART(hh, StartTime) AS NVARCHAR(2)), 2)
+ N':'
+ RIGHT(N'0' + CAST(DATEPART(n, StartTime) AS NVARCHAR(2)), 2)
+ N':'
+ RIGHT(N'0' + CAST(DATEPART(ss, StartTime) AS NVARCHAR(2)), 2)
, te.name AS [EventType]
, [EventCount] = COUNT(te.name)
, [ReadCount] = SUM(Reads)
FROM [YOUR_TRACE_TABLE_SCHEMA].[YOUR_TRACE_TABLE_NAME] AS d
@bartread
bartread / eventsbytypeperminute.sql
Last active January 6, 2017 11:55
SQL Server Profiler trace table - events by type per minute
SELECT [TimeSlice] =
RIGHT(N'0' + CAST(DATEPART(hh, StartTime) AS NVARCHAR(2)), 2)
+ N':'
+ RIGHT(N'0' + CAST(DATEPART(n, StartTime) AS NVARCHAR(2)), 2)
, te.name AS [EventType]
, [EventCount] = COUNT(te.name)
, [ReadCount] = SUM(Reads)
FROM [YOUR_TRACE_TABLE_SCHEMA].[YOUR_TRACE_TABLE_NAME] AS d
INNER JOIN sys.trace_events AS te
ON te.trace_event_id = d.EventClass
@bartread
bartread / interestingsqlprofilereventspersecondincolumns.sql
Last active January 29, 2017 12:42
Interesting SQL Profiler events per second in columns
SELECT [TimeSlice] =
RIGHT(N'0' + CAST(DATEPART(hh, StartTime) AS NVARCHAR(2)), 2)
+ N':'
+ RIGHT(N'0' + CAST(DATEPART(n, StartTime) AS NVARCHAR(2)), 2)
+ N':'
+ RIGHT(N'0' + CAST(DATEPART(ss, StartTime) AS NVARCHAR(2)), 2)
, d.DatabaseName
, [AuditDBCCEventCount] = COUNT(CASE WHEN te.Name = 'Audit DBCC Event' THEN 1 ELSE NULL END)
, [ErrorLogCount] = COUNT(CASE WHEN te.Name = 'ErrorLog' THEN 1 ELSE NULL END)
, [LockTimeoutCount] = COUNT(CASE WHEN te.Name = 'Lock:Timeout' THEN 1 ELSE NULL END)
@bartread
bartread / indexscanstatistics.sql
Last active January 10, 2017 09:24
Get index scan statistics by object ID from SQL Profiler trace data
/*
T-SQL QUERY FOR INDEX SCAN STATISTICS FROM SQL PROFILER TRACE TABLE
Author: Bart Read
Copyright © 2016 bartread.com Ltd
License: MIT
This query gets the number of index scans by object ID, along with the total reads, and
average reads for those scans, as well as showing the time period over which the scans
@bartread
bartread / mapobjectidstoqualifiednames.sql
Created January 10, 2017 12:18
Map object IDs (e.g., retrieved from SQL Profiler trace table) to qualified object names
/*
This script maps object IDs retrieved from, for example, a SQL Profiler trace table
back onto their object names.
Many SQL Profiler events, such as Scan:Stopped, collect the object ID, but do not map
it back onto an object name. If your trace table isn't on the same instance (smart)
or the database to which the event pertains (again, smart).
The IDs in this script are for the purposes of illustration only.
@bartread
bartread / getalleventtypesrecorded.sql
Created January 10, 2017 14:00
Gets a list of all the distinct event types recorded in a SQL Profiler trace table.
SELECT DISTINCT te.name
FROM [YOUR_TRACE_SCHEMA_NAME].[YOUR_TRACE_TABLE_NAME] AS d
INNER JOIN sys.trace_events AS te
ON te.trace_event_id = d.EventClass;
@bartread
bartread / sqlprofileeventsrelatingtotempdbactivity.sql
Created January 10, 2017 17:36
Maps activity in tempdb over a given time period to events in SQL Profiler trace by Transaction ID to allow you to relate tempdb activity to activity in other databases.
/*
This query allows you to relate activity in tempdb to activity in other databases
where that activity has been recorded in a SQL Profiler trace table.
This can come in useful when, for example, diagnosing the source of I/O activity
and possible bottlenecking in tempdb.
*/
@bartread
bartread / mapallobjectidstoschemaandobjectnames.sql
Created January 10, 2017 20:21
Map all object IDs to schema and object names in database
SELECT o.[object_id] AS [ObjectID],
s.[name] AS [SchemaName],
o.[name] AS [ObjectName],
s.[name] + '.' + o.[name] AS [QualifiedName],
o.[type]
FROM sys.objects AS o
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
ORDER BY [QualifiedName];
@bartread
bartread / basicsqlserverosinformation.sql
Created January 10, 2017 20:29
Get basic SQL Server and OS information
SELECT [cpu_ticks] ,
[ms_ticks] ,
[cpu_count] ,
[hyperthread_ratio] ,
[physical_memory_kb] ,
[virtual_memory_kb] ,
[committed_kb] ,
[committed_target_kb] ,
[visible_target_kb] ,
[stack_size_in_bytes] ,