Script to Check TempDB Speed

The following script adopted from Brent Ozar’s blog on his TempDB Performance and Configuration topic. I strongly encourage you to pay a visit to his site.

This query hits the dynamic management function (DMF) sys.dm_io_virtual_file_stats for all of the TempDB data files and lists out how fast they’re responding to write (and read) requests:

SELECT files.physical_name, files.name,
    stats.num_of_writes, (1.0 * stats.io_stall_write_ms / stats.num_of_writes) AS avg_write_stall_ms,
    stats.num_of_reads, (1.0 * stats.io_stall_read_ms / stats.num_of_reads) AS avg_read_stall_ms
FROM sys.dm_io_virtual_file_stats(2, NULL) as stats
INNER JOIN master.sys.master_files AS files
    ON stats.database_id = files.database_id
    AND stats.file_id = files.file_id
WHERE files.type_desc = 'ROWS'

Brent Ozar suggested to look for two things from the result generate from the script:

  • Are writes being evenly distributed between data files?
  • Are writes finishing in 20ms or less?

If either of those question is no, then we have some performance tuning work need to kick in.

How to: Check If xp_cmdshell is Enabled

“xp_cmdshell” allow us to run windows commands by using SQL Server but first we need to enable it. We can check whether the “xp_cmdshell” configuration is enabled from the system table sys.configurations which contains server-wide configuration option value in the system.

Execute the following T-SQL will allow you to check if “xp_cmdshell” is enabled on a server. You may need to manually enable the “xp_cmdshell” function if the return result is not “1”.

SELECT CONVERT(INT, ISNULL(value, value_in_use)) AS config_value
FROM  sys.configurations
WHERE  name = N'xp_cmdshell' ;

Enable “xp_cmdshell”

Execute the following T-SQL to enable the “xp_cmdshell” configuration.

--Enable Change in Advanced Options
EXEC SP_CONFIGURE N'show advanced options', 1
GO
RECONFIGURE
GO
--Enable XP_cmdshell
EXEC SP_CONFIGURE N'xp_cmdshell', 1
GO
RECONFIGURE
GO
--Disable Changes in Advanced Options
EXEC SP_CONFIGURE N'show advanced options', 0
GO
RECONFIGURE
GO

How to: Find Who Deleted the Database or Table

Found a very useful script from SQLServerCentral Forums that it will help you to easily identify who is the person that had deleted your database or table.

With cteObjectTypes AS
    (
    SELECT
        TSV.trace_event_id,
        TSV.subclass_name,
        TSV.subclass_value
    FROM
        sys.trace_subclass_values AS TSV JOIN
        sys.trace_columns AS TC ON
            TSV.trace_column_id = TC.trace_column_id 
    WHERE
        TC.[name] = 'ObjectType'
    ),
    cteEventSubClasses AS
    (
    SELECT
        TSV.trace_event_id,
        TSV.subclass_name,
        TSV.subclass_value
    FROM
        sys.trace_subclass_values AS TSV JOIN
        sys.trace_columns AS TC ON
            TSV.trace_column_id = TC.trace_column_id 
    WHERE
        TC.[name] = 'EventSubClass'
    )
SELECT
    TE.[name],
    I.ApplicationName,
    I.BigintData1,
    I.ClientProcessID,
    I.ColumnPermissions,
    I.DatabaseID,
    I.DatabaseName,
    I.DBUserName,
    I.Duration,
    I.EndTime,
    I.Error,
    I.EventSequence,
    Convert(nvarchar(10), I.EventSubClass) + N'-' + ESC.subclass_name as EventSubClass,
    I.FileName,
    I.HostName,
    I.IndexID,
    I.IntegerData,
    I.IsSystem,
    I.LineNumber,
    I.LoginName,
    I.LoginSid,
    I.NestLevel,
    I.NTDomainName,
    I.NTUserName,
    I.ObjectID,
    I.ObjectID2,
    I.ObjectName,
    Convert(nvarchar(10), I.ObjectType) + N'-' + OT.subclass_name as ObjectType,
    I.OwnerName,
    I.ParentName,
    I.Permissions,
    I.RequestID,
    I.RoleName,
    I.ServerName,
    I.SessionLoginName,
    I.Severity,
    I.SPID,
    I.StartTime,
    I.State,
    I.Success,
    I.TargetLoginName,
    I.TargetLoginSid,
    I.TargetUserName,
    I.TextData,
    I.TransactionID,
    I.Type,
    I.XactSequence
FROM
    sys.traces T CROSS Apply 
    sys.fn_trace_gettable((SELECT CONVERT(varchar(100), value)
             FROM sys.fn_trace_getinfo(default)
             WHERE property=2), default) I JOIN
    sys.trace_events AS TE ON 
        I.EventClass = TE.trace_event_id LEFT JOIN
    cteEventSubClasses AS ESC ON
        TE.trace_event_id = ESC.trace_event_id And
        I.EventSubClass = ESC.subclass_value LEFT JOIN
    cteObjectTypes AS OT ON 
        TE.trace_event_id = OT.trace_event_id AND
        I.ObjectType = OT.subclass_value
WHERE
    T.is_default = 1 AND
    TE.NAME = 'Object:Deleted'

Source from SQLServerCentral – Identify who deleted your database or table

How to: Kill User Session

Let’s say you want to kill all running process by a particular user or account. The following query will help you to find all related SPID for all processes initiated by a particular user or account.

You may need to replace “USER_ACCOUNT” in the query with your target account name.

SELECT spid from master..sysprocesses
WHERE loginame = N'USER_ACCOUNT'

After you obtain the list of SPID, it’s time for you to reload your weapon, you can simply execute KILL command against those processes.

How to: Identify Database Creation Date

Sometimes the database creation date will help you to examine whether any database within your SQL Server environment has been dropped and recreated or identify when the new database created. The following T-SQL statement will provide you a simple piece of information on that.

SELECT 
	name AS [DB Name], 
	dbid AS [DB ID]
	crdate AS [Created Date]
FROM 
	master..sysdatabases

Meanwhile, you might probably want to check out how to examine your table creation date and last modified date in order to help you dive deeper into your SQL Server environment.

How to: Get I/O Utilization by Database

This cool scripts obtain from Glenn Berry’s SQL Server Performance Scripts library to help you to find out the I/O utilization for each databases in your SQL Server instance. If you are looking for a script to shows you which database are the busiest one, like I do, in our database instance, this will the right script you need to grab and put in your toolbox.

-- Get I/O utilization by database (IO Usage By Database)
-- Helps determine which database is using the most I/O resources on the instance
-- Scripts adopted from SQL Server 2008 R2 Diagnostic Information Queries (February 2013).sql

SELECT 
	DB_NAME(database_id) AS [Database Name],
	CAST(SUM(num_of_bytes_read + num_of_bytes_written)/1048576 AS DECIMAL(12, 2)) AS io_in_mb
FROM 
	sys.dm_io_virtual_file_stats(NULL, NULL) AS [DM_IO_STATS]
GROUP BY database_id;

How to: Sort SQL Results in Your Own Preferred Order

Classic sorting method allows you to sort table base on particular column either ascending or descending order. The following method allow you to customize the sorting result in your own preferred order, for instance sort a list of SQL Server in your environment based on server environment.

SELECT * FROM [SQL_ServerList]
ORDER BY
	(CASE ServerType
	WHEN 'PROD'	THEN 1
	WHEN 'QA'	THEN 2
	WHEN 'DEV'	THEN 3 END), 
	[Instance Name], [SQLServer Name]