Skip to main content

Common Commands of SQL Server for a SQL DBA

Common Commands of SQL Server for a SQL DBA

sp_helpdbThis gives you information about all databases in the instance or specific information about one database.
sp_helpdb
sp_helpdb databasename

fn_virtualfilestats
This command will show you the number of read and writes to a data file. Use sp_helpdb with the database name to see the logical file numbers for the data files and the database id.
SELECT * FROM :: fn_virtualfilestats(dabaseid, logicalfileid)
SELECT * FROM :: fn_virtualfilestats(1, 1)
fn_get_sql()
Returns the text of the SQL statement for the specified SQL handle. This is similar to using DBCC INPUTBUFFER, but this command will show you additional information. This can also be embedded in a process easier then using the DBCC command
DECLARE @Handle binary(20) SELECT @Handle = sql_handle FROM sysprocesses WHERE spid = 52 SELECT * FROM ::fn_get_sql(@Handle)

sp_lock
This command shows you all of the locks that the system is currently tracking This is similar to information you can see in Enterprise Manager.
sp_lock
sp_lock spid
sp_lock spid1, spid2

sp_help
This command gives you information about the objects within a database. The command without an objectname will give you a list of all objects within the database.
sp_help
sp_help objectname

sp_who2
Gives you process information similar to what you see when using Enterprise Manager.
sp_who2
sp_who2 spid

sp_helpindex
Gives you information about the indexes on a table as well as the columns used for the index.
sp_helpindex objectname

sp_spaceused
This command shows you how much space has been allocated for the database (or if specified an object) and how much space is being used.
sp_spaceused
sp_spaceused objectname
DBCC CACHESTATS
Displays information about the objects currently in the buffer cache.
DBCC CACHESTATS



DBCC CHECKDB
This will check the allocation of all pages in the database as well as check for any integrity issues.
DBCC CHECKDB

DBCC CHECKTABLE
This will check the allocation of all pages for a specific table or index as well as check for any integrity issues.
DBCC CHECKTABLE (‘tableName’)

DBCC DBREINDEX
This command will reindex your table. If the indexname is left out then all indexes are rebuilt. If the fillfactor is set to 0 then this will use the original fillfactor when the table was created.
DBCC DBREINDEX (tablename, indexname, fillfactor)
DBCC DBREINDEX (authors, '', 70)
DBCC DBREINDEX ('pubs.dbo.authors', UPKCL_auidind, 80)

DBCC PROCCACHE
This command will show you information about the procedure cache and how much is being used. Spotlight will also show you this same information.
DBCC PROCCACHE
DBCC MEMORYSTATUS
Displays how the SQL Server buffer cache is divided up, including buffer activity.
DBCC MEMORYSTATUS

DBCC SHOWCONTIG
This command gives you information about how much space is used for a table and indexes. Information provided includes number of pages used as well as how fragmented the data is in the database.
DBCC SHOWCONTIG
DBCC SHOWCONTIG WITH ALL_INDEXES
DBCC SHOWCONTIG tablename

DBCC SHOW_STATISTICS
This will show how statistics are laid out for an index. You can see how distributed the data is and whether the index is really a good candidate or not.DBCC SHOW_STATISTICS (tablename, indexname)

DBCC SHRINKFILE
This will allow you to shrink one of the database files. This is equivalent to doing a database shrink, but you can specify what file and the size to shrink it to. Use the sp_helpdb command along with the database name to see the actual file names used.
DBCC SHRINKFILE (filename, size in MB)
DBCC SHRINKFILE (DataFile, 1000)

DBCC SQLPERF
This command will show you much of the transaction logs are being used.

DBCC SQLPERF(LOGSPACE)


DBCC TRACEON
This command will turn on a trace flag to capture events in the error log. Trace Flag 1204 captures Deadlock information.
DBCC TRACEON(traceflag)

DBCC TRACEOFF
This command turns off a trace flag.
DBCC TRACEOFF(traceflag)

Comments

Popular posts from this blog

Self Introduction and Responsibilities of a SQL Server DBA.

Hi, Self Introduction:                      I am Vang chew bigger , currently I am working with   Citronics , Here my role is SQL Server Database Administrator , I am having 3+ yrs Exp in SQL SERVER DBA with T-SQL, I have experience on SQL Server 2000, 2005 and 2008, I have a great experience on installation and configuration of SQL SERVER versions, and also applying the patches, hot fix, services packs and RTM’s accordingly, involved in upgrading on in place and side by side as per client requirement, and also Migrating the database on SQL SERVER 2000 to 2005 and 2008, Solid experience on configuring and maintenance of High availability SQL Server solutions, including Log shipping, Database Mirroring, Replication(Transactional and Merge) and SQL Server Clustering., having experience on performance tuning on server level, database level and query level, Responsible for working with application developers in identifying, resolving and proactively working to prevent performance or oth

SQL Server 2016 Windows Server 2016 Firewall Rule Step-By-Step.

SQL Server 2016 Windows Server 2016 Firewall Rule Step-By-Step. Opening Firewall in SQL Server 2016. Once the SQL Server is installed on the machine. We must enable the incoming traffic i.e., from the application to the SQL Server Database. For this purpose, we must create rule for SQL Server ports so that the application can connect to database & perform the CRUD operations on the tables. Now Let’s start Step-by-Step to enable it. 1.   Start System and Login (As Administrator); 2.   Open Control Panel. Click on Windows Firewall; 3. Now click on Advanced Settings; 4. Windows Firewall console open. If you click on Properties (right side) – you can disable firewall for all networks. We going to Inbound Rules (left side) for our rule creation; After Clicking on the Inbound rules. We will see the below screen.  Click on New

Shrinking a SQL Server Log File with Database in Always ON Availability.

Shrinking a SQL Server Log File with Database in Always ON Availability.                    Once we had a situation in our production server that the database which is in Always ON Availability group has been grown to the maximum size.  Many of them say that we can't shrink the log file as the database is in Always ON and we should take the downtime to perform the operation. But we shrink-ed the database log file to 5 GB from 180 GB. Steps are as below: --To Check the Number of pages allocated by the LogFile. DBCC LOGINFO --To check the file sixe sp_helpdb [SQLPantry] --To check the Log file Size DBCC SQLPERF(LOGSPACE) The above statement will show the logspace of all db at present situation. --To check the Log_reuse_wait description of a log file. select log_reuse_wait_desc,* from sys.databases The above statement will show the status of the log whether it has to be backuped i.e., LogBackup, Nothing, Active Transaction.  When ever there is status as Activ