This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* EXEC sp_helptext 'dbo.sp_ArchiveTableData' */ | |
| USE [master] | |
| GO | |
| /* | |
| Author: Tim Cartwright | |
| Purpose: | |
| Archives large tables using a "nibbling delete" pattern - moving data in small batches |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Check for lock escalation issues | |
| SELECT | |
| 'lock escalation' AS [result_name], | |
| OBJECT_NAME(p.object_id, db_id()) AS TableName, | |
| p.index_id, | |
| i.name AS IndexName, | |
| SUM(CASE WHEN l.resource_type = 'EXTENT' THEN 1 ELSE 0 END) AS ExtentLocks, | |
| SUM(CASE WHEN l.resource_type = 'PAGE' THEN 1 ELSE 0 END) AS PageLocks, | |
| SUM(CASE WHEN l.resource_type = 'KEY' THEN 1 ELSE 0 END) AS KeyLocks, | |
| SUM(CASE WHEN l.resource_type = 'OBJECT' THEN 1 ELSE 0 END) AS TableLocks, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- sourced from claude.ai | |
| -- Create Extended Event session to capture lock escalations | |
| CREATE EVENT SESSION [CaptureEscalations] ON SERVER | |
| ADD EVENT sqlserver.lock_escalation( | |
| ACTION( | |
| sqlserver.client_app_name, | |
| sqlserver.client_hostname, | |
| sqlserver.database_name, | |
| sqlserver.query_hash, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| -- 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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
NewerOlder