Skip to content

Instantly share code, notes, and snippets.

@chadbaldwin
chadbaldwin / Actual execution plan - Bad.sqlplan
Last active September 29, 2025 16:32
SQL Server 2022 Bug - Bad plan received with new CE for indexed views
<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.564" Build="16.0.4205.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementCompId="2" StatementEstRows="1" StatementId="1" StatementOptmLevel="FULL" CardinalityEstimationModelVersion="160" StatementSubTreeCost="126.466" StatementText="UPDATE dbo.vw_FooFiltered SET Code = 'ABC' WHERE FooID = @FooID" StatementType="UPDATE" QueryHash="0xD280ECD9D3E6F9E2" QueryPlanHash="0x45FC3E4D8415E259" RetrievedFromCache="true" StatementSqlHandle="0x09009E9BB574372BDDFB48BD416DC587C0EE0000000000000000000000000000000000000000000000000000" DatabaseContextSettingsId="1" ParentObjectId="0" StatementParameterizationType="1" SecurityPolicyApplied="false">
<StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="false" CONCAT_NULL
@chadbaldwin
chadbaldwin / SSMS Clicky Column.sql
Last active September 18, 2025 20:51
Create an XML clickable column in SSMS
DROP TABLE IF EXISTS #clicky_data;
CREATE TABLE #clicky_data (
random_text nvarchar(MAX) NULL,
);
INSERT #clicky_data (random_text)
VALUES (OBJECT_DEFINITION(-495130372)) -- sys.dm_db_partition_stats
, (OBJECT_DEFINITION(-411581074)) -- sys.dm_exec_query_stats
-- , (N'This is a ?> test <? bleh')
, (N'This '+NCHAR(0)+' is a '+NCHAR(18)+N' test '+NCHAR(20)+N' bleh')
import re
from typing import Dict, List, Optional, Union
from dataclasses import dataclass
@dataclass
class BoxImport:
"""Represents a single import statement within box::use()"""
module: str
alias: Optional[str] = None
functions: Optional[List[str]] = None
@chadbaldwin
chadbaldwin / New Index TX Log Impact Testing.sql
Created March 26, 2025 21:25
New Index TX Log Impact Testing
------------------------------------------------------------
GO
------------------------------------------------------------
SELECT [CurrentDB] = (SELECT x.recovery_model_desc FROM sys.databases x WHERE [name] = DB_NAME())
------------------------------------------------------------------------------
GO
------------------------------------------------------------------------------
IF OBJECT_ID('tempdb..#tmp_log_track','U') IS NOT NULL DROP TABLE #tmp_log_track; --SELECT * FROM #tmp_log_track
CREATE TABLE #tmp_log_track (
ID int NOT NULL IDENTITY(1,1),
@chadbaldwin
chadbaldwin / TableSize.sql
Last active October 13, 2024 07:40
DBADash - Improve performance of TableSize report
------------------------------------------------------------------------------
GO
------------------------------------------------------------------------------
DROP VIEW IF EXISTS dbo.vw_TableSize_Snapshots;
GO
CREATE OR ALTER VIEW dbo.vw_TableSize_Snapshots
WITH SCHEMABINDING
AS
SELECT InstanceID, DatabaseID, SnapshotDate, ObjectCount = COUNT_BIG(*)
FROM dbo.TableSize
@chadbaldwin
chadbaldwin / feedback test results.md
Last active August 23, 2024 22:00
SQL Feedback: Odd (and inconsistent) behaviour with datetime/datetime2 comparisons
@chadbaldwin
chadbaldwin / gist:a6f17da40d6696a4febb9c3de3fb521f
Last active February 5, 2024 21:46
SQL Server Suggestions
Add some sort of "reset_time" or similar to Index stats DMVs
https://feedback.azure.com/d365community/idea/e9e84bf2-64c4-ee11-92bc-000d3a0fb290
Output warning message when altering an indexed view that all indexes (clustered and non-clustered will be dropped)
https://feedback.azure.com/d365community/idea/0f1f2993-43b6-ee11-92bc-000d3a033659
Statement execution duration global variable - Similar to @@ROWCOUNT, but instead how long the previous statement took to run
https://feedback.azure.com/d365community/idea/884c86b1-9b85-ee11-a81c-000d3ae5ae95
# Run this script from within the git repo you want to copy data OUT of
$copyTo = 'C:\MyTestFolder'
$ErrorActionPreference = 'Stop'
$currBranch = git branch --show-current
$repoRoot = git rev-parse --show-toplevel
if ($LASTEXITCODE -gt 0) { return }