Skip to main content

What is FORCESEEK And FORCESCAN Table Hints

FORCESEEK and FORCESCAN are two Table Hint options you can use to avoid performance issues by enforcing SQL Server engine to execute by overriding what is chosen by cardinality estimation.

Lets see the usage of it. I used AdventureWorks2012 sample database and written a query by joining SalesOrderHeader and SalesOrderDetails table.

I set the Time and IO statistics to ON for getting the execution statistics. (Refer the below screen)



38,575 rows returned by execution the query. Now take a look at Actual Execution Plan of the query.


If you have a closer look at the Execution plan you might noticed that SalesOrderDetails table has used Table Scan though the tables contain clustered keys on OrderID columns and used for join condition.


Lets make a note of Statistics information too what we received by executing the query.(Please, make sure to clean the cache before you getting the execution IO and Time info in-order to get more accurate stat.



Now, lets use FORCESEEK table hint and see the execution plan.



If you see the execution plan now you might notice SalesOrderDetail table has used Index Seek instead of Table scan.



Statistics information for the query execution with FORCESEEK Table hint.

You can clearly see, there is a little difference of Execution times and with the FORCESEEK table hint execution performed well than earlier.

Note: FORCESEEK and FORCESCAN table hints we can use to override the default cardinality estimation. Most of the scenarios SQL Server engine produce and select the best and accurate execution plan. But, there are scenarios you may need to override by enforcing it, where table hints coming into play.

Every time when we enforce to engine we should make sure there won't be any performance bottle-neck after apply it. You can test queries by using IO and Time statistics whether you gain any performance.

Reference: https://technet.microsoft.com/en-us/library/bb510478(v=sql.105).aspx

Comments

Post a Comment

Popular posts from this blog

Step-by-Step Twitter Sentiment Analysis Using Power BI Streaming Dataset, Microsoft Flow and Azure Text API

Sentiment Analysis is known as Opinion mining or emotion AI which is a branch of Natural Language Processing and text analytics where systematically identify, extract, quantify, and study affective states and subjective information. This kind a analysis widely apply to analyse the product or service reviews, voice of the customer, survey responses from online and social media feeds to analyze the attitude of the customer. Basically from the sentiment analysis the output would be either Positive, Negative or Neutral.  There are various algorithms and methods to do a sentiment analysis out there. In this post here I'm doing a sentiment analysis for iPhone 8 product by analyzing twitter feeds. Because, I wanted to know what others are thinking about the latest phone released by Apple. In order to do this task I'm using,  Microsoft Azure cognitive services : Text Analytics (to run the sentiment analysis algorithms to get out the results)  Microsoft Flow ...

How to Get Row Counts for all Tables in your SQL Database

This is the simplest method to get the row counts of all tables in your SQL Server database. Might be useful when you are doing the data testing over your BI project.     select   schema_name (tab.schema_id) , tab.name  as  [table],         sum (part.rows)  as  [rows]     from  sys.tables  as  tab          inner join  sys.partitions  as  part              on  tab.object_id  =  part.object_id where  part.index_id  IN  ( 1 ,  0 )  //   0 :  Table   without  PK,  1 :  table   with  PK and   schema_name (tab.schema_id)  =   'dbo'   group by   schema_name (tab.schema_id) , tab.name --order by sum(part.rows) desc  

COVID-19 Situation in Sri Lanka: Real-time Dashboard using Power BI

[Updated on March 28, 2020] COVID-19  is a new strain that was discovered in late December, 2019 and by now it has been started to spread all over the world including 199 countries. There are around 26,000 people were died due to the virus and 580,000 confirmed cases at the moment I update this blog post. Though, the fatality rate is lower when compared to other viruses in similar strains like MERS and SARS . However, the biggest challenge most of the countries facing right now is to accommodate the patients with the growing numbers at every minute. The public was asked by the government to avoid nonessential gatherings as much as possible to stop spreading this deadly virus. Even though there were not many cases found in Sri Lanka, from March first week, they found the initial case a patient who works as a tourist. Now the situation is turning to worst after they found many cases especially tourists who are born Sri Lankans came down from Italy and European countries....