Wednesday, June 24, 2009

How to Improve slow running query performance using an Index ?

I recently come across the situation where my query was running very slow. I written a perfect query with good inner join and with proper condition. Have a look at query and its executing plan.

Query Execution Plan Before indexing

As you can see in the image query takes 1.28 minute to execute successfully. And as you can see in the figure , cause for the problem is Clustered Index Scan on LotDetails Table.

See the image below to get the clear idea about Clustered Index Scan Cost in this query.

Query Execution plan Analysis

What I did was Created Non-clustered index on “SlipID” and added “Active” column into Included Columns.

Create Index On Table

Include columns in index


After doing this when I run my query it took 0 second to execute successfully. And Clustered index scan is now turned into Index seek which is 3% of the total execution cost.

Query Execution Plan After indexing

So use index as and when required to boost the performance of your T-SQL Query.

Please correct me if I am wrong anywhere. :)

No comments: