Skip to content

Instantly share code, notes, and snippets.

View tcartwright's full-sized avatar

Tim Cartwright tcartwright

  • Houston, Texas
View GitHub Profile
@tcartwright
tcartwright / CalculateNibblingDeletesBatchSize.sql
Created November 13, 2025 19:17
SQL SERVER: Calculate optimum batch size for nibbling deletes based on rows per page
-- Calculate optimal batch size based on actual rows per page
DECLARE @RowsPerPage INT
DECLARE @MaxLocks INT = 4000 -- 80% of 5000 threshold
DECLARE @OptimalBatchSize INT
-- Get rows per page
SELECT @RowsPerPage =
CASE
WHEN SUM(used_page_count) > 0
THEN SUM(row_count) / SUM(used_page_count)
@tcartwright
tcartwright / CreateAppRoles.sql
Last active October 30, 2025 14:43
SQL SERVER: Create AppUser and AppUserReadOnly roles, and map logins to roles
DECLARE @DatabaseName NVARCHAR(128)
DECLARE @SQL NVARCHAR(MAX)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM sys.databases
WHERE database_id > 4 -- Excludes system databases (master, tempdb, model, msdb)
AND state_desc = 'ONLINE'
AND [name] NOT IN ('DBATools')
AND is_read_only = 0
@tcartwright
tcartwright / GetTableDataDistribution.sql
Last active October 23, 2025 13:29
SQL SERVER: Get tables data distribution for help with index creation
DECLARE @table SYSNAME = '[dbo].[TableName]';
DECLARE @sql NVARCHAR(MAX) = N'';
DECLARE @RowCount BIGINT;
-- Get total row count once (no need to scan per column)
SELECT @RowCount = SUM(p.[rows])
FROM sys.partitions AS p
WHERE p.[object_id] = OBJECT_ID(@table)
AND p.[index_id] IN (0, 1); -- heap or clustered index
@tcartwright
tcartwright / IndexColumnAnalysis.sql
Last active October 22, 2025 14:10
SQL SERVER: Index column analysis
/*
-- Brent Ozars script:
EXEC dbo.sp_BlitzIndex @SchemaName='dbo', @TableName='TableName', @fullOutput = 1
*/
-- makes it very easy to spot possible duplicates that can be combined.
SELECT
[i].[name] AS IndexName,
[i].[type_desc] AS IndexType,
STUFF(
@tcartwright
tcartwright / ResetChanges.cmd
Created October 16, 2025 18:59
CMD: Git undo changes to a file when git or visual studio wont let you
git rm .gitattributes # Removes the .gitattributes file from both the working directory and the Git index (staging area).
git add -A # Stages all changes: added files, modified files, and deletions (like the .gitattributes removal) for the next commit.
git reset --hard # Resets the working directory and index to the last committed state — all staged and unstaged changes are lost.
@tcartwright
tcartwright / TestSQLServerConnectivity.ps1
Last active August 28, 2025 13:16
SQL SERVER: Test connectivity to server from sql server
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
/************************************************************
** Change the value of the @server variable here
*************************************************************/
DECLARE @Server VARCHAR(255) = 'servername'
@tcartwright
tcartwright / GenerateCert.ps1
Created August 25, 2025 16:05
POWERSHELL: Generate a cert
# IMPORTANT: !!Generated certs should NOT be used in production!!
$cert = New-SelfSignedCertificate `
-Subject "CN=sso.servername.it" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-NotAfter (Get-Date).AddYears(5) `
-KeyExportPolicy Exportable `
-Type Custom `
-TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3") # Code signing usage
@tcartwright
tcartwright / CreateFKIndexes.sql
Last active August 22, 2025 21:03
CreateFKIndexes.sql
/*
ORIGINAL SCRIPT: https://lukelowrey.com/sql-server-script-to-automatically-create-indexes-for-foreign-keys/
TDC: script modifications
- include the schema name when creating the index
- switched to inner join syntax
- switched to using concat for string concatenation
- general cleanup
*/
DECLARE @IndexName TABLE (
@tcartwright
tcartwright / LogTableCleanup.sql
Created July 10, 2025 13:18
SQL SERVER AGENT: : Log table cleanup script
SET NOCOUNT ON
DECLARE @current_id INT = 1,
@db_name sysname,
@table_name sysname,
@target_months INT,
@datetime_column sysname,
@sql NVARCHAR(MAX),
@sep VARCHAR(100) = REPLICATE('*', 80)
@tcartwright
tcartwright / Download_and_install_Solarwinds_SentryOne_Plan_Explorer.ps1
Created June 27, 2025 16:21
POWERSHELL: Download and install sentry one plan explorer
<# download and install Solarwinds SentryOne Plan Explorer #>
$filenamePlanExplorerInstall = "$env:USERPROFILE\downloads\" + ([string](Get-Date -format "yyyy-MM-dd")) + "_SolarWinds-PlanExplorer.exe"
Start-BitsTransfer -Source 'https://downloads.solarwinds.com/solarwinds/Release/FreeTool/SolarWinds-PlanExplorer.exe' -Destination $filenamePlanExplorerInstall
& $filenamePlanExplorerInstall /install /passive /norestart