Presentation on theme: "6. Monitoring SQL Server Performance"— Presentation transcript:
1 6. Monitoring SQL Server Performance ObjectivesMonitoring Resource UsageContentsDeveloping a Performance Monitoring MethodologyChoosing Among Monitoring ToolsPerforming Monitoring TasksPracticalsMonitor your SQL server systemSummaryThis chapter prepares you to monitor SQL Server 2000 performance and activity. You will learn about developing a performance monitoring methodology for SQL Server. You will learn about each of the Microsoft tools that are available for monitoring SQL Server 2000, including when the use of each tool is most appropriate. Finally, you will learn how to perform specific monitoring tasks using the most commonly used monitoring tools.
2 Developing a Performance Monitoring Methodology Establish appropriate and effective monitoring goalsDetermine which activities and resources to monitorDevelop a long-term monitoring strategyEstablishing Monitoring GoalsBefore you begin monitoring SQL Server 2000 performance, you should determine your monitoring goalsUser perspectiveTo minimize the response time for each query submitted by each userServer perspectiveTo maximize the total server throughput of queries submitted by usersBefore you begin monitoring SQL Server 2000 performance, you must develop a performance monitoring methodology.
3 Identifying Performance Bottlenecks Inadequate hardware resources, such as memory or processor, are common causes of bottlenecks.Low gauge meter numbers can mean that the system is performing better than expected, but they can also reveal a performance bottleneck. Usally beyond the server hardware. Can be network or software bottlenecks.Some bottlenecks can be solved by adding additional hardware resources or by moving some of the load to other servers.Solving one performance bottleneck can reveal another performance bottleneck.You might need to optimize queries that were efficient with fewer users and plenty of hardware capacity as database utilization changes
4 Determine TrendsWhen monitoring SQL Server 2000, you must gain an understanding of the normal range of values for various counters.You should establish an evolving performance baseline.This involves recording an initial performance baseline using a number of different monitoring tools.This will help you understand how various aspects of your system perform under normal production loadsRecording and monitoring the same type of information over time enables the database administrator to recognize values that are far above or far below normal. The cause of abnormal values will generally reveal the onset of a problem that warrants additional investigation. Maintaining an evolving baseline assists the database administrator in determining when additional hardware resources are needed, additional indexes should be created, or frequently run queries need to be optimized. An application or a database design that is efficient for a certain number of users can become inefficient as more users increase competition for server resources. They can also remain efficient, but simply require additional hardware resources to handle the increased load. Regularly monitoring the trends will help you to determine the areas in which you will need to concentrate time and resources.
5 Determining Resources and Activities to Monitor Hardware resources might be inadequate for the load on the serverCompeting server applications on the SQL Server 2000 computer might be using excessive resourcesHardware resource use might be unbalancedA hardware resource might be malfunctioningGeneral network congestion might occurImproper use of cursors or ad hoc queriesPoor database designPoorly written applicationsFactors to monitor that affect SQL Server 2000 performance include hardware resources, network traffic, database design, and client applications.The goal of performance monitoring is to maintain and improve the overall performance and efficiency of your SQL Server 2000 installation. This consists of improving response times and server throughput. Maintaining an evolving performance baseline enables the database administrator to detect patterns of change before serious problems occur. Solving performance problems involves identifying and isolating performance bottlenecks to determine the areas in which to concentrate resources and most effectively improve performance.
6 Choosing Among Monitoring Tools Using System MonitorWindows 2000 System Monitor is used to monitor resource usage on either the local computer or a remote computerUsing Task ManagerTask Manager is used to provide a snapshot in real timeUsing SQL ProfilerSQL Profiler is a graphical SQL Server 2000 tool used to monitor (trace) selected SQL Server eventsUsing the SQL Server Enterprise Manager Current Activity WindowThe SQL Server Enterprise Manager Current Activity window displays a snapshot of information regarding processes, user activity, locks held by processes, and locks held on objectsUsing SQL Query AnalyzerThe system stored procedures you can use to monitor SQL Server 2000 activity and performanceUsing SNMPSNMP can only monitor the default instance.SQL Server 2000 support for SNMP is enabled automaticallySQL Server 2000 and Windows 2000 provide a number of tools the database administrator can use to monitor SQL Server performance and activity. In this lesson, you are introduced to each of the tools and its use. The two primary tools are Windows 2000 System Monitor, which is used to monitor overall system resource use, and SQL Profiler, which is used to monitor selected details regarding selected SQL Server events.Look in chapter 14 lesson 1 and 2 MCSA/MCSE/MCDBA Self-Paced Training Kit
7 Performance Objects for Tracking Using System MonitorSQL Server: Access MethodsIndex and query efficiency based on types of pages accessedSQL Server: Backup DeviceThroughput or progress of backup and restoreSQL Server: Buffer ManagerLack of physical memorySQL Server: Cache ManagerEfficiency of plan caching and reuseSQL Server: DatabasesLevel of user activity in a databaseSQL Server: General StatisticsOverall connection activitySQL Server: LatchesPerformance length of waits for internal resourceSQL Server: LocksOverall number and types of locksSQL Server: Memory ManagerOverall memory usageSQL Server: SQL StatisticsQuery compilation speedSQL Server: User Settable ObjectSystem Monitor is generally used either to view real-time performance data or to record data to disk for later review and analysis. System Monitor includes performance objects, counters, and instances.Note!Performance objects generally correspond to hardware resources
8 Event Categories That Can Be Monitored Using SQL ProfilerCursorsThe actual types of cursors being usedDatabaseAutomatic growth of data and transaction log filesErrors and WarningsLocksObjectscreation of objects by applications and usersPerformanceQuery execution and query optimizer efficiencyScansTypes of scans being performed on an object. (tables/indexes)Security auditServerChanges in SQL Server memory usageSessionsConnected users, database activityStored proceduresto determine additional memory needsTransactionsTSQLSQL Profiler is a graphical SQL Server 2000 tool used to monitor (trace) selected SQL Server events, and save the information to a table or file with a .TRC filename extension for later analysis. For example, you can monitor slowly executing stored procedures or events immediately preceding deadlocks. You can create traces and then replay them (in real time or step by step) on another computer running SQL Server (a test server) to debug performance and coding problems with Transact-SQL statements or stored procedures
9 Using SQL Query analyzer Sp_whoActive users and their processesSp_who2Active users and their processes, more readable.Sp_lockBlocking locks and deadlocks, and the process causing them.sp_spaceusedDatabase or object space usage.Sp_monitorVolume of work performed during period of time.There are a number of types of Transact-SQL statements that you can issue to monitor SQL Server 2000 activity and performance. These statements can display either current resource information or performance over a period of time.In addition, all of the functions of SQL Profiler can be executed using stored procedures. These include the sp_trace_create, sp_trace_generateevent, sp_trace_setevent, sp_trace_setfilter, and sp_trace_setstatus system stored procedures.
10 Task Manager Monitoring Resource momentary Usage View these columns 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 ManagerIn the Processes tab, notice the five default columns of information that are displayed. You can sort the information displayed by clicking a column heading. Click Select Columns on the View menu to select additional columns to appear on the Processes page
11 Task Manager Monitoring Resource momentary Usage System to left has enough memory, system to right need more physical memoryMonitoring SQL Server 2000 consists of performing various tasks to monitor levels of resource usage and specific SQL Server events.
12 System Monitor, analysis Object Counter Recomended value MeasuresProcessor %cpu time <70% Time spent workingMemory Pages/sec <5 Ram to disk and reverseMemory Available >4MBhyte Non allocated memory.Memory Commited < physical ram Amount of ram commitedPhysicalDisk Disk queue <2 Waiting to be writtenDisk % Disk time <50% Disk busyNetworkSegment Network use <30% Network bandwith usedIn order to see the Network Segment: % Network Utilization, you must install the Network Monitor Agent in the Add/Remove Programs applet in Control Panel.If you don’t enable the logical disk counters by executing diskperf –yv, all logical disk counters will read zero. Physical disk counters are turned on by default in Windows 2000.See chapter 14 lession 3 in MCSA/MCSE/MCDBA Self-Paced Training Kit
13 Monitoring with Windows System Monitor Log in to Windows as Administrator.From the Start menu, select Programs -> Administrative Tools -> Performance.Click the + icon on the toolbar to start adding counters to the chart.You can connect to any SQL server that you have access to, here it is the default on my server after logging in as admin.
14 Monitoring, adding some counters 4. In the Performance Object drop-down list, select Processor (not Process).5. In the counter list box, select % Processor Time and click Add.6. In the Performance Object drop-down list, select Memory.7. In the Counter list box, select Pages/Sec and click Add.8. Click Close and notice the graph being created on the screen.
15 Monitoring, highligting & alerts 9. Press Ctrl+H and notice the current counter turn white. This makes the chart easier to read.10. In the left pane, under Performance Logs and Alerts, select Alerts.11. From the Action menu, select New Alert Settings.12. Enter Test Alert in the Name box and click OK.
16 Monitoring, making alert on processor 13. In the Comment box on the General tab of the Test Alert property sheet, type Processor Alert.14. Click Add, select Processor in the Performance Object drop-down list and % Processor Time in the Counter list box, click Add, then click Close.15. Back in the Test Alert property sheet select Alert When the Value Is Under and set the value to 100. This will generate an alert if the processor is not busy 100 percent of the time. In the real world, this would be set to Over 70 percent, thus warning you just before it becomes a serious problem.
17 Monitoring, making alert on processor 16. On the Action tab, check the Send a Network Message To box and enter your machine name in the text box below.17. Click OK to start the alert and watch the alerts generated for a short time. Then right-click the alert and select Stop to stop the alert from firing.18. Exit Windows System Monitor.From now on, alerts would be sent to server and logs would be written.This when processor time is under 70 %, this is just testing, in real life OVER would make sense
18 Logging with Windows System Monitor Open Windows System Monitor from the Administrative Tools folder on the Start menu.In the left pane, select Counter Logs.From the Action menu, select New Log Settings and when the New Log Settings dialog box appears, name the new log Test Log, then click OK.On the General tab of the Test Log property sheet, under the Counters box, click Add.In the Performance Object drop-down list, select Processor (not Process).In the Counter list box, select % Processor Time and click Add.In the Performance Object drop-down list, select Memory.In the Counter list box, select Pages/Sec and click Add.
19 Logging with Windows System Monitor 9. Click Close to return to the Test Log dialog box.10. On the Log Files tab, notice the location and name of the files that will be created and click OK to start the log11. Allow the log to run for about five minutes to gather some data.12. Right-click the Test Log and select Stop to stop the log so that you can read from it.13.In the left pane, click System Monitor.14. Now right-click in the gray area of the chart and select Properties.15. On the Source tab, click Log File.
20 Logging with Windows System Monitor 16. Click Browse, select the log file created in step 10, and click OK.17. Click the + icon on the toolbar to add objects to the graph.18. In the Add Counters dialog box, in the Performance Object drop-down list, select Processor (not Process).19. In the Counter list box, select % Processor Time and click Add.20. In the Performance Object drop-down list, select Memory.21. In the Counter list box, select Pages/Sec and click Add.22. Click close and notice that the recorded data now appears in the graph.23. Close Windows System monitor
21 Monitoring Current Locking and User Activity 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.
22 Monitoring Current Locking and User Activity Process information, right click on process and select properties.Locks and Processes IDTo view the most recent command issued process, right-click the process ID and then click Properties to display the Process Details dialog boxIt is possible to see if some process is blocking another which is waiting to access a table.
23 Monitoring Current Locking and User Activity Process information,Locks and Processes IDright click on process and select properties. You can see latest SQL message caused processTo view the most recent command issued process, right-click the process ID and then click Properties to display the Process Details dialog boxIt is possible to see if some process is blocking another which is waiting to access a table.
24 Using Query Analyzer, check your expressions 1. From the Start menu, choose Programs -> Microsoft SQL Server -> Query Analyzer.2. When asked to log in, use Windows Authentication. You will see the query window.3. From the Query menu, select Current Connection Properties.4. In the property sheet, check Set Statistics Time and Set Statistics IO. Set Statistics Time displays CPU time used, while Set Statistics IO displays disk time.Up to this point, you have been using Query Analyzer to enter queries and see results, but it can do more. One clue as to its enhanced capabilities comes from its name: Query Analyzer. It is used not only to enter queries, but also to analyze them, to see how many resources they consume, and to see how fast they run
25 Using Query Analyzer, check your expressions 5. From the Query menu, select Show Execution Plan to see a graphic representation of how SQL Server executes your query.6. On the query window toolbar, select Northwind in the DB list box to set Northwind as the default database.7. In the query window, type the following query:8. USE NorthwindSELECT * FROM employees9. Click the Messages tab (at the bottom of the screen) and notice the execution, parse, and compile times; then click the Execution Plan tab.10. In the Execution Plan pane, hold your mouse pointer over each icon in turn; notice that they come with ToolTips to help you better understand each step of execution.
26 Monitoring with SQL Profiler 1. From the Start menu, choose Programs -> SQL Server -> Profiler.2. From the File menu, choose New, and then click Trace to bring up the Trace property sheet. You will be asked to log in at this point; do so with either form of authentication.3. In the Trace Name box, type Monitor.4. There are several templates to choose from in the templates drop-down list box. Each of them gives you a preset trace definition to work with. Leave the template name as SQLProfilerStandard.5. Check the Save to File check box and click Save in the Save As dialog box to accept the default name and location.6. Check the Save to Table check box, log in to the server again, and in the Destination Table dialog box fill in the following:Database: NorthwindTable: Monitor7. Click OK to return to the Trace property sheet.When running a company, once you have the management team working in harmony, you can focus your attention on the rest of the workforce. In this analogy, Query Analyzer would be like interviewing prospective employees: before you hire them, you want to be sure they have the appropriate qualifications, can fit in with the rest of the team, and will do their fair share of the work. Like new employees, new queries need to be monitored regularly (with other queries, on a day-to-day basis).Profiler allows you to monitor and record what is happening inside the database engine. This is accomplished by performing a trace, which is a record of data that has been captured about events. Traces can be stored in a table, a trace log file, or both for future reference.The actions you will be monitoring are called events and are logically grouped into event classes. Some of these events are useful for maintaining security, and some are useful for troubleshooting problems, but most of these events are used for monitoring and optimization
27 Monitoring with SQL Profiler 7. Click the Events tab.8. Under Available Event Classes, select Objects and click Add. This will monitor the opening and closing of objects such as tables.9. Click the Data Columns tab to change the data you see in the trace.10. Under Unselected Data, select End Time and click Add.11. Click Run to start the trace.When running a company, once you have the management team working in harmony, you can focus your attention on the rest of the workforce. In this analogy, Query Analyzer would be like interviewing prospective employees: before you hire them, you want to be sure they have the appropriate qualifications, can fit in with the rest of the team, and will do their fair share of the work. Like new employees, new queries need to be monitored regularly (with other queries, on a day-to-day basis).Profiler allows you to monitor and record what is happening inside the database engine. This is accomplished by performing a trace, which is a record of data that has been captured about events. Traces can be stored in a table, a trace log file, or both for future reference.The actions you will be monitoring are called events and are logically grouped into event classes. Some of these events are useful for maintaining security, and some are useful for troubleshooting problems, but most of these events are used for monitoring and optimization
28 Monitoring with SQL Profiler 12. Leave Profiler running and open Query Analyzer; log in using Windows Authentication.13. Execute the following query:14. USE NorthwindSELECT * FROM products15. Switch back to Profiler and click the Pause button (double blue lines). Notice the data that was collected by the trace.You may have found the amount of data collected by the trace overwhelming. That data was for only one user executing one query. Imagine trying to sort through a trace of hundreds of users with dozens of queries—a daunting task, to say the least. Fortunately, you will not be subjected to such tortures because you can filter your trace data.
29 Using the Index Tuning Wizard 1. Open Profiler.2. From the Tools menu, select Index Tuning Wizard. This will open the Welcome screen.3. Click Next and log in.4. On the Select Server and Database screen, select Northwind as the database to tune.5. Check Keep All Existing Indexes.6. Check Thorough as the analysis type and click Next.Systems, like automobiles or musical instruments, must be properly tuned to function properly. If even one SQL Server index were out of tune, it could slow down the entire system. Perhaps the wrong columns were indexed from the beginning, or maybe users started querying different data over time, which would require the creation of new indexes. In situations such as these, your indexes need tuning.The one thing you need before you can run the Index Tuning Wizard is a workload. You get this by running and saving a trace in Profiler. It is best to get this workload during times of peak database activity to make sure you give the wizard an accurate load.
30 Using the Index Tuning Wizard 7. In the Specify Workload screen, click the My Workload File button.8. In the File Open dialog box, select the Monitor trace (created earlier) and click OK to return to the Specify Workload screen, where you will see the Monitor file listed under My Workload File.9. Click the Advanced Options button, note the defaults, and click OK.10. Click Next.11. In the Select Tables to Tune screen, click Select All Tables.
31 Using the Index Tuning Wizard 12. Click Next, and the wizard will start examining your indexes.13. After SQL Server has finished examining your queries it displays a list of recommended indexes for you to create. Browse this list and click Next.14. On the final screen, click Finish to end the wizard.15. When you receive a message stating that the wizard has completed, click OK.16. Exit Profiler.
32 Optimizing Techniques Queries and Stored ProceduresThe first thing to ask yourself when you are getting slow response times is whether you could be using a stored procedure here instead of a client-side query.TempdbTempdb should be between 25 and 40 percent of the size of your largest databaseIs your Tempdb big enough to handle the load that your queries put on it? Think of Tempdb as a scratchpad for SQL Server; when queries are performed, SQL Server uses this scratchpad to make notes about the result setQuery GovernorControlled by the Query Governor Cost Limit setting. This setting tells SQL Server not to run queries longer than x. Limit is set to 2, any query that is estimated to take longer than two seconds would not be allowed to runMax Async I/OThe maximum number of asynchronous input/output (Max Async I/O) threads by default in SQL Server is 32. That means that SQL Server can have 32 outstanding read and 32 outstanding write requests at a timeLazyWriterLazyWriter is a SQL Server process that moves information from the data cache in memory to a file on disk. Average Disk Queue Windows System Monitor counter and verify that it is not more than 2 per physical diskSQL Server has the ability to dynamically adjust most of its settings to compensate for problems. It can adjust memory use, threads spawned, and a host of other settings. In some cases, unfortunately, those dynamic adjustments may not be enough and you may need to make some manual changes.Note! You will need to divide the Average Disk Queue counter by the number of physical drives to get an accurate count