3 4
Monitoring SQL Server 2000 consists of performing various tasks to monitor levels of resource usage and specific SQL Server events. In this lesson, you will learn to use Windows 2000 System Monitor and Task Manager to monitor memory, I/O, and processor use. You will also monitor the execution of stored procedures and SQL batches using SQL Profiler. Finally, you will monitor locks using the SQL Server Enterprise Manager Current Activity window and system stored procedures.
You monitor resource use (memory, I/O, and processor) to determine whether adequate resources exist, and also to determine the relative use of resources by different server processes. It is useful to test resource usage on a computer with minimal load to establish the performance baseline before you place a load on the system.
To monitor resource use with Task Manager, right-click the Windows taskbar and then click Task Manager. You can also press the Ctrl+Alt+Del key combination and then click the Task Manager button in the Windows Security menu. To view resource use on a per-process basis, click the Processes tab in Task Manager. See Figure 14.1.
Figure 14.1
Task Manager (Processes tab).
In the Processes tab, notice the five default columns of information that are displayed. You can sort the information displayed by clicking a column heading. Notice that in Figure 14.1, the columns are ordered based on the amount of memory used for each process. Click Select Columns on the View menu to select additional columns to appear on the Processes page. Figure 14.2 shows the Select Columns dialog box.
Figure 14.2
Selecting additional columns within Task Manager.
You can control the frequency with which the information is refreshed (or pause the display) by clicking Update Speed on the View menu in Task Manager. In the Options menu, you can control whether Task Manager always appears on top of other applications on the desktop (this is the default behavior) and also whether Task Manager appears on the Windows taskbar when minimized (default).
Click the Performance tab to view overall resource use on the computer. See Figure 14.3.
Figure 14.3
Task Manager (Performance tab).
Notice, in the Totals group box, that you can view processor use, based on handles, threads, and processes. You can also view memory use, based on committed memory (including virtual memory) in the Commit Charge (K) group box, physical memory in the Physical Memory (K) group box, and kernel memory in the Kernel Memory (K) group box. In Figure 14.3, notice that the peak committed memory is higher than the total physical memory on the computer. This indicates that this computer could benefit from additional memory.
Note
To monitor resource use with System Monitor, click Performance in the Administrative Tools program group. System Monitor is a snap-in within the Performance console. See Figure 14.4.
Figure 14.4
Performance MMC with the System Monitor snap-in.
System Monitor displays the counter values for selected performance objects, counters, and instances. These can be current values, a snapshot of values at a given point in time, or values from a saved Counter Log. To add counters to System Monitor, click the Add button on the toolbar to display the Add Counters dialog box. See Figure 14.5. Notice that you can use counters from the local computer, or you can select a remote computer to monitor.
Figure 14.5
Adding counters to System Monitor.
You can view counter values as a chart, histogram, or report by clicking the corresponding button on the toolbar. See Figures 14.6, 14.7, and 14.8.
Figure 14.6
Chart view of System Monitor.
Figure 14.7
Histogram view of System Monitor.
Figure 14.8
Report view of System Monitor.
Select a counter and then click the Highlight button on the toolbar (or press Ctrl+H on your keyboard) to highlight the counter in white in the chart or histogram view. You can only highlight one counter at any given time. Click the Freeze Display button on the toolbar to freeze the counter values at a particular point for further analysis. Click the Properties button on the System Monitor toolbar (not the Performance MMC toolbar) to alter the properties of System Monitor. Properties include update frequency (default is 1 second) and display characteristics.
To create a log for later review, expand Performance Logs And Alerts, right-click Counter Logs, and then click New Log Settings. You will be prompted to enter the name of the new log file in the New Log Settings dialog box, and then the NewLogFile dialog box appears, as shown in Figure 14.9.
You can create a log file definition containing the counters to be logged, the sampling interval, the file size (including enabling file rollover), and a logging schedule.
You should monitor your computer to determine whether there is adequate memory on the computer for the processes that are running. Indications of insufficient memory are a lack of available memory and hard disk paging. Multiple counters must be interpreted together. You might also need to determine which processes are using memory and to isolate the memory used by SQL Server. This information is useful for setting minimum and maximum memory values for SQL Server when sharing the computer with other server applications.
Figure 14.9
Creating a memory counter log in System Monitor.
Table 14.7 describes the most important memory counters to monitor and provides help in interpreting values received.
Note
Overloaded disk subsystems are one of the most common performance problems with SQL Server installations. I/O-bound disks cause disk queuing and a general degradation of overall system performance. This problem is solved, in most cases, by adding additional disks to RAID or by implementing RAID. However, insufficient memory and inefficient queries can also cause excessive disk I/O. Table 14.8 describes the most important I/O object counters to monitor and provides help on interpreting values received.
Inadequate processor resources are also a common performance problem. Additional processors or a faster processor can solve the problem. However, inadequate memory (causing excessive paging and processor use) or inefficient statements (generating excessive processor use) can also be the cause of the performance bottleneck. Table 14.9 describes the most important processor object counters to monitor and provides help on interpreting values received.
Table 14.7 Memory Object Counters
Object Counter | This Object Counter Reports . . . | How to Interpret. . . |
Memory \ Available Mbytes | Megabytes of memory currently available for use. | A low value can indicate a shortage of memory or an application failing to release memory. |
Memory \ Pages/sec | Number of pages retrieved from disk or written to free space in the working set on the disk because of hard page faults. | A high value (above 20) can indicate excessive hard disk paging because of a memory shortage. |
PhysicalDisk \ Avg. Disk Queue Length | Average number of both read and write requests that were queued. | If an increase in disk queue length is not accompanied by a decrease in memory paging, a memory shortage exists. |
Memory \ Page Faults/sec | The overall rate that faulted pages are handled by the processor. | A high value for overall page faults/sec coinciding with a low value for the SQL Server instance indicates that SQL Server is not causing the memory shortage. |
Process \ Page Faults/sec for the SQL Server instance | The rate faulted pages occur because of the SQL Server process for a given SQL Server instance. | A high value for overall page faults/sec coinciding with a low value for the SQL Server instance indicates that SQL Server is not causing the memory shortage. |
SQL Server: Memory Manager \ Total Server Memory (KB) | Total amount of dynamic memory SQL Server is currently using for its memory buffer. | If this value is consistently close to the total amount of physical memory of the computer (or the configured maximum memory value), more memory might be needed. |
Process \ Working Set for the SQL Server instance | Total number of current bytes in the Working Set for the SQL Server instance. | Use this value to determine the maximum memory needed for SQL Server with the current load. |
SQL Server: Buffer Manager \ Buffer Cache Hit Ratio | Percentage of pages found in the memory buffer pool without having to incur a read from disk. | This value should generally be above 90%. If not, more memory is generally needed. |
SQL Server: Buffer Manager \ Total Pages | Total number of pages in the buffer pool (including data, free, and stolen pages). | A low value can indicate a shortage of memory available for the buffer pool. |
Table 14.8 I/O Object Counters
Object Counter | This Object Counter Reports . . . | How to Interpret. . . |
PhysicalDisk \ % Disk Time | Percentage of elapsed time during which the hard disk is busy servicing read or write requests. | A low rate of disk paging coinciding with a high rate of disk usage and disk queuing indicates a disk bottleneck. Can be over 100% if using a RAID device or an intelligent disk controller. |
PhysicalDisk \ Avg. Disk Queue Length | Average number of both read and write requests that were queued. | A low rate of disk paging coinciding with a high rate of disk usage and disk queuing indicates a disk bottleneck. A disk queue above 1.5 to 2 times the number of disk spindles indicates a disk bottleneck. |
PhysicalDisk\ Current Disk Queue Length | Instantaneous number of both read and write requests that are queued. | Use in conjunction with the PhysicalDisk\ Avg. Disk Queue Length to determine whether a disk bottleneck exists. |
PhysicalDisk \ Avg. Disk/sec Write | Average time to write data to disk (disk latency). | A disk latency value above 15 to 20 milliseconds indicates a disk bottleneck. |
PhysicalDisk \ Avg. Disk/sec Read | Average time to read data from disk. | A disk latency value above 15 to 20 milliseconds indicates a disk bottleneck. |
PhysicalDisk \ Disk Writes/sec | Rate of write operations. | If the rate of disk writes plus disk reads is not close to the capacity of the hard disk and disk latency is high, a faulty disk is likely. If the combined rate is close to the capacity, a disk bottleneck is likely. |
PhysicalDisk \ Disk Reads/sec | Rate of read operations. | If the rate of disk writes plus disk reads is not close to the capacity of the hard disk and disk latency is high, a faulty disk is likely. If the combined rate is close to the capacity, a disk bottleneck is likely. |
Table 14.9 Processor Object Counters
Object Counter | This Object Counter Reports . . . | How to Interpret. . . |
Processor \ % Processor Time | Percentage of time the processor spent executing non-idle threads. | Rates above 75% generally indicate a processor bottleneck. Systems with values above 60% can generally benefit from more processor power. |
System \ Processor Queue Length | Number of threads in the processor queue. | A sustained processor queue above two threads generally indicates a processor bottleneck. |
In this practice you use System Monitor and Task Manager to monitor system resource use.
To monitor system resources using System Monitor and Task Manager
Windows Task Manager appears.
Notice the overall values for process and memory use. In particular, note the delta between Total Physical Memory and both the Total Commit Charge and the Peak Commit Charge. These indicators will tell you if you have an overall memory shortage on your computer.
A preconfigured Performance console appears displaying a number of performance object counters in the Report view. Review the displayed counters.
You will use this frozen display to compare values with a SQL Server load and without a SQL Server load.
A second version of the same preconfigured Performance console appears.
The two Performance consoles are displayed side by side.
The OSQL command-prompt utility runs the LoadInLoop.sql command in the Command Prompt window.
Notice the effect of the load on the performance object counters. Compare the counters between the frozen instance of System Monitor and the unfrozen instance. Notice the load on the disk. Determine whether you have a memory shortage on your computer or the processor needs additional power.
Windows Task Manager appears.
Notice the overall values for process and memory use. In particular, notice how the delta between Total Physical Memory and both the Total Commit Charge and the Peak Commit Charge changed. Does your system have a memory shortage?
To monitor stored procedures and Transact-SQL batches, click Profiler in the Microsoft SQL Server program group. See Figure 14.10.
Figure 14.10
SQL Profiler.
You can create a new trace, create a new template, or open an existing trace file for analysis and replay. To create a new trace, click the New Trace button on the toolbar (or point to New and then click Trace in the File menu) and then, in the Connect To SQL Server dialog box, connect to the SQL Server instance on which you want to trace SQL Server events. The Trace Properties dialog box then appears, as shown in Figure 14.11.
Figure 14.11
SQL Profiler Trace properties (General tab).
In the General tab, you can create a new trace definition or use one of the preconfigured trace templates. In the Trace Name text box, use a descriptive naming convention for each new trace definition you create. You can choose to save the captured trace information to a file or table, and set a maximum size for the captured data. If you save the captured trace information to a file, notice that the default is to enable file rollover (this creates a new file when the maximum size is reached). You can also enable a stop time for the trace.
In the Events tab, you can choose SQL Server events (event classes) from various categories of events. Certain event classes will already be selected based on the initial template selected. See Figure 14.12.
In the Data Columns tab, you can choose to add (or subtract) to the default data columns to capture the information you want. You will want different types of information for different types of traces. By moving one or more data columns up to the Groups category, you can group the output by that data column. You move data columns up by clicking the Up button. You can also order the data columns in the output, by clicking the Up button or the Down button. See Figure 14.13.
In the Filters tab, you can choose to include or exclude various types of events. Notice that, by default, SQL Profiler events are not captured. See Figure 14.14. You might also want to exclude all events related to the domain user account of the SQL Server service.
Figure 14.12
SQL Profiler Trace Properties (Events tab).
Figure 14.13
SQL Profiler Trace Properties (Data Columns tab).
Figure 14.14
SQL Profiler Trace Properties (Filters tab).
You can also select the Exclude System IDs check box to exclude SQL events referencing system objects. This will generally capture the information you want to analyze and will significantly reduce the size of your trace because system objects that are being accessed can result in 50 to 75 percent of the trace events. Click the Run button to start the trace.
After a trace has been captured, you can search within the trace for specified strings (by clicking the Find String button on the toolbar). You can also choose to save the template for later use or save the data captured for later analysis and comparison by clicking Save As on the File menu.
You should create separate trace templates for different types of traces.
In this practice you use SQL Profiler to monitor Transact-SQL batches and stored procedures.
To monitor SQL batches and stored procedures
SQL Profiler appears.
The Connect To SQL Server dialog box appears.
The Trace Properties dialog box appears.
Notice that the only event classes being traced are RPC:Completed and SQL:BatchCompleted.
Notice that the data columns selected are being grouped by EventClass and then by Duration.
Notice that events generated by SQL Profiler are being excluded.
Notice that the Duration trace starts.
The Connect To SQL Server dialog box appears.
SQL Query Analyzer appears.
The Open Query File dialog box appears.
A Transact-SQL script appears that performs a variety of SELECT statements, which will take differing amounts of time to complete.
Notice the SELECT statements that appear in the trace.
Notice that the Transact-SQL statement executed appears in the lower pane.
The Trace Properties dialog box appears, displaying the trace properties for the Duration trace.
Notice that the existing filters are displayed. Also notice that you cannot modify a running trace.
To view current locking and user activity in SQL Server Enterprise Manager, expand the Management container for the instance and then expand the Current Activity container. See Figure 14.15.
Notice that there are three containers: Process Info, Locks/Process ID, and Locks/Object. In the console tree, click the Process Info container to view detailed information in the details pane regarding all current connections and processes. See Figures 14.16 and 14.17.
Note
Figure 14.15
The Current Activity window.
Figure 14.16
Current Activity window (Process Info—left half).
Figure 14.17
Current Activity window (Process Info—right half).
The information available in the Process Info container is described in Table 14.10.
Table 14.10 Information in the Process Info Container
Column | Description |
Process ID | SQL Server Process ID (SPID) of the process. |
Context ID | Execution context ID (ECID) of a subthread operating on behalf of the process. |
User | User ID of the user who executed the command that initiated the process. |
Database | Current database context of the process (certain system processes will have no database context). |
Status | Process status (running, sleeping, runnable, or background) of the process. |
Open Transactions | Number of open transactions for the process. |
Command | SQL Server command currently executing for the process. |
Application | Name of the application program being used by the process. |
Wait Time | Wait time (in milliseconds), if any, for the process. |
Wait Type | A string indicating the current or last wait type for the process. |
Wait Resource | Textual representation of a lock resource, such as a row identifier (RID) of the process. |
CPU | Cumulative processor time for the process. |
Physical IO | Cumulative disk reads and writes for the process. |
Memory Usage | Number of pages in the procedure cache that are currently allocated to this process. A negative number indicates pages being freed for another process. |
Login Time | Time a client process logged into the server. For server processes, the time the server started. |
Last Batch | Last time a client process executed an EXECUTE statement or a remote stored procedure call. For server processes, the time the server started. |
Host | Name of the computer that initiated the process. |
Network Library | Net-library used by the client to initiate the process. |
Network Address | Network address (the Message Authentication Code [MAC] address) for the network interface card (NIC) on the client computer that initiated the process. |
Blocked By | SPID of a blocking process for the process. |
Blocking | SPID of process being blocked by the process. |
The Locks/Process ID container displays each active process. Expand the Locks/Process ID container and then click an active process in the console tree to view the locks held by the process. See Figures 14.18 and 14.19.
Notice that SPID 53 is blocking SPID 56. SPID 53 has been granted an exclusive row lock on the same resource on which SPID 56 is waiting for a lock. For a complete list of the types and modes of locks, refer to the topic "Monitoring with SQL Server Enterprise Manager" in SQL Server Books Online. To view the most recent command issued by the blocking process, right-click the process ID and then click Properties to display the Process Details dialog box. See Figure 14.20.
Figure 14.18
Locks/Process ID information for a blocking process.
Figure 14.19
Locks/Process ID information for a blocked process.
Figure 14.20
Last TSQL command by the blocking process.
In Figure 14.20, notice that the most recent TSQL command batch began a transaction but did not complete the transaction because this open transaction is blocking other processes. To resolve a blocking lock or a deadlock (in the short term), the database administrator can either click the Send Message button to send a message to the user who initiated the blocking process or click the Kill Process button to kill the process directly.
Note
The Locks/Object container displays each database object that is locked, from the row and table level up to the database level. See Figure 14.21.
Notice that three processes have locks on the Customer table in the SSEMDB database. Process ID 53 has an exclusive row lock, and process IDs 55 and 56 are waiting to place exclusive row locks on the same row.
Figure 14.21
Locks/object information for a locked object.
In this practice you use the SQL Server Enterprise Manager and system stored procedures to monitor blocking problems.
To monitor blocking problems using the Current Activity window and system stored procedures
The Process Info details are displayed in the details pane. If you do not see the details, right-click Process Info, point to View, and then click Detail.
Notice two user processes for SelfPacedSQL\Administrator: one for the existing connection by SQL Server Enterprise Manager to the master database and one for the existing connection by SQL Query Analyzer to the Northwind database. Note the Process ID number for the connection by SQL Query Analyzer.
The Open Query File dialog box appears.
A Transact-SQL script appears that will begin a transaction to update the ContactName for a particular CustomerID. It also executes the sp_lock system stored procedure.
Notice the information returned from the sp_lock system stored procedure. In particular, note the locks granted to the SQL Query Analyzer SPID and note in particular the exclusive row lock granted to this process.
The Connect To SQL Server dialog box appears.
The Open Query File dialog box appears.
Notice that the query does not complete its execution.
Notice that the first process is blocking the second process.
The Process Details dialog box appears displaying the last TSQL command batch executed by the blocking process.
The Send Message - SelfPacedCPU dialog box appears.
A Send Message message box appears stating that the message was successfully sent and a Messenger Service message box appears containing the message (one message box will appear behind the other message box).
In the details pane, notice in the Status column that the lock request for a row lock is waiting.
In the details pane, notice the two row locks. The first process was granted an exclusive row lock and the second process is waiting.
Notice that the second query still has not completed.
Switch to the second query. Notice that it has now executed successfully. Also notice that the second query does not hold any exclusive locks at the end of the transaction.
Notice that no blocking locks or blocked processes appear.
When monitoring SQL Server performance and activity, determine the monitoring task and then select the appropriate tool. Rather than monitoring for everything all at once, perform specific and limited monitoring tasks. Repeat these same tasks over time to establish your evolving baseline. Use the SQL Server Enterprise Manager Current Activity window to manage deadlocks and to view current blocking locks.