Chapter 5. Database Engine Tuning Advisor

SQL Server's performance largely depends upon having proper indexes on the database tables. However, as the workload and data change over time, the existing indexes may not be entirely appropriate, and new indexes may be required. The task of deciding upon the correct indexes is complicated by the fact that an index change that benefits one set of queries may be detrimental to another set of queries.

To help you through this process, SQL Server provides a tool called the Database Engine Tuning Advisor. This tool helps identify an optimal set of indexes and statistics for a given workload without requiring an expert understanding of the database schema, workload, or SQL Server internals. It can also recommend tuning options for a small set of problem queries. In addition to the tool's benefits, I cover its limitations in this chapter, because it is a tool that can cause more harm than good if used incorrectly.

In this chapter, I cover the following topics:

  • How the Database Engine Tuning Advisor works

  • How to use the Database Engine Tuning Advisor on a set of problematic queries for index recommendations, including how to define traces

  • The limitations of the Database Engine Tuning Advisor

Database Engine Tuning Advisor Mechanisms

You can run the Database Engine Tuning Advisor directly by selecting Microsoft SQL Server 2008

Database Engine Tuning Advisor Mechanisms
Selecting the server and database in the Database Engine Tuning Advisor

Figure 5.1. Selecting the server and database in the Database Engine Tuning Advisor

The Database Engine Tuning Advisor is already connected to a server. From here, you begin to outline the workload and the objects you want to tune. Creating a session name is necessary to label the session for documentation purposes. Then you need to pick a workload, either a file or a table, and browse to the appropriate location. The workload is defined depending on how you launched the Database Engine Tuning Advisor. If you launched it from a query window, you would see a Query radio button, and the File and Table radio buttons would be disabled. You also have to define the Database for Workload Analysis setting and finally select a database to tune.

Tip

The Database Engine Tuning Advisor recommends indexed views only for platforms that support them. SQL Server 2008 Enterprise Edition does, but Standard Edition doesn't.

When you select a database, you can also select individual tables to be tuned by clicking the drop-down box on the right side of the screen; you'll see a list of tables like those shown in Figure 5-2.

Clicking the boxes defines individual tables for tuning in the Database Engine Tuning Advisor.

Figure 5.2. Clicking the boxes defines individual tables for tuning in the Database Engine Tuning Advisor.

Once you define the workload, you need to select the Tuning Options tab, which is shown in Figure 5-3.

Defining options in the Database Engine Tuning Advisor

Figure 5.3. Defining options in the Database Engine Tuning Advisor

You define the length of time you want the Database Engine Tuning Advisor to run by selecting Limit Tuning Time and then defining a date and time for the tuning to stop. The longer the Database Engine Tuning Advisor runs, the better recommendations it should make. You pick the type of physical design structures to be considered for creation by the Database Engine Tuning Advisor, and you can also set the partitioning strategy so that the tuning advisor knows whether it should consider partitioning the tables and indexes as part of the analysis. Just remember, partitioning isn't necessarily a desirable outcome if your data and structures don't warrant it. Finally, you can define the physical design structures that you want left alone within the database. Changing these options will narrow or widen the choices that the Database Engine Tuning Advisor can make to improve performance.

You can click the Advanced Options button to see even more options, as shown in Figure 5-4.

Advanced Tuning Options dialog box

Figure 5.4. Advanced Tuning Options dialog box

This dialog box allows you to limit the space of the recommendations and the number of columns that can be included in an index. Finally, you can define whether the new indexes or changes in indexes are done as an online or offline index operation.

Once you've appropriately defined all of these settings, you can start the Database Engine Tuning Advisor by clicking the Start Analysis button. The sessions created are kept in the msdb database for any server instance that you run the Database Engine Tuning Advisor against. It displays details about what is being analyzed and the progress made, which you can see in Figure 5-5.

Tuning progress

Figure 5.5. Tuning progress

You'll see more detailed examples of the progress displayed in the example analysis in the next session.

After the analysis completes, you'll get a list of recommendations (visible in Figure 5-6), and a number of reports become available. Table 5-1 describes the reports.

Table 5.1. Database Engine Tuning Advisor Reports

Report Name

Report Description

Column Access

Lists the columns and tables referenced in the workload.

Database Access

Lists each database referenced in the workload and percentage of workload statements for each database.

Event Frequency

Lists all events in the workload ordered by frequency of occurrence.

Index Detail (Current)

Defines indexes and their properties referenced by the workload.

Index Detail (Recommended)

Is the same as the Index Detail (Current) report but shows the information about the indexes recommended by the Database Engine Tuning Advisor.

Index Usage (Current)

Lists the indexes and the percentage of their use referenced by the workload.

Index Usage (Recommended)

Is the same as the Index Usage (Current) report but from the recommended indexes.

Statement Cost

Lists the performance improvements for each statement if the recommendations are implemented.

Statement Cost Range

Breaks down the costs improvements by percentiles to show how much benefit you can achieve for any given set of changes.

Statement Detail

Lists the statements in the workload, their cost, and the reduced cost if the recommendations are implemented.

Statement-to-Index Relationship

Lists the indexes referenced by individual statements. Current and recommended versions of the report are available.

Table Access

Lists the tables referenced by the workload.

View-to-Table Relationship

Lists the tables referenced by materialized views.

Workload Analysis

Gives details about the workload, including the number of statements, the number of statements whose cost is decreased, and the number where the cost remains the same.

Database Engine Tuning Advisor Examples

The best way to learn how to use the Database Engine Tuning Advisor is to use it. It's not a terribly difficult tool to master, so I recommend opening it and getting started.

Tuning a Query

You can use the Database Engine Tuning Advisor to recommend indexes for a complete database by using a workload that fairly represents all SQL activities. You can also use it to recommend indexes for a set of problematic queries.

To learn how you can use the Database Engine Tuning Advisor to get index recommendations on a set of problematic queries, say you have a simple query that is called rather frequently. Because of the frequency, you want a quick turnaround for some tuning. This is the query:

SELECT  soh.DueDate
       ,soh.CustomerID
FROM    Sales.SalesOrderHeader AS soh
WHERE   DueDate BETWEEN '1/1/1970' AND '1/1/1971'
        AND Status > 200

To analyze the query, right-click it in the query window, and select Analyze Query in the Database Engine Tuning Advisor. The advisor opens with a window where you can change the session name to something meaningful. In this case, I chose Report Query Round 1 – 10/5/2008. The database and tables don't need to be edited. The first tab, General, will look like Figure 5-6 when you're done.

Query tuning general settings

Figure 5.6. Query tuning general settings

Because this query is important and tuning it is extremely critical to the business, I'm going to change some settings on the Tuning Options tab. For the purposes of the example, I'm going to let the Database Engine Tuning Advisor run for 15 minutes, but a more realistic approach here would be to let it run for an hour. I'm going to select the Include Filtered Indexes check box so that if a filtered index will help, it can be considered. I'm also going to switch the Partitioning Strategy to Employ setting from No Partitioning to Full Partitioning. Finally, I'm going to allow the Database Engine Tuning Advisor to come up with structural changes if it can find any that will help by switching from Keep All Existing PDS to Do Not Keep Any Existing PDS. Once completed, the Tuning Options tab will look like Figure 5-7.

Tuning Options tab adjusted

Figure 5.7. Tuning Options tab adjusted

After starting the analysis, the progress screen should appear. Although the settings were for 15 minutes of evaluations, it took only about a minute for it to evaluate this query. The initial recommendations were not good. As you can see in Figure 5-8, the Database Engine Tuning Advisor has recommended dropping a huge swath of indexes in the database. This is not the type of recommendation that you want when running the tool.

Query tuning initial recommendations

Figure 5.8. Query tuning initial recommendations

This is because the Database Engine Tuning Advisor assumes that the load being tested is the full load of the database. If there are indexes not being used, then they should be removed. This is a best practice and one that should be implemented on any database. However, in this case, this is a single query, not a full load of the system. To see whether the advisor can come up with a meaningful recommendation, you must start a new session.

This time, I'll adjust the options so that the Database Engine Tuning Advisor will not be able to drop any of the existing structure. This is set on the Tuning Options tab (shown earlier in Figure 5-7). There I'll change the Physical Design Structure (PDS) to Keep in Database setting from Do Not Keep Any Existing PDS to Keep All Existing PDS. I'll keep the running time the same because the evaluation worked well within the time frame. Running the Database Engine Tuning Advisor again, it finishes in less than a minute and displays the recommendations shown in Figure 5-9.

Query tuning recommendations

Figure 5.9. Query tuning recommendations

The first time through, the Database Engine Tuning Advisor suggested dropping most of the indexes on the tables being tested and a bunch of the related tables. This time it suggests creating a covering index on the two columns referenced in the query. As outlined in Chapter 4, a covering index is one of the most performant methods of indexing. The Database Engine Tuning Advisor was able to recognize that creating an index with all the columns referenced by the query, a covering index, would perform best.

Once you've received a recommendation, you should take a look at the proposed T-SQL command. Finally, you'll want to apply the recommendation. Select Actions

Query tuning recommendations
Apply Recommendations dialog box

Figure 5.10. Apply Recommendations dialog box

If you click the OK button, the Database Engine Tuning Advisor will apply the index to the database where you've been testing queries (see Figure 5-11).

A successful tuning session applied

Figure 5.11. A successful tuning session applied

After you generate recommendations, you may want to, instead of applying them on the spot, save the T-SQL statements to a file and accumulate a series of changes for release to your production environment during scheduled deployment windows. Remember that applying indexes to tables, especially large tables, can cause a performance impact to processes actively running on the system while the index is being created.

Although getting index suggestions one at a time is nice, it would be better to be able to get large swaths of the database checked all at once. That's where tuning a trace workload comes in.

Tuning a Trace Workload

Capturing a trace from the real-world queries that are running against a production server is a way to feed meaningful data to the Database Engine Tuning Advisor. (Capturing traces was covered in Chapter 3.) The easiest way to define a trace for use in the Database Engine Tuning Advisor is to implement the trace using the Tuning template. Start the trace on the system you need to tune. I generated three different loads by running queries in a loop from the sqlps.exe command prompt. This is the PowerShell command prompt with the SQL Server configuration settings. It gets installed with SQL Server.

These are the scripts run (queryload.ps1):

$val = 0; while ($val -lt 50) {$val++; Invoke-Sqlcmd
-Server "YourServerName" -Database "AdventureWorks2008"
-InputFile "pathqueryload.sql"}
$val = 0; while ($val -lt 20000){$val++; Invoke-sqlcmd
-Server "YourServerName" -Database "AdventureWorks2008"
-Query "EXEC dbo.uspGetEmployeeManagers @BusinessEntityID = $val"}
$val = 0; while ($val -lt 20000){$val++; Invoke-sqlcmd
-Server "YourServerName" -Database "AdventureWorks2008"
-Query "EXEC dbo.uspGetManagerEmployees @BusinessEntityID = $val"}

The queryload.sql file is also available in the download file. It's just a collection of queries with various parameters to provide a simulated load.

Note

For more information on PowerShell, check out Pro Windows PowerShell by Hristo Deshev (Apress, 2008).

Once you've created the trace file, open the Database Engine Tuning Advisor. It defaults to a file type, so you'll only have to browse to the trace file location. Set the appropriate tuning options, and start the analysis. This time, it will take more than a minute to run (see Figure 5-12).

Database tuning engine in progress

Figure 5.12. Database tuning engine in progress

As you can see, simply passing any number to the uspGetEmployeeManagers procedure, and others, can generate instances where no tables were referenced. The processing runs for about 15 minutes on my machine. Then it generates output, shown in Figure 5-13.

No recommendations

Figure 5.13. No recommendations

After running all the queries through the Database Engine Tuning Advisor, the best recommendation it could currently come up with was to drop a couple of indexed views. That doesn't mean there aren't other possible improvements; it just means that the advisor is not always able to recognize all the possible improvements.

Database Engine Tuning Advisor Limitations

The Database Engine Tuning Advisor recommendations are based on the input workload. If the input workload is not a true representation of the actual workload, then the recommended indexes may sometimes have a negative effect on some queries that are missing in the workload. But most important, as you saw in the second example of this chapter, the Database Engine Tuning Advisor may not recognize possible tuning opportunities. It has a sophisticated testing engine, but in some scenarios, its capabilities are limited.

For a production server, you should ensure that the SQL trace includes a complete representation of the database workload. For most database applications, capturing a trace for a complete day usually includes most of the queries executed on the database. A few of the other considerations/limitations with the Database Engine Tuning Advisor are as follows:

  • Trace input using the SQL:BatchCompleted event: As mentioned earlier, the SQL trace input to the Database Engine Tuning Advisor must include the SQL:BatchCompleted event; otherwise, the wizard won't be able to identify the queries in the workload.

  • Query distribution in the workload: In a workload, a query may be executed multiple times with the same parameter value. Even a small performance improvement to the most common query can make a bigger contribution to the performance of the overall workload, compared to a large improvement in performance of a query that is executed only once.

  • Index hints: Index hints in a SQL query can prevent the Database Engine Tuning Advisor from choosing a better execution plan. The wizard includes all index hints used in a SQL query as part of its recommendations. Because these indexes may not be optimal for the table, remove all index hints from queries before submitting the workload to the wizard, bearing in mind that you need to add them back in to see whether they do actually improve performance.

Summary

As you learned in this chapter, the Database Engine Tuning Advisor is a useful tool for analyzing the effectiveness of existing indexes and recommending new indexes for a SQL workload. As the SQL workload changes over time, you can use this tool to determine which existing indexes are no longer in use and which new indexes are required to improve performance. It can be a good idea to run the wizard occasionally just to check that your existing indexes really are the best fit for your current workload. It also provides many useful reports for analyzing the SQL workload and the effectiveness of its own recommendations. Just remember that the limitations of the tool prevent it from spotting all tuning opportunities. If your database is in bad shape, this tool can give you a quick leg up. If you're already monitoring and tuning your queries regularly, you may see no benefit from the recommendations of the Database Engine Tuning Advisor.

Frequently, you will rely on nonclustered indexes to improve the performance of a SQL workload. This assumes that you've already assigned a clustered index to your tables. Because the performance of a nonclustered index is highly dependent on the cost of the bookmark lookup associated with the nonclustered index, you will see in the next chapter how to analyze and resolve a bookmark lookup.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.240