Skip to content

Instantly share code, notes, and snippets.

@OsirisDBA
OsirisDBA / gist:3c0c5f4d9445a3af958b9772ff29e7a9
Created January 3, 2024 21:05
Plan Summary for single query ( Resource Consumers, Memory Consumption, Total )
exec sp_executesql N'WITH
bucketizer as
(
SELECT
rs.plan_id as plan_id,
rs.execution_type as execution_type,
SUM(rs.count_executions) count_executions,
DATEADD(d, ((DATEDIFF(d, 0, rs.last_execution_time))),0 ) as bucket_start,
DATEADD(d, (1 + (DATEDIFF(d, 0, rs.last_execution_time))), 0) as bucket_end,
ROUND(CONVERT(float, SUM(rs.avg_query_max_used_memory*rs.count_executions))/NULLIF(SUM(rs.count_executions), 0)*8,2) as avg_query_max_used_memory,
@OsirisDBA
OsirisDBA / AutomaticSeedingStatus.sql
Created October 9, 2023 19:05
Status Query for SQL Server Availability Group Automatic Seeding
WITH x
AS ( SELECT local_database_name
, remote_machine_name
, role_desc
, internal_state_desc
, transfer_rate_bytes_per_second / 1024 / 1024 AS transfer_rate_MBps
, transferred_size_bytes / 1024 / 1024 AS transferred_size_MB
, database_size_bytes / 1024 / 1024 AS Database_Size_MB
, is_compression_enabled
FROM sys.dm_hadr_physical_seeding_stats )
@OsirisDBA
OsirisDBA / Tables By Size with Compression option
Last active October 19, 2022 14:44
This gives the command to compress SQL Server tables if they aren't already compressed. For SQL Express, remove the ONLINE=ON option
SELECT --TOP 100
s.name + '.' + t.Name AS [Table Name],
part.rows AS [Total Rows In Table - Modified],
CAST((SUM( DISTINCT au.Total_pages) * 8 ) / 1024.000 / 1024.000 AS NUMERIC(18, 3))
AS [Table's Total Space In GB],
'ALTER TABLE [' + s.name + '].[' + t.Name + '] REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE, ONLINE=ON);' AS [daSQL]
FROM
SYS.Tables t INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN SYS.Indexes idx ON t.Object_id = idx.Object_id
@OsirisDBA
OsirisDBA / sql_table_metadata.sql
Created August 16, 2021 15:41
SQL Server Table Metadata
DECLARE @SchemaName sysname = ''
, @TableName sysname = '';
WITH pk /* Primary Keys */
AS ( SELECT t.object_id
, pkc.column_id
, ic.index_column_id pk_index
, pk.name AS pk_name
FROM sys.tables t
JOIN sys.schemas s ON s.schema_id = t.schema_id
@OsirisDBA
OsirisDBA / Get-DBARegistryRoot example.ps1
Last active April 13, 2021 21:56
Remotely setting a SQLServerAgent registry value with dbatools help
import-module dbatools
$TargetServer = "MyRemoteComputer"
$regRoot = Get-DbaRegistryRoot -ComputerName $TargetServer
# Reading a key
Invoke-Command -ComputerName $regRoot.ComputerName -ScriptBlock {
param($r)
Get-ItemProperty -Path "$($r.RegistryRoot)\SQLServerAgent" -Name MSXEncryptChannelOptions
} -ArgumentList $regRoot
@OsirisDBA
OsirisDBA / AdventureWorks2017.puml
Created August 9, 2019 14:35
AdventureWorks2017 PlantUML ERD
@startuml
skinparam linetype ortho
hide circle
skinparam roundcorner 20
title AdventureWorks2017
Entity dbo.AWBuildVersion #62A9FF {
* "SystemInformationID" : tinyint
--
"Database Version" : nvarchar(25)
param(
$SqlInstance = 'SQLSRV01',
$Database = "AdventureWorks2017"
)
import-module dbatools;
if ( $SqlInstance.GetType() -ne 'Server' ) {
$inst = Connect-DbaInstance -SqlInstance $SqlInstance.ToString()
}
@OsirisDBA
OsirisDBA / TestLastBackup.ps1
Created January 15, 2019 22:11
Restore and DBCC testing with dbatools
import-module dbatools
$CMSInstance = "MyCMSInst"
$Test2014Instance = "SQL2014"
$Test2017Instance = "SQL2017"
$InventoryInst = "MYINV"
$InventoryDB = "DBATools"
$InventoryTable = "BackupTestResults"
Enum SQLVersionMajor {
@OsirisDBA
OsirisDBA / TestLastBackup.ps1
Created December 20, 2018 20:25
Testing last backup
import-module dbatools
Enum SQLVersionMajor {
SQL2019 = 15
SQL2017 = 14
SQL2016 = 13
SQL2014 = 12
SQL2012 = 11
SQL2008 = 10
SQL2005 = 9