Skip to content

Instantly share code, notes, and snippets.

View Otterpohl's full-sized avatar
🐌

Tristan Otterpohl Otterpohl

🐌
  • London
  • 10:32 (UTC +01:00)
View GitHub Profile
@Otterpohl
Otterpohl / ConvertFrom-DeadlockXEL.sql
Last active May 13, 2022 13:44
Query to Parse Deadlock Extended Event Data
IF(OBJECT_ID('tempdb..#Tempxel') IS NOT NULL)
BEGIN
Drop Table #Tempxel
END
CREATE TABLE #Tempxel(XEL_XML XML)
IF(OBJECT_ID('tempdb..#Tempxml') IS NOT NULL)
BEGIN
DROP TABLE #Tempxml
END
@Otterpohl
Otterpohl / Get-AgentJobFailedText.sql
Last active March 10, 2023 18:56
Fetches the full failed text from an Agent Job
SELECT CAST(RIGHT(JH.run_date, 2) AS CHAR(2)) + '/' + CAST(SUBSTRING(CAST(JH.run_date AS CHAR(8)), 5, 2) AS CHAR(2)) + '/' + CAST(LEFT(JH.run_date, 4) AS CHAR(4)) AS date_run
,LEFT(RIGHT('0' + CAST(JH.run_time AS VARCHAR(6)), 6), 2) + ':' + SUBSTRING(RIGHT('0' + CAST(JH.run_time AS VARCHAR(6)), 6), 3, 2) + ':'
+ LEFT(RIGHT('0' + CAST(JH.run_time AS VARCHAR(6)), 6), 2) AS time_run
,JS.step_name
,JH.run_duration
,CASE
WHEN JSL.log IS NULL THEN JH.message
ELSE JSL.log
END AS log_output
FROM msdb.dbo.sysjobsteps AS JS
@Otterpohl
Otterpohl / Get-AgentJobRunTime.sql
Last active May 19, 2022 08:50
Gets the Agent Job Run duration without silly date and time
SELECT run_duration
,msdb.dbo.agent_datetime(run_date, run_time) AS datetime
FROM msdb.dbo.sysjobhistory
WHERE step_name = 'Enter Job Step Name Here'
ORDER BY msdb.dbo.agent_datetime(run_date, run_time) ASC;
@Otterpohl
Otterpohl / Get-BlockingTree.sql
Created May 13, 2022 13:43
Generates Blocking Tree in Table Format
/*
https://blog.sqlauthority.com/2020/04/20/sql-server-blocking-tree-identifying-blocking-chain-using-sql-scripts/
*/
IF OBJECT_ID('tempdb..#Blocks') IS NOT NULL
DROP TABLE #Blocks
SELECT spr.spid
,spr.blocked
,REPLACE (REPLACE (st.TEXT, CHAR(10), ' '), CHAR (13), ' ' ) AS batch
@Otterpohl
Otterpohl / Get-DatabaseBufferCacheStatus.sql
Created May 13, 2022 13:58
Gets how much data is stored in cache per table
/*
https://blog.sqlauthority.com/2010/06/17/sql-server-data-pages-in-buffer-pool-data-stored-in-memory-cache/
*/
SELECT COUNT(*) AS cached_pages_count
,obj.name
,obj.index_id
,i.name AS IndexName
FROM sys.dm_os_buffer_descriptors AS bd
INNER JOIN
@Otterpohl
Otterpohl / Get-ConnectionsByDatabase.sql
Created May 13, 2022 14:04
Gets number of connections per database
DECLARE @temp_sp_who2 TABLE
(
SPID INT NULL,
Status VARCHAR(1000) NULL,
Login sysname NULL,
HostName sysname NULL,
BlkBy sysname NULL,
DBName sysname NULL,
Command VARCHAR(1000) NULL,
CPUTime INT NULL,
@Otterpohl
Otterpohl / Get-ServerDiskStats.sql
Created May 13, 2022 14:07
Gets the servers disk statistics and details
SELECT DB_NAME(a.database_id) AS [Database Name],
b.name + N' [' + b.type_desc COLLATE SQL_Latin1_General_CP1_CI_AS + N']' AS [Logical File Name],
UPPER(SUBSTRING(b.physical_name, 1, 2)) AS [Drive],
CAST(((a.size_on_disk_bytes / 1024.0) / (1024.0 * 1024.0)) AS DECIMAL(9, 2)) AS [Size (GB)],
a.io_stall_read_ms AS [Total IO Read Stall],
a.num_of_reads AS [Total Reads],
CASE
WHEN a.num_of_bytes_read > 0 THEN
CAST(a.num_of_bytes_read / 1024.0 / 1024.0 / 1024.0 AS NUMERIC(23, 1))
ELSE
@Otterpohl
Otterpohl / Get-MemorySummary.sql
Created May 13, 2022 14:09
Gets a high level summary of memory usage
SELECT total_physical_memory_kb / 1024 AS [Physical Memory (MB)],
available_physical_memory_kb / 1024 AS [Available Memory (MB)],
total_page_file_kb / 1024 AS [Total Page File (MB)],
available_page_file_kb / 1024 AS [Available Page File (MB)],
system_cache_kb / 1024 AS [System Cache (MB)],
system_memory_state_desc AS [System Memory State]
FROM sys.dm_os_sys_memory;
@Otterpohl
Otterpohl / Get-MemoryTree.sql
Created May 13, 2022 14:17
Get detailed memory information
/*
https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/SQLServer_Memory_Information.sql
*/
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET LOCK_TIMEOUT 10000;
DECLARE @ServiceName NVARCHAR(100);
SET @ServiceName = CASE
@Otterpohl
Otterpohl / Get-OrphanedSIDS.sql
Created May 13, 2022 14:19
Gets users who have been orphaned
SELECT dp.name AS [user_name],
dp.type_desc AS [user_type],
ISNULL(sp.name, 'Orphaned!') AS [login_name],
sp.type_desc AS [login_type]
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp
ON (dp.sid = sp.sid)
WHERE dp.type IN ( 'S', 'U', 'G' )
AND dp.principal_id > 4
ORDER BY sp.name;