A number of tools, utilities, and information sources are built in to SQL Server to help you optimize SQL Server operations. These mechanisms can be used to diagnose, maintain, and optimize all operations. To obtain immediate information about the server, you can observe the current activity using the Enterprise Manager or stored procedures. To observe information from a historical perspective, you can view the SQL Server Logs from the Enterprise Manager. Current Server ActivityThe SQL Server Enterprise Manager provides a facility where an administrator can go to find out pertinent information about what the server is currently doing. Use the Current Activity window to perform monitoring of SQL Server. This enables you to determine, at a glance, the volume and general types of activity on the system related to current blocked and blocking transactions in the system, connected users, the last statement executed, and locks that are currently in effect. The Current Activity view provides a display of process information, locks broken down by process identification, and locks broken down by object. Process InformationThe process information provides information on all activity currently executing against the system. It also lists current connections that may not be active but are still using resources. Here are descriptions of the process information columns :
NOTE CPU Returning Zero The CPU entry is updated only for processes performed on behalf of T-SQL statements executed when SET STATISTICS TIME ON has been activated in the same session. When zero is returned, SET STATISTICS TIME is OFF . CPU time for each process is accumulated regardless of the statistics setting. A significant amount of information is available about each of the processes utilizing the SQL Server database system. Each process also controls SQL Server locking behavior. Lock InformationLocking information provides information on all locks currently being used by SQL Server to protect the object and data integrity of all SQL Server objects. Here are descriptions of the lock information columns:
There are several available lock types and lockable SQL Server resource:
Several lock modes control how SQL Server sets and recognizes locks, blocking, and user access. Lock modes represent levels of locking from the lowest levelsharedto the highest levelexclusive.
Locks are set at different modes as needed to allow for data updates, index creation, and table redefinition. An example of locking used during an update process is shown in Step by Step 12.3.
Only compatible lock types can be placed on a resource that is already locked. Table 12.1 provides a compatibility matrix for resource lock modes that shows which locks are compatible with other locks obtained on the same resource. Table 12.1. Lock Computability Matrix
The compatibility matrix indicates whether a specific type of lock can be granted with another already in place. Some of the same information seen in the Current Activity window can also be retrieved programmatically using stored procedures. Stored procedures play an important part in developing system code to aid in server management. Stored ProceduresStored procedures can be used to perform data gathering in much the same manner that the Enterprise Manager uses to gather its information to display to the user. By querying system objects, functions, and counters, the stored procedures produce output that can be used directly, or they can perform other conditional activity through the systematic use of the results. Much system level coding can be developed by leveraging the procedures that already exist. Hundreds of available stored procedures are installed with SQL Server, as well as a few dozen extended stored procedures. Not all these procedures are used in data gathering, troubleshooting, and optimization, but many provide information that assists in these activities. Stored procedures are stored in the Master and user databases, whereas extended stored procedures are found in only the Master database. Additional stored procedures can be created, and additional extended stored procedures can be installed. A selection of the more commonly used procedures is covered in this section. For more information on these and other procedures, refer to stored procedures coverage in SQL Server Books Online. sp_whoThis procedure reports current information about users and processes, including the executing statement and whether the statement is blocked. This is a T-SQL alternative to viewing user activity in the Enterprise Manager. The resultset provided by this procedure provides user and process information, including the process identification and status information, as well as an ID for any blocking process. Also provided from the command is the database in use and the T-SQL command that is executing. sp_lockThis procedure reports information about locks and the resources affected by locking. This is a T-SQL alternative to viewing lock activity in the Enterprise Manager. It provides a resultset similar to the one displayed in the Enterprise Manager, listing all relevant locks, their status, mode, and resources being affected. sp_monitorThis procedure displays statistics, including CPU usage, I/O usage, and the amount of time idle since the stored procedure was last executed. A variety of information is produced in the resultset for this command. The time of execution of sp_monitor , the number of seconds that SQL Server has been working, the number of seconds that SQL Server has been performing input and output operations, and the number of seconds that SQL Server has been idle provide information that can be useful in determining whether processing resources are adequate. The number of input packets read by SQL Server, output packets written by SQL Server, and errors encountered by SQL Server while reading and writing packets can help you determine SQL Server's network utilization. The number of reads by SQL Server, number of writes, and the number of errors encountered by SQL Server while reading and writing can help you determine file storage interactions. Knowing the number of logins or attempted logins to SQL Server can be useful in security information gathering or as an aid in determining SQL Server utilization. sp_spaceusedThis procedure displays an estimate of the current amount of disk space used by a table or database. This is a T-SQL alternative to viewing database usage in the Enterprise Manager. With sp_spaceused you can easily see the number of rows, disk space reserved, and disk space used by a table in the current database, or display the disk space reserved and used by the entire database. xp_sqlmaintThis is an extended stored procedure that calls the SQL maintenance utility. Passed during the call to this utility is a string containing switches that indicate the tasks to be performed and other related details. The sqlmaint utility performs a set of maintenance operations on one or more databases, in a similar fashion as each operation's corresponding T-SQL functions. The sqlmaint utility performs maintenance operations on one or more databases. Use sqlmaint to run DBCC checks, back up a database and its transaction log, update statistics, and rebuild indexes. The utility performs database maintenance activities and generates a report. The report can be sent to a designated text file, HTML file, or email account. sp_configureThis stored procedure is used to display and/or alter global configuration settings for the server. When executed with no parameters, the stored procedure returns a resultset showing the current configuration options. After you execute the procedure to change a configuration option, you should use the RECONFIGURE WITH OVERRIDE statement for the change to take immediate effect. sp_dboptionThis procedure is used to display and/or alter database options. It is supported for backward compatibility, and you should therefore use ALTER DATABASE to set database options. sp_helpThis procedure reports information about an object listed in the sysobjects table. In general, you can tack any object name to the end of help to receive help and information about a particular type of object, such as sp_helpalert , sp_helpfilegroup , sp_helpserver , and sp_helpuser . Over 100 stored procedures have the sp_help prefix. SQL Profiler Stored Procedures and FunctionsUse T-SQL stored procedures to gather SQL Profiler statistics. The SQL Profiler uses stored procedures to create traces and send the trace output to the appropriate location. These procedures can be used from within an application to create traces manually. This enables you to write custom applications specific to the needs of your business. In SQL Server version 7.0, these procedures were all prefixed with xp_trace . The features of many of these procedures have been combined, and in the 2000 release there are fewer, more streamlined procedures, as summarized in the following list:
Using these stored procedures, you can configure and execute traces and forward trace events from one or more servers to a file. REVIEW BREAK: Finding the FactsNow you have examined everything in a SQL Server installation, using all but the two most diverse and granular tools, Query Analyzer and Profiler. Gathering information from the operating system tools and the server activity procedures and displays plays an important part in the initial fact-finding that must be performed to alter any of the properties of the server. The processes discussed thus far are often used in an implementation as a starting point toward:
As you begin any of these processes, begin with the hardware and operating system and then proceed into the application server. As you get further into data gathering and analysis, you should look into each database and the interactions between the data and user applications. To view SQL Server tasks in detail, after the initial data gathering processes, use the SQL Server Profiler to develop a more complete picture. When more granularity is desired and you want to look into the user applications, the functionality provided by the Query Analyzer can be used to obtain the most detailed and granular data. |