Skip to content

Instantly share code, notes, and snippets.

View ronascentes's full-sized avatar

Rodrigo Nascentes ronascentes

View GitHub Profile
@ronascentes
ronascentes / try_catch_sample.sql
Created October 1, 2016 17:40
Using TRY...CATCH in T-SQL
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- A FOREIGN KEY constraint exists on this table. This
-- statement will generate a constraint violation error.
DELETE FROM Production.Product
WHERE ProductID = 980;
-- If the DELETE statement succeeds, commit the transaction.
@ronascentes
ronascentes / process_large_amount_records.sql
Last active February 17, 2022 19:48
Delete/ Update a large amount of records
-- got from https://sqlperformance.com/2013/03/io-subsystem/chunk-deletes
DECLARE @rows INT, @ErrorMessage NVARCHAR(4000);
SET NOCOUNT, XACT_ABORT ON;
SET @rows = 1;
BEGIN TRY
WHILE (@rows > 0)
BEGIN
BEGIN TRANSACTION;
@ronascentes
ronascentes / truncate_date.sql
Created October 1, 2016 21:07
How to truncate date in T-SQL
CONVERT(VARCHAR(10), LAST_LOAD_DATE, 101) < CONVERT(VARCHAR(10), GETDATE(), 101)
@ronascentes
ronascentes / create_mock_data.sql
Created October 1, 2016 21:17
Create mock data
CREATE TABLE dbo.mock_table
(
MyKeyField VARCHAR(10) NOT NULL,
MyDate1 DATETIME NOT NULL,
MyDate2 DATETIME NOT NULL,
MyDate3 DATETIME NOT NULL,
MyDate4 DATETIME NOT NULL,
MyDate5 DATETIME NOT NULL
)
@ronascentes
ronascentes / enable_trace_flags.sql
Last active October 4, 2016 17:42
Useful trace flags for SQL Server performance improvement
-- Trace flag 1118 turns off mixed page allocations. Preventing mixed page allocations reduces the risk of page latch contention
-- on the SGAM allocation bitmatps that track mixed extents; which Paul says is one of the leading causes for contention in tempdb.
-- When doing allocations for user tables always allocate full extents. Reducing contention of mixed extent allocations
DBCC TRACEON (1118,-1)
-- simply stops SQL Server from writing backup successful messages to the error log.
DBCC TRACEON (3226,-1)
-- Changes to automatic update statistics
DBCC TRACEON (2371,-1)
@ronascentes
ronascentes / check_advanced_settings.sql
Last active October 28, 2016 13:03
Check SQL Server advanced settings
-- show advanced options
-- cost threshold for parallelism
-- max degree of parallelism
-- max server memory (MB)
-- optimize for ad hoc workloads
SELECT name, description, value, value_in_use
FROM sys.configurations
WHERE configuration_id IN (518,1538,1539,1544,1581)
@ronascentes
ronascentes / find_missing_indexes.sql
Last active October 4, 2016 17:43
Finding missing indexes
-- Find Missing Indexes by Index Advantage
-- Look at index advantage, last user seek time, number of user seeks to help determine source and importance
-- SQL Server is overly eager to add included columns, so beware
-- Do not just blindly add indexes that show up from this query!!!
SET NOCOUNT ON
GO
SELECT TOP 25
DB_NAME(dm_mid.database_id) AS Database_Name,
user_seeks * avg_total_user_cost * (avg_user_impact * 0.01) AS [index_advantage],
dm_migs.last_user_seek AS Last_User_Seek,
@ronascentes
ronascentes / clear_memory_cache.sql
Last active October 5, 2016 17:32
Clearing cache and buffer
-- releases all unused cache entries from all caches
DBCC FREESYSTEMCACHE('All') WITH NO_INFOMSGS;
-- flushes the distributed query connection cache used by distributed queries against an instance
DBCC FREESESSIONCACHE WITH NO_INFOMSGS;
-- manually removes a single plan or all plans from the cache
DBCC FREEPROCCACHE WITH NO_INFOMSGS;
-- clear the data buffer
@ronascentes
ronascentes / check_database_space.sql
Last active May 3, 2017 19:16
Like sp_spaceused but much better
USE <db_name>
GO
SELECT SUM((size/128.0))/1024 AS [Total Size in GB],
SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0)/1024 AS [Space Used in GB],
SUM(size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0)/1024 AS [Available Space in GB],
sum((((size)/128.0) - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0)) / sum(((size)/128.0)) * 100 as '% Available'
FROM sys.database_files WITH (NOLOCK)
--WHERE type = 1 -- log
@ronascentes
ronascentes / check_errorlog.sql
Last active November 29, 2016 17:34
Look for usually errors by using xp_readerrrorlog
/*
xp_readerrrorlog - extended stored procedure accepts at least 7 parameters
Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc...
Log file type: 1 or NULL = error log, 2 = SQL Agent log
Search string 1: String one you want to search for
Search string 2: String two you want to search for to further refine the results
Search from start time
Search to end time
Sort order for results: N'asc' = ascending, N'desc' = descending
EXEC xp_readerrorlog 0, 1, NULL, NULL, NULL, NULL, N'desc'