Skip to content

Instantly share code, notes, and snippets.

View ghotz's full-sized avatar

Gianluca Hotz ghotz

View GitHub Profile
@ghotz
ghotz / dump_datasources.ps1
Last active March 27, 2020 15:48
Dump Reporting Services Data Sources used in all reports to CSV
# Install-Module ReportingServicesTools; # https://github.com/microsoft/ReportingServicesTools
Import-Module ReportingServicesTools;
$rsServerName = "localhost";
$rsAllContent = Get-RsFolderContent -RsFolder "/" -ReportServerUri "http://$rsServerName/ReportServer" -Recurse;
$rsAllReports = $rsAllContent | ? { $_.TypeName -eq "Report" } #| Select * -First 5;
$rsAllReports | % {
$ReportName = $_.Path;
$rsAllReportsDS = Get-RsItemDataSource -RsItem $_.Path -ReportServerUri "http://$rsServerName/ReportServer";
$rsAllReportsDS | % {
@ghotz
ghotz / quick-test-table.sql
Created October 9, 2019 14:59
Create a table for quick testing with random data
DROP TABLE IF EXISTS dbo.test;
CREATE TABLE dbo.Test (
ID int IDENTITY(1, 1) NOT NULL PRIMARY KEY
, PayLoad nvarchar(1000) NOT NULL DEFAULT (REPLICATE(CHAR(33 + ABS(CHECKSUM(NEWID())) % 90), ABS(CHECKSUM(NEWID())) % 1000))
);
GO
SET NOCOUNT ON;
DECLARE @i int = 10000;
WHILE @i > 0
BEGIN
@ghotz
ghotz / sys-health-memory-broker.sql
Created August 14, 2019 15:04
Various memory troubleshooting queries on System Health session files
-- get memory broker events from system health file session
IF OBJECT_ID('tempdb..#events') IS NOT NULL
DROP TABLE #events;
WITH XmlDataSet AS
(
SELECT CAST(xe.event_data AS xml) AS XMLDATA
FROM sys.fn_xe_file_target_read_file('system_health_*.xel',NULL,NULL,NULL) as xe
WHERE xe.[object_name] = 'memory_broker_ring_buffer_recorded'
)
@ghotz
ghotz / compress-pal.ps1
Created August 6, 2019 11:55
Compress Perfmon BLG files
dir C:\PerfLogs\Admin\PAL | % { Compress-Archive -Path $_.FullName -CompressionLevel Optimal -DestinationPath (Join-Path $_.DirectoryName $_.BaseName) }
@ghotz
ghotz / clear-broker-queue.sql
Last active July 25, 2019 20:27 — forked from jdaigle/gist:2781349
SQL Server Service Broker: Short Script to clear empty a named queue
DECLARE @handle UNIQUEIDENTIFIER;
WHILE (SELECT COUNT(*) FROM NameOfQueue) > 0
BEGIN
RECEIVE TOP (1) @handle = conversation_handle FROM NameOfQueue;
END CONVERSATION @handle WITH CLEANUP
END
@ghotz
ghotz / get-users-default-schema-alldbs.sql
Last active April 17, 2019 17:14
Get all users with a non standard default schema for all databases
EXEC sp_MSforeachdb '
USE [?];
SELECT DB_NAME() AS database_name, name AS principal_name, default_schema_name
FROM sys.database_principals
WHERE default_schema_name NOT IN (''dbo'', ''guest'');
';
@ghotz
ghotz / generate-query-store-activate.sql
Last active April 11, 2019 14:45
Generate Query Store activation commands for multiple databases
WITH DBsize AS
(
SELECT D1.[name] AS DatabaseName, SUM(CAST(F1.size as bigint)) * 8192 / 1024 / 1024 AS DatabaseSizeMB
FROM sys.databases AS D1
JOIN sys.master_files AS F1
ON D1.database_id = F1.database_id
WHERE D1.database_id > 4 AND F1.type_desc = 'ROWS'
GROUP BY D1.[name]
)
, SizeRules AS
@ghotz
ghotz / find-forced-plan-not-being-forced-no-explanation.sql
Created April 10, 2019 08:55
Finds in Query Store forced plan not being forced and without an explanation
SELECT I.start_time, I.end_time, P.query_id, p.plan_id
, (SELECT P2.plan_id
FROM sys.query_store_plan AS P2
WHERE P.query_id = P2.query_id
AND P.plan_id <> P2.plan_id
AND P.last_force_failure_reason = 0
) AS plan_id_different
FROM sys.query_store_runtime_stats_interval AS I
JOIN sys.query_store_runtime_stats AS S
ON I.runtime_stats_interval_id = S.runtime_stats_interval_id
@ghotz
ghotz / single-plan-usage.sql
Created January 26, 2019 10:31
Analyze single plan usage
SELECT
objtype
, COUNT_BIG(*) AS plans_count
, SUM(CAST(size_in_bytes AS bigint) / 1024. / 1024.) AS plans_MB
, AVG(usecounts) AS plans_count_avg
, SUM(CASE WHEN usecounts = 1 THEN 1 ELSE 0 END) AS plans_single_use_count
, SUM(CASE WHEN usecounts = 1 THEN CAST(size_in_bytes AS bigint) / 1024. / 1024. ELSE 0 END) AS plans_single_use_MB
FROM sys.dm_exec_cached_plans
GROUP BY objtype
ORDER BY plans_single_use_MB DESC
@ghotz
ghotz / temp-table-stats.sql
Created October 22, 2018 10:17
Get info about temp table statistics
SELECT S1.name AS stats_name, C1.name AS column_name
FROM tempdb.sys.stats AS S1
JOIN tempdb.sys.stats_columns AS S2
ON S1.object_id = S2.object_id
AND S1.stats_id = S2.stats_id
JOIN tempdb.sys.all_columns AS C1
ON S2.object_id = C1.object_id
AND S2.column_id = C1.column_id
WHERE S1.[object_id] = OBJECT_ID('tempdb..#ftplog_t_dati_1')
ORDER BY