Skip to content

Instantly share code, notes, and snippets.

View EitanBlumin's full-sized avatar
🕵️‍♂️
Figuring it out

Eitan Blumin EitanBlumin

🕵️‍♂️
Figuring it out
View GitHub Profile
@EitanBlumin
EitanBlumin / CaptureTSQLEvents_XE_Buffer.sql
Last active December 5, 2021 12:14
Collect T-SQL Events using an Extended Events Buffer
-- Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
-- Date: 2020-05-31
-- Last Update: 2021-04-22
-- Description: Collect T-SQL Events using an Extended Events Buffer
SET NOCOUNT ON;
DECLARE
@SourceLinkedServer SYSNAME
, @MinimumDurationMilliSeconds BIGINT
@EitanBlumin
EitanBlumin / Find SHRINK sessions with lock info.sql
Last active July 8, 2023 06:32
SQL queries to troubleshoot a long-running DBCC SHRINK operation
/*
Author: Eitan Blumin | https://www.eitanblumin.com
Create Date: 2020-03-18
Description:
This script will detect currently running sessions in your database which are running DBCC SHRINK commands.
It will also output the name of any tables and indexes the session is currently locking.
Use this query to find out what causes a SHRINK to run for too long.
You may need to run it multiple times to "catch" the relevant info.
Optionally, set @RunUntilCaughtLockInfo to 1 to continuously run until a session with object lock info was caught.
@EitanBlumin
EitanBlumin / visualize_page_allocation_compact.sql
Last active September 3, 2020 00:56
SQL Server Queries to Visualize Data Page Allocations (more info: https://github.com/EitanBlumin/mssql-data-allocation-report )
SELECT
databse_name = DB_NAME()
, file_name
, check_file_total_size = file_total_size
, check_file_total_used_space = file_total_used_space
, check_file_total_unused_pages = file_total_unused_pages
, agg_file_total_reserved_pages = file_total_reserved_pages
, agg_file_total_unused_pages = SUM(pt.consecutive_unused_pages) OVER (PARTITION BY file_id)
, pt.*
@EitanBlumin
EitanBlumin / ReNumber Identity Column.sql
Last active September 3, 2020 00:55
Re-number the identity column for a table that has very large number gaps
/*
Re-Number Identity Column
=================================
Author: Eitan Blumin | https://www.eitanblumin.com
Create Date: 2020-03-24
Description:
Use this script to re-number a table with an identity column, which has very large number gaps.
The specified parameter @ChunkSize must be smaller than the current minimum value
in the table.
*/
@EitanBlumin
EitanBlumin / CHECKDB on non-readable AG secondaries.sql
Last active September 3, 2020 00:52
Run DBCC CHECKDB on all databases which are either standalone, or SECONDARY in AG. Supports non-readable secondaries by creating DB snapshots.
/*
Author: Eitan Blumin (t: @EitanBlumin | b: eitanblumin.com)
Date: March, 2020
Description:
Run DBCC CHECKDB on all databases which are either standalone, or SECONDARY in AG.
Supports non-readable secondaries by creating DB snapshots.
*/
DECLARE @CurrDB SYSNAME, @IsInAG BIT, @CMD NVARCHAR(MAX);
-- Find all databases which are either standalone, or SECONDARY in AG
@EitanBlumin
EitanBlumin / Grow a database file in specified increments.sql
Last active September 3, 2020 00:51
Grow a database file in specified increments up to a specific size or percentage of used space
/*
----------------------------------------------------------------------------
Grow a Database File in Specified Increments
----------------------------------------------------------------------------
Author: Eitan Blumin | https://www.eitanblumin.com
Creation Date: 2020-03-30
----------------------------------------------------------------------------
Description:
This script uses small intervals to grow a file (in the current database)
@EitanBlumin
EitanBlumin / investigate AlwaysOn_health extended events.sql
Last active December 6, 2021 07:34
Investigate AlwaysOn_health extended events using T-SQL
/*
AlwaysOn Availability Group Error Events
========================================
Author: Eitan Blumin
Date: 2020-05-31
This alert check the contents of the AlwaysOn_Health extended events session for data suspension, role changes, and other errors.
For more info:
https://docs.microsoft.com/sql/database-engine/availability-groups/windows/always-on-extended-events
*/
@EitanBlumin
EitanBlumin / drop jobs and jobs_internal schemas.sql
Created April 3, 2020 10:21
Generate commands to drop the "jobs" and "jobs_internal" schemas and all of their objects
SELECT
'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(OBJECT_NAME(parent_object_id))
+ ' DROP CONSTRAINT ' + QUOTENAME(name)
FROM sys.foreign_keys
WHERE schema_id IN ( SCHEMA_ID('jobs'), SCHEMA_ID('jobs_internal') )
ORDER BY
CASE schema_id WHEN SCHEMA_ID('jobs') THEN 1 ELSE 2 END ASC
SELECT
'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name)
@EitanBlumin
EitanBlumin / detect table subsets.sql
Created April 3, 2020 10:43
Script to detect table subsets based on foreign key dependencies
DROP TABLE IF EXISTS #Tree;
CREATE TABLE #Tree
(
object_id INT PRIMARY KEY WITH(IGNORE_DUP_KEY=ON),
subset_group_id INT,
referenced_object_id INT NULL
);
-- Insert 1st level tables
INSERT INTO #Tree
@EitanBlumin
EitanBlumin / TempDB_GetAvailableSpace_adhoc.sql
Last active September 3, 2020 00:48
Inspect and return the current available space in TempDB, including available disk space for auto-growth
/*
Copyright 2020 @EitanBlumin, https://eitanblumin.com
Source: https://bit.ly/TempDBFreeSpace
Full URL: https://gist.github.com/EitanBlumin/afed2587e89e260698c4753fcc5d1917
License: MIT (https://opensource.org/licenses/MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: