SQL Server Filtered Indexes

Hi Friends,

Today, I just want to explain about a new type of Index, which introduce in SQL Server 2008 (SQL Server Filtered Indexes).

What is Filtered Index?

Filtered Index is a special type of nonclustered index, which is having a filter criteria. Filtered Index can improve query performance and plan quality. Filtered index can also Reduce index maintenance cost as well as index storage cost.

Now the question arises that when we should use Filtered Indexes?Now we can understand this with a very good example. suppose, i have a table xtInfo in the database GEEKS and this table contain three columns id, name, citywhere city may contain Null values.

USE [GEEKS]
GO
CREATE TABLE [dbo].[xtInfo](
    [id] [int],
    [name] [varchar](50) NOT NULL,
    [city] [varchar](50) NULL
) ON [PRIMARY]
GO

here xtinfo table contain total 9999 rows. where city column contains NOT NULL values for 2894 rows. I know here that i will use this table to select only those rows for which city column contain values.Now i want to create indexes on this table as shown below:

create nonclustered index IX_xtinfo_id_name_city on xtinfo(id,name,city)
GO

Now I want to run the query:

set statistics io on
set statistics time on
select id, name from xtinfo where name='sanjay' and city IS NOT NULL
set statistics io off
set statistics time off

now the statistics output of this query is shown below:

1_Filtered_Indexes

   

now i am creating a new index, which is filtered index:

create nonclustered index IX_xtinfo_id_name_city on xtinfo(id,name,city)
where city IS NOT NULL
WITH (Drop_Existing=ON)

now, i again run the query

set statistics io on
set statistics time on
select id, name from xtinfo where name='sanjay' and city IS NOT NULL
set statistics io off
set statistics time off

now the statistics output of this query is shown below:

2_Filtered_Indexes

With the help of above statistics, it is clear that filtered index provide improvement in performance. Here for only demo purpose i take less ammount of data in my table but if you have large amount of such type data then filtered index  provide you the great improvement.

 

Regards

Prince Rastogi

Like us on FaceBook Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook

Follow me on TwitterFollow me on FaceBook

   

About Prince Rastogi

Prince Rastogi is working as Database Administrator at Elephant Insurance, Richmond. He is having more than 8 years of experience and worked in ERP Domain, Wealth Management Domain. Currently he is working in Insurance domain. In the starting of his career he was working on SQL Server, Internet Information Server and Visual Source Safe. He is post graduate in Computer Science. Prince is ITIL certified professional. Prince likes to explore technical things for Database World and Writing Blogs. He is Technical Editor and Blogger at SQLServerGeeks.com. He is a regular speaker at DataPlatformDay events in Delhi NCR. He has also presented some in depth sessions about SQL Server in SQL Server Conferences in Bangalore.

View all posts by Prince Rastogi →

2 Comments on “SQL Server Filtered Indexes”

  1. I have tried to use filtered indexes, but often I find that SQL Server prefers not to use the index.

    Have you seen the same thing?

  2. Hi Henrik,

    Can you give me the more details about your problem?

    Regards:

    Prince Kumar Rastogi

Leave a Reply

Your email address will not be published.