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:
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:
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 Twitter | Follow me on FaceBook
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?
Hi Henrik,
Can you give me the more details about your problem?
Regards:
Prince Kumar Rastogi