using an index

I have created an index on a table, but when I see the execution plan, it is not using this index

How can force the query to use this index

Regards

July 17th, 2013 6:01am

Forcing the sql to use an index is not a good option. Did you recompile the stored proc. Can you share the query, table structure , indexes and
Free Windows Admin Tool Kit Click here and download it now
July 17th, 2013 6:03am

Hi satheesh

How to recompile the stored proc. I have a query which is taking around 35 sec, so I created a non clustered index but the query is not using it

Regards

July 17th, 2013 6:06am

read about sp_recompile here

sp_recompil

July 17th, 2013 6:07am

The optimizer is very efficient in choosing the best plan. By following some guidelines we could help optimizer choose the best plan. However forcing the optimizer to use an index is a bad idea(use it as last option)

With out looking at the code, execution plan and table structure etc no-one could explain why the query is not choosing appropriate indexes. So please share the code( at least  some sample query) 

July 17th, 2013 6:14am

Hi sateesh,

I am little confused, as I told my query was taking 35 seconds, after creating the index on columns in group by clause,it was using this index  and time was reduced by 10 seconds. then I droped the index to cross check.

But now when I created again this index, it is not using this index as it did earlier

please suggest

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2013 2:15pm

You need to provide table definitions, index definitions, the query used etc. so that this will help to analyse this further without guessing..or a generic answer.
July 17th, 2013 2:27pm

Did you recompile the stored proc after creating the index?
Free Windows Admin Tool Kit Click here and download it now
July 17th, 2013 2:30pm

As the table definition, index definition and query are big enough, it is difficult to post them all.

Since i created and droped this index multiple times, may be it is fragmented,

how can check whether this particular index is fragmented?

Regards

July 17th, 2013 2:32pm

You can find the fragmentation using the following query..

SELECT 
OBJECT_NAME(OBJECT_ID), index_id,index_type_desc,index_level,
avg_fragmentation_in_percent,page_count
FROM sys.dm_db_index_physical_stats (DB_ID(N'yourDBName'), NULL, NULL, NULL , 'SAMPLED')
ORDER BY avg_fragmentation_in_percent DESC

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2013 2:41pm

An index fragmentation will not make the index unusable by optimizer, I believe. 

Here is the query to use fragmentation 

SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName, 
ind.name AS IndexName, indexstats.index_type_desc AS IndexType, 
indexstats.avg_fragmentation_in_percent 
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats 
INNER JOIN sys.indexes ind  
ON ind.object_id = indexstats.object_id 
AND ind.index_id = indexstats.index_id 
WHERE indexstats.avg_fragmentation_in_percent > 30 
ORDER BY indexstats.avg_fragmentation_in_percent DESC

July 17th, 2013 3:01pm

Check Fragmentation using,

DBCC SHOWCONTIG

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2013 5:15pm

Update statistics also.
July 17th, 2013 6:20pm

The following article is on optimization:

http://www.sqlusa.com/articles/query-optimization/

For additional assistance you need to post data/index DDL.

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2013 8:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics