Skip to content

Instantly share code, notes, and snippets.

View ghotz's full-sized avatar

Gianluca Hotz ghotz

View GitHub Profile
@ghotz
ghotz / rs-get-exec-hourly.sql
Created December 21, 2015 07:37
Reporting Services execution log queries
WITH cte AS
(
SELECT *
, AdditionalInfo.value('(/AdditionalInfo/ScalabilityTime/Pagination)[1]', 'bigint') AS pagination_scalability_ms
, AdditionalInfo.value('(/AdditionalInfo/EstimatedMemoryUsageKB/Pagination)[1]', 'bigint') AS pagination_estimated_kb
, AdditionalInfo.value('(/AdditionalInfo/ScalabilityTime/Rendering)[1]', 'bigint') AS rendering_scalability_ms
, AdditionalInfo.value('(/AdditionalInfo/EstimatedMemoryUsageKB/Rendering)[1]', 'bigint') AS rendering_estimated_kb
, AdditionalInfo.value('(/AdditionalInfo/ScalabilityTime/Processing)[1]', 'bigint') AS processing_scalability_ms
, AdditionalInfo.value('(/AdditionalInfo/EstimatedMemoryUsageKB/Processing)[1]', 'bigint') AS processing_estimated_kb
FROM ExecutionLog2
@ghotz
ghotz / gen-columnstore-rebuild.sql
Created December 21, 2015 14:51
Generate various maintenance commands
SELECT 'ALTER INDEX [' + I1.name + '] ON [' + SCHEMA_NAME(O1.[schema_id]) + '].[' + O1.name + '] REBUILD;'
FROM sys.indexes AS I1
JOIN sys.objects AS O1
ON I1.[object_id] = O1.[object_id]
WHERE I1.type_desc = 'NONCLUSTERED COLUMNSTORE'
# Set culture information
$currentThread=[System.Threading.Thread]::CurrentThread
$culture=[System.Globalization.CultureInfo]::GetCultureInfo("en-US")
$currentThread.CurrentCulture=$culture
$currentThread.CurrentUICulture=$culture
Set-Culture $culture
dir *.blg | % { relog.exe "$_" -f SQL -o "SQL:Relog!$($_.BaseName)" }
@ghotz
ghotz / last-backups.sql
Last active January 24, 2021 12:22
Last 3 backups per database
WITH BackupSets AS (
SELECT database_name
, CASE [type]
WHEN 'D' THEN 'Full'
WHEN 'I' THEN 'Differential'
WHEN 'L' THEN 'Log'
WHEN 'F' THEN 'File/Filegroup'
END backup_type
, backup_start_date, backup_finish_date, physical_device_name
, ROW_NUMBER() OVER (PARTITION BY database_name, [type] ORDER BY backup_start_date DESC) as rn
--
-- Generate Columnstore indexes based on all columns in a table
--
SELECT
'CREATE COLUMNSTORE INDEX [ci_' + T1.name + '] ON [' + S1.name + '].[' + T1.name + '] ('
+ STUFF(
(
SELECT DISTINCT ',[' + C1.name + ']'
FROM sys.columns AS C1
WHERE T1.object_id = C1.object_id
@ghotz
ghotz / who-is-active-no-broker.sql
Created November 23, 2017 18:09
Call sp_whoisactive filtering broker
DECLARE @schema VARCHAR(MAX)
EXEC sp_whoisactive @return_schema = 1, @schema = @schema OUTPUT
SET @schema = 'IF object_id(''tempdb..##tmp'') IS NOT NULL DROP TABLE ##tmp; ' + REPLACE(@schema, '<table_name>', '##tmp')
EXEC (@schema)
EXEC sp_whoisactive @destination_table = '##tmp'
SELECT *
FROM ##tmp
WHERE wait_info NOT LIKE '%BROKER_RECEIVE_WAITFOR%'
OR wait_info IS NULL
@ghotz
ghotz / get-plan-example.ps1
Created December 6, 2017 09:05
Execute query with SqlCmd variables and save SQL Server execution plan example
$SqlCmdVariables = "DatabaseName=master", "ColumnName=name";
$Results = (Invoke-Sqlcmd -ServerInstance "localhost\sql2017" -Query "SET STATISTICS XML ON; SELECT `$(ColumnName) FROM sys.databases WHERE [name] = '`$(DataBaseName)';" -Variable $SqlCmdVariables -MaxCharLength 10MB);
$Plan = [xml]$Results[$Results.GetUpperBound(0)].Item(0);
$Plan.Save("C:\temp\test.sqlplan");
@ghotz
ghotz / predict-disk-failure.ps1
Created December 16, 2017 18:04
Predict disk failure through S.M.A.R.T.
Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ErrorAction Silentlycontinue | Select InstanceName, PredictFailure, Reason | Format-Table –Autosize
@ghotz
ghotz / jobs-update-enabled.sql
Created February 2, 2018 10:41
SQL Agent Jobs enable/disable
-- Generates enabled/disabled updates for all jobs with their original setting
SELECT 'EXEC msdb.dbo.sp_update_job @job_name = ''' + REPLACE(J1.[name], '''', '''''') + ''', @enabled = ' + CAST(J1.[enabled] AS varchar(1)) + ' ;' AS SqlCmd
FROM msdb.dbo.sysjobs AS J1
ORDER BY J1.[name]
-- Generates enabled updates for all jobs
SELECT 'EXEC msdb.dbo.sp_update_job @job_name = ''' + REPLACE(J1.[name], '''', '''''') + ''', @enabled = 0;' AS SqlCmd
FROM msdb.dbo.sysjobs AS J1
ORDER BY J1.[name]