Skip to content

Instantly share code, notes, and snippets.

View BrentOzar's full-sized avatar
❤️

Brent Ozar BrentOzar

❤️
View GitHub Profile
@BrentOzar
BrentOzar / Conversation.tsql
Created January 11, 2024 18:44
Triggers to keep Posts.CommentCount in sync
/* What sent to ChatGPT 4: */
You are a T-SQL database developer working with Microsoft SQL Server 2019. Given these two tables:
CREATE TABLE [dbo].[Comments](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreationDate] [datetime] NOT NULL,
[PostId] [int] NOT NULL,
[Score] [int] NULL,
[Text] [nvarchar](700) NOT NULL,
/* SQL Server Statistics Explained with Playing Cards
v0.1 - 2020-08-14
https://BrentOzar.com/go/learnstats
This first RAISERROR is just to make sure you don't accidentally hit F5 and
run the entire script. You don't need to run this:
*/
RAISERROR(N'Oops! No, don''t just hit F5. Run these demos one at a time.', 20, 1) WITH LOG;
GO
@BrentOzar
BrentOzar / schema.sql
Created October 21, 2022 16:50
Stack Overflow Data Dump Schema
USE [StackOverflow]
GO
/****** Object: Table [dbo].[Badges] Script Date: 10/21/2022 12:48:56 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Badges]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Badges](
CREATE DATABASE [٠০౦০٠];
GO
USE [٠০౦০٠];
GO
CREATE SCHEMA [٠০౦০٠];
GO
CREATE TABLE [٠০౦০٠].[٠০౦০٠]([٠০౦০٠] NVARCHAR(20), [۰০౦০٠] NVARCHAR(20), [٠০౦০۰] NVARCHAR(20), [۰০౦০۰] NVARCHAR(20));
GO
CREATE UNIQUE CLUSTERED INDEX [٠০౦০٠] ON [٠০౦০٠].[٠০౦০٠]([٠০౦০٠], [۰০౦০٠], [٠০౦০۰], [۰০౦০۰]);
GO
USE StackOverflow;
GO
DROP TABLE IF EXISTS dbo.UsersMemberships;
CREATE TABLE dbo.UsersMemberships
(Id INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED,
UserId INT NOT NULL,
StartDate DATETIME NOT NULL,
EndDate DATETIME NOT NULL,
CancelledEarlyDate DATETIME NULL);
@BrentOzar
BrentOzar / PlanCacheByDateAndHour.sql
Created July 3, 2018 11:28
Report on SQL Server's plan cache history over time.
/*
SQL Server Plan Cache History by Date & Time
Brent Ozar, 2018/07/03 - https://BrentOzar.com/go/plansbydate
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
@BrentOzar
BrentOzar / TopQueries.sql
Last active July 4, 2021 16:04
Some of the top StackOverflow queries from http://data.stackexchange.com. Great for demoing wide SELECT workloads.
USE StackOverflow;
GO
IF OBJECT_ID('dbo.usp_Q7521') IS NULL
EXEC ('CREATE PROCEDURE dbo.usp_Q7521 AS RETURN 0;')
GO
ALTER PROC dbo.usp_Q7521 @UserId INT AS
BEGIN
/* Source: http://data.stackexchange.com/stackoverflow/query/7521/how-unsung-am-i */
ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 140;
GO
CREATE OR ALTER FUNCTION dbo.Test()
RETURNS INT AS
BEGIN
DECLARE @i BIGINT;
SELECT TOP 1 @i = CHECKSUM(*)
FROM master.dbo.spt_values;
RETURN 1;
@BrentOzar
BrentOzar / sys.index_resumable_operations.sql
Created February 20, 2020 14:25
Showing the (in)accuracy of sys.index_resumable_operations.
DROP TABLE IF EXISTS dbo.DiningRoomTable;
GO
CREATE TABLE dbo.DiningRoomTable (Id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, Stuffing CHAR(1000));
INSERT INTO dbo.DiningRoomTable (Stuffing)
SELECT 'Stuff'
FROM sys.messages;
GO
CREATE INDEX IX_Stuffing ON dbo.DiningRoomTable(Stuffing)
WITH (ONLINE = ON, RESUMABLE = ON);
@BrentOzar
BrentOzar / How to Make SELECT COUNT(*) Queries Crazy Fast.sql
Created December 22, 2019 15:10
How to Make SELECT COUNT(*) Queries Crazy Fast.sql
USE StackOverflow;
GO
DropIndexes;
GO
ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 140;
GO
/* Check the table's size: */
sp_BlitzIndex @TableName = 'Votes';
GO