Baskar B.V

Microsoft Business Unit, Wipro Technologies

  Home  |   Contact  |   Syndication    |   Login
  21 Posts | 0 Stories | 14 Comments | 0 Trackbacks

News

Baskar

Archives

.NET Interop

BizTalk Links

Blogs

SQL-SERVER

When sql queries running very slower we can consider forcing an index using WITH INDEX command

Syntax: Select * from <tablename> with (index(<index name>))

This we found very useful in one of our project where we were faced performance issues. Our code is basically querying a table with date condition.

select @st_dt =  max(field1)   from table1 with (index (index_nm))  where field1 <= @start_dt

The date value is passed through sql variable. The query is running for ever even though it has the index on field1. We tried to run the query using WITH INDEX by mentioning the index name on the field. This query ran just like that in few minutes.

You can think of using forced index when you pass some values in where clause thru variables and only the indexed fields are used in the where clause. 

Let me know your thoughts on this.

posted on Thursday, June 26, 2008 1:59 PM

Feedback

# re: forced index using WITH INDEX 6/27/2008 11:27 AM John Workman
This can be a very dangerous practice. The concern with this is right index today may not be the right index tomorrow. If this begins to perform poorly in a year’s time, as the data set grows, you will be required to do a patch and deploy new code to fix a hardcoded index. The same thing goes for forcing the type of join (such as a hash join). I have seen a few (very few) cases where there was no other option, but in most cases there are ways to “help” sql pick the right index. The query optimizer will do a nice job, in most cases of picking the right index. In a case where it does not, it is your job to figure out why sql skips the index that you know will perform better. Many people believe that the less joins and the less filters in a where clause, the better. That is not always the case. Sometimes additional filters are necessary to help the index. Sometimes additional joins will help the index. For example, if I have a table that has a primary key on site_id, date, item_id. It contains columns with transaction_count and sales_amt. If I want to pull sales across multiple days for multiple sites, the optimizer is likely to try to scan the table. If I have a table with the day status in there per site, I will join the sales table to the day_status table on site_id and date and then apply my filters to the day_status table. This will give a direct, one-to-one match on the index and the query will work as expected. A great book that helps explain how to tune queries is SQL Server Query Performance Tuning Distilled -- http://www.amazon.com/Server-Performance-Tuning-Distilled-Second/dp/1590594215/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1214584001&sr=8-1

Cheers,
John

# re: forced index using WITH INDEX 7/3/2008 1:43 PM Baskar
In this scenerio, we are sure the query will be based on date alone and that is reason why we specify forced index here.

But curious to know, why the index is not applied automatically when we use pass variable to the where clause that involves date field. Let me know if you have any clue.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: