As a database administrator (DBA), navigating the vast landscape of database management can be challenging and rewarding. It’s important to be able to execute the commands necessary to simplify day-to-day tasks and ensure that your SQL Server performs smoothly. In this guide, we’ll go through some everyday commands that every DBA should have in their toolkit.
1. Locate Database Files:
Use the SQL command below to locate database files starting with the letter ‘c’:
SELECT * FROM sysaltfiles WHERE filename LIKE 'c%'
SQL2. Shrink a Full Log File:
Execute the following commands to shrink a full log file:
BACKUP LOG dbname WITH NO_LOG
DBCC SHRINKFILE(2) --(2), is specifically targeting a file with a file ID of 2.
SQL3. Check Open Transactions:
Use the following command to check for open transactions:
DBCC OPENTRAN
SQL4. Detect Blocked Process:
Identify blocked processes with the query below:
SELECT * FROM sysprocesses WHERE blocked > 0 AND spid = 57
SQL5. Buffer Cache Hit Ratio < 90:
Analyze potential performance issues using:
DBCC SHOWFILESTATS
SQL6. View Database Status:
Check the status of your database with:
DBCC SHOWFILESTATS
SQL7. Check CPU Usage (when buffer cache hit ratio < 90):
Monitor CPU usage with:
SELECT * FROM sys.sysprocesses ORDER BY cpu ASC
SQL8. Get Table Information:
Retrieve table information using:
SELECT * FROM sysobjects WHERE name LIKE '%table_name%'
SQL9. Retrieve Data from Linked Server:
Access data from a linked server with:
SELECT * FROM LinkedServer.DBName.SchemaName.TableName
SQL10. Find Service Pack Version:
-- Determine the service pack version with:
SELECT SERVERPROPERTY('PRODUCTLEVEL')
SQL11. View Server Collation Setting:
--Check the server collation setting using:
SP_HELPSORT
SQL12. Check Log Usage:
--Monitor log usage with:
SELECT * FROM sys.databases WHERE name LIKE '%log_reuse_wait_desc%'
SQL13. See Connection Information with DMVs:
--Retrieve connection information using:
SELECT * FROM sys.dm_exec_sessions
SQL14. View Transaction Isolation Level:
--Check transaction isolation level with:
DBCC USEROPTIONS
SQL15. Change Database Compatibility Level (SQL 2016 to 2019):
--Execute the command to change the compatibility level:
EXEC sp_dbcmptlevel AdventureWorks, 150
SQL16. Check Query Memory Usage:
--Monitor query memory usage with:
SELECT * FROM sys.dm_exec_query_memory_grants
SQL17. Clear Buffer Cache Data:
--Clear buffer cache data using:
DBCC DROPCLEANBUFFERS
SQL18. Monitor Tempdb Space Allocation:
--Keep an eye on tempdb space allocation with:
SELECT * FROM sys.dm_db_session_space_usage
SQL19. Get Index Usage Stats:
--Retrieve index usage statistics with:
SELECT * FROM sys.dm_db_index_usage_stats ORDER BY object_id, index_id
SQL20. List Schedulers and Runnable Tasks:
--View schedulers and tasks with:
SELECT * FROM sys.dm_os_schedulers WHERE scheduler_id < 255
SQL21. Identify High CPU Queries:
--Identify high CPU queries using:
SELECT * FROM sys.dm_exec_query_stats
SQL22. Check for Active Parallel Requests:
--Identify active parallel requests with:
SELECT * FROM sys.dm_exec_requests
SQL23. View Lock Information:
--Check lock information with:
SELECT * FROM sys.dm_tran_locks
SQLAdditional Handy Commands:
Open Cluster Administrator:
cluadmin.msc
SQLsp_readerrorlog:
View the error log with:
sp_readerrorlog
SQLsp_helpdb:
View information about databases with:
sp_helpdb
SQLsp_helplogins ‘Loginname’:
Verify login permissions with:
sp_helplogins 'Loginname'
SQLsp_helpuser:
Verify users in the database with:
sp_helpuser
SQLxp_logininfo ‘Loginname’:
Verify Active Directory login group information with:
xp_logininfo 'Loginname'
SQLsp_who2:
View running processes on SQL Server with:
sp_who2
SQLsp_who2 active:
View details of active processes on SQL Server with:
sp_who2 active
SQLselect * from sys.sysprocesses where blocked != 0:
View currently blocked processes with:
select * from sys.sysprocesses where blocked != 0
SQLDBCC INPUTBUFFER(spid):
View the current query being executed by a specific process with:
DBCC INPUTBUFFER(spid)
SQLDBCC SQLPERF(logspace):
View information about log space usage with:
DBCC SQLPERF(logspace)
SQLThese commands, when used judiciously, can empower DBAs to efficiently manage and optimize SQL Server performance on a daily basis. Remember, each command serves a specific purpose, contributing to the overall health and robustness of your database system.