ms_sql_server:performance_tuning

MS SQL Server Performance Tuning

Run these two line while anyone else log on to your database. Check the name YOUR_DATABASE_NAME as the name of your database.

ALTER DATABASE YOUR_DATABASE_NAME SET ALLOW_SNAPSHOT_ISOLATION ON  
ALTER DATABASE YOUR_DATABASE_NAME SET READ_COMMITTED_SNAPSHOT ON WITH NO_WAIT

Testing if the above code run correctly: You should see 1 instead of 0 for the result for the code below:

SELECT is_read_committed_snapshot_on FROM sys.databases WHERE name= 'YOUR_DATABASE_NAME'

Testing the Performance Gains: Before running the above code, the transaction below will block the table YOUR_TABLE access for one minute, and even listing the whole table will be blocked (SELECT * FROM YOUR_TABLE). After running the code, the blocking no longer exist as the read command read form a snapshot other than the actual uncommitted table.

BEGIN TRANSACTION [Tran1]
	BEGIN TRY
		insert into YOUR_TABLE  values ('SOMEVALUE')
		WAITFOR DELAY '00:01:00'
		COMMIT TRANSACTION [Tran1]
	END TRY
	BEGIN CATCH
		ROLLBACK TRANSACTION [Tran1]
	END CATCH  

Here is the code found on stackoverflow - Get size of all tables in database. Thanks marc_s!

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    t.Name

Result:

  • ms_sql_server/performance_tuning.txt
  • Last modified: 2018/10/16 09:29
  • by chongtin