Thursday, September 15, 2011

SQL SERVER – 2008-Filtered Index

SQL SERVER – 2008 – Introduction to Filtered Index – Improve performance with Filtered Index

Filtered Index is a new feature in SQL SERVER 2008. Filtered Index is used to index a portion of rows in a table that means it applies filter on INDEX which improves query performance, reduce index maintenance costs, and reduce index storage costs compared with full-table indexes.
When we see an Index created with some WHERE clause then that is actually a FILTERED INDEX.
For Example,
If we want to get the Employees whose Title is “Marketing Manager”, for that let’s create an INDEX on EmployeeID  whose Title is “Marketing Manager” and then write the SQL Statement to retrieve Employees who are “Marketing Manager”.
CREATE NONCLUSTERED INDEX NCI_Department
ON HumanResources.Employee(EmployeeID)
WHERE Title= 'Marketing Manager'
Points to remember when creating Filtered Index:
-          They can be created only as Nonclustered Index
-          They can be used on Views only if they are persisted views.
-          They cannot be created on full-text Indexes.
Let us write simple SELECT statement on the table where we created Filtered Index.
SELECT he.EmployeeID,he.LoginID,he.Title
FROM HumanResources.Employee he
WHERE he.Title = 'Marketing Manager'
Now we will see the Execution Plan and compare the performance before and after the Filtered Index was created on Employee table.
As we can see, in first case the index scan in 100% done on Clustered Index taking 24% of total cost of execution. Once the Filtered index was created on Employee table, the Index scan is 50% on Clustered Index and 50% on Nonclustered Index which retrieves the data faster taking 20% of total cost of execution compared to 24% on table with no index.
If we have table with thousands of records and we are only concern with very few rows in our query we should use Filtered Index.
Conclusion:
A filtered Index is an optimized non clustered Index which is one of the great performance improvements in SQL SERVER 2008 reducing the Index storage cost and reduces maintenance cost.

No comments:

Post a Comment