programming4us
programming4us
DATABASE

SQL Server 2008: Managing Query Performance - Forcing Index Seeks

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
The FORCESEEK table hint was introduced in SQL Server 2008 to force an index seek in an execution plan in place of an index scan. The query optimizer does a really good job deciding whether a seek or scan is more efficient, but in certain scenarios you may want to override the execution plan's decision. For example, poor cardinality estimation using the LIKE and IN operators may cause the query optimizer to choose an index scan when an index seek may be more efficient. Another reason for a poor execution plan could be due to plan reuse. For example, an execution plan could be initially generated using parameters that were more efficient using a scan, and now the plan is reused several times for parameters that would be more efficient using a seek. Take a look at the query in Listing 1.
Example 1. Query to Create a Clustered Index Scan
USE AdventureWorks2008
GO

SELECT *
FROM HumanResources.Employee A
JOIN HumanResources.EmployeeDepartmentHistory B
ON A.BusinessEntityID = B.BusinessEntityID
WHERE A.BusinessEntityID > 270 OR
(A.BusinessEntityID < 10 and B.DepartmentID =1)

As you can see in Figure 1, the query in Listing 1 used a clustered index scan on the EmployeeDepartmentHistory table, and then used a merge join to combine the results with the Employee table.

Figure 1. Execution plan created by running the query in Listing 16-10

Now let's see what happens when we execute the same query using the FORCESEEK table hint by running the query in Listing 2.

Example 2. Query to Force a Clustered Index Seek
USE AdventureWorks2008
GO

SELECT *
FROM HumanResources.Employee A
JOIN HumanResources.EmployeeDepartmentHistory B WITH (FORCESEEK)
ON A.BusinessEntityID = B.BusinessEntityID
WHERE A.BusinessEntityID > 270 OR
(A.BusinessEntityID < 10 and B.DepartmentID =1)

As you can see in Figure 2, using the FORCESEEK table hint caused the same query to use a clustered index seek on the EmployeeDepartmentHistory table, and the results are now joined with the Employee table using nested loops.

Figure 2. Execution plan created by running the query in Listing 2

Both of these execution plans provide almost identical performance metrics because the result set is so small. However, as the parameters change and the result set gets larger, the execution plan originally chosen by the query optimizer will outperform the execution plan generated using the FORCESEEK option.

Before considering using the FORCESEEK table hint, you should make sure the statistics for the database are up to date. In most cases, the query optimizer will choose the best execution plan for a query, but in rare cases, the FORCESEEK table hint could prove to be a very useful new feature.

Other  
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 4)
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 3)
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 2) - Suspending and Reseeding a Database
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 1) - Creating the File Share Witness
  •  Database Availability Group Replication in Exchange Server 2010 : Understanding Database Availability Groups
  •  SQL Server 2008 : Managing Query Performance - Optimizing for Specific Parameter Values
  •  SQL Server 2008 : Managing Query Performance - Running the Standard Performance Reports
  •  SQL Server 2008 : Explaining XML - Well-Formed XML
  •  SQL Server 2008 : Explaining XML - XML Schema
  •  SQL Azure : Tuning Techniques (part 5) - Provider Statistics & Application Design
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us