Use hints to test indexes in SQL Server)

Source: Internet
Author: User

From http://www.mssqltips.com/tip.asp? Tip = 2045

Using hints to test indexes in SQL Server
Written by: Ken Simmons -- 6/24/2010 -- 1 comments -- print -- free stuff --

Problem
When tuning indexes, it is somewhat difficult to tell how much impact the changes you have made will have on a query. other than just looking at the different execution plans, is there an easy way to compare the queries using the old and new indexes?

Solution
After making index changes, I always like to use hints to see how much impact the change has on a query. Let's take the following query that uses the adventureworks database as an example.

Use

 

Adventureworks
Go

Select

Nationalidnumber
From HumanResources. Employee
Where title = 'stocker'

As you can see by the following execution plan, the query uses a clustered index scan to retrieve the information.

Since the query is searching by title and returning the nationalidnumber, you can create the following non-clustered index on title and include the nationalidnumber, so all of the information the query needs is located in the index. you shoshould note that if you did not include the nationalidnumber, the optimizer wowould still find it more optimal to scan the clustered index instead of seeking the non-clustered index and having to lookup the nationalidnumber.

Create

Index ix_title_include
On HumanResources. employee (title)
Include (nationalidnumber)

 

Index ix_title_include
On HumanResources. employee (title)
Include (nationalidnumber)

Index ix_title_include
On HumanResources. employee (title)
Include (nationalidnumber)

 

 

After creating the new index and rerunning the original query, you will now see that the query is refreshing an index seek on the new non-clustered index. that sounds good, but how much more efficient is it?

In order to answer that question, you can use a hint to force one of the queries to use the original index as shown in the following query.

Select

 

Nationalidnumber
From HumanResources. Employee
With (index (pk_employee_employeeid ))
Where title = 'stocker'

Select

 

Nationalidnumber
From HumanResources. Employee
Where title = 'stocker'

You can now see that using the new index, the query cost is only 29% compared to the original index which is 71%. if you saw something like 50/50, you may want to rethink the new index, because it hasn't given you any performance and now you have an additional index to update and maintain.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.