Skip to content

Instantly share code, notes, and snippets.

Avatar

J Rokicki DataSic

View GitHub Profile
@DataSic
DataSic / Allocation Units
Created Apr 20, 2014
[SqlServer,SqlOs] Allocation Units per object. OUTER APPLY / TOP(1) pattern.
View Allocation Units
DECLARE @ObjectId INT
SET @ObjectId = OBJECT_ID('SchemaName.TableName')
SELECT AU.*,
COALESCE(P1.object_id, P2.object_id) OBJECT_ID,
COALESCE(P1.index_id, P2.index_id) INDEX_ID
FROM sys.allocation_units AU
OUTER APPLY (SELECT TOP(1) P.object_id,
P.index_id
FROM sys.partitions P
@DataSic
DataSic / Latch Contention in TempDB
Created Apr 22, 2014
[SqlServer,SqlOs] Script to identify latch contention in TempDB
View Latch Contention in TempDB
;WITH TasksCTE
AS (SELECT session_id,
wait_type,
wait_duration_ms,
blocking_session_id,
resource_description,
PageID = CAST(Right(resource_description, LEN(resource_description)-
CHARINDEX(':', resource_description, 3)) As Int)
FROM sys.dm_os_waiting_tasks WITH (NOLOCK)
WHERE wait_type Like 'PAGE%LATCH_%'
@DataSic
DataSic / WorkloadGenerator.sql
Last active Dec 17, 2017
[SqlServer,Dev] Workload Generator
View WorkloadGenerator.sql
;WITH EdgeCTE
AS
(
SELECT parent_object_id Object1,
referenced_object_id Object2,
'[' + OBJECT_SCHEMA_NAME(FK.parent_object_id) + '].[' + OBJECT_NAME(FK.parent_object_id) + '] [T#L#1]' Table1,
'[' + OBJECT_SCHEMA_NAME(FK.referenced_object_id) + '].[' + OBJECT_NAME(FK.referenced_object_id) + '] [T#L#2]' Table2,
(SELECT CASE WHEN ROW_NUMBER() OVER(ORDER BY FKS.constraint_column_id) > 1 THEN ' AND ' ELSE '' END +
'[T#L#1].[' + C1.name + '] = ' +
'[T#L#2].[' + C2.name + ']' [text()]
View AzureDW-GetLastAvailableGeoBackupDate.ps1
try
{
# Built-in account
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
# Init variables (Automation Account variables)
$ResourceGroup = Get-AutomationVariable -Name 'ResourceGroup'
$SqlServerName = Get-AutomationVariable -Name 'SqlServerName'
$DatabaseName = Get-AutomationVariable -Name 'DatabaseName'
# Log into Azure with AzureRunAsConnection
@DataSic
DataSic / QS Wait Stats duplicates
Last active Dec 18, 2017
QueryStore Wait Stats duplicates
View QS Wait Stats duplicates
USE [WWI]
GO
ALTER DATABASE CURRENT SET QUERY_STORE (OPERATION_MODE = READ_WRITE,
QUERY_CAPTURE_MODE = ALL,
INTERVAL_LENGTH_MINUTES = 60)
ALTER DATABASE CURRENT SET QUERY_STORE CLEAR;
GO
SELECT *
FROM [Sales].[CustomerTransactions]
ORDER BY [TaxAmount] DESC
@DataSic
DataSic / QS Wait Stats aggregation
Last active Dec 18, 2017
Query Store Wait Stats aggregation
View QS Wait Stats aggregation
SELECT w.plan_id,
w.runtime_stats_interval_id,
w.execution_type,
w.wait_category,
w.wait_category_desc,
SUM(w.total_query_wait_time_ms) total_query_wait_time_ms,
AVG(w.avg_query_wait_time_ms) avg_query_wait_time_ms,
MIN(w.min_query_wait_time_ms) min_query_wait_time_ms,
MAX(w.max_query_wait_time_ms) max_query_wait_time_ms
FROM sys.query_store_wait_stats w
@DataSic
DataSic / QS Wait Stats Categories
Last active Dec 19, 2017
Query Store Wait Stats Categories
View QS Wait Stats Categories
SELECT *
FROM (VALUES (0,'Unknown','Unknown'),
(1,'CPU','SOS_SCHEDULER_YIELD'),
(2,'Worker Thread','THREADPOOL'),
(3,'Lock','LCK_M_%'),
(4,'Latch','LATCH_%'),
(5,'Buffer Latch','PAGELATCH_%'),
(6,'Buffer IO','PAGEIOLATCH_%'),
(7,'Compilation*','RESOURCE_SEMAPHORE_QUERY_COMPILE'),
(8,'SQL CLR','CLR%, SQLCLR%'),
@DataSic
DataSic / CosmosDB.Firewall.ps1
Created May 29, 2018
CosmosDB - Add Firewall Rule
View CosmosDB.Firewall.ps1
<#
https://docs.microsoft.com/en-us/azure/templates/microsoft.documentdb/databaseaccounts
#>
# Init variables
$resourceGroupName = "ResourceGroupName"
$resourceType = "Microsoft.DocumentDB/databaseAccounts"
$apiVersion = "2015-04-08"
$cosmosDbAccountName = "CosmosDbAccountName"
$propertyObject = @{"databaseAccountOfferType"="Standard";
"ipRangeFilter"="IpAddress1,IpAddress2";}
@DataSic
DataSic / AzureSqlPasS.Firewall.ps1
Last active May 31, 2018
AzureSqlPasS - Add Firewall Rule
View AzureSqlPasS.Firewall.ps1
<#
https://docs.microsoft.com/en-us/powershell/module/azurerm.sql/new-azurermsqlserverfirewallrule
#>
# Init variables
$resourceGroupName = "ResourceGroupName"
$serverName = "ServerName"
$firewallRuleName = "ServerFirewallRuleName"
# Add firewall rules
$newAzureRmSqlServerFirewallRuleSplat = @{
@DataSic
DataSic / AzureSqlPass.Firewall.sql
Created May 29, 2018
AzureSqlPass - Add Firewall Rule tSQL
View AzureSqlPass.Firewall.sql
/*
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-set-database-firewall-rule-azure-sql-database
*/
--[DatabaseName] context
EXEC sp_set_database_firewall_rule @name = N'NewDatabaseFirewallRule',
@start_ip_address = 'IpAddressStart',
@end_ip_address = 'IpAddressEnd'
/*
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-set-firewall-rule-azure-sql-database
You can’t perform that action at this time.