Filtered Indexes in SQL 2008
What are filtered indexes? Indexes with a WHERE clause!
When to Use Filtered Indexes? Filtered indexes are useful when columns contain well-defined subsets of data that queries reference in SELECT statements. Examples are: Sparse columns that contain only a few non-NULL values. Heterogeneous columns that contain categories of data. Columns that contain ranges of values such as dollar amounts, time, and dates. Table partitions that are defined by simple comparison logic for column values.
Reduced maintenance costs for filtered indexes are most noticeable when the number of rows in the index is small compared with a full-table index. If the filtered index includes most of the rows in the table, it could cost more to maintain than a full-table index. In this case, you should use a full-table index instead of a filtered index.
How to use filtered indexes Just add a WHERE clause CREATE INDEX filteredindex ON TABLE WHERE ...
Indexed views? This feature actually existed in FoxPro, over a decade ago!  It also existed in another form in MS SQL server, the indexed view. Filtered indexes are defined on one table and only support simple comparison operators. If you need a filter expression that references multiple tables or has complex logic, you should create a view.
Indexed views are a complicated, and somewhat expensive, but more powerful than the filtered index.  You can do everything in a filtered index with an indexed view.  Of course, indexed view matching is only supported in the Enterprise edition of the software, so perhaps not everyone has seen those benefits. 
Allowed in expressions  Views  Filtered indexes  Computed columns Yes No Joins Yes No Multiple tables Yes No Simple comparison logic in a predicate* Yes Yes Complex logic in a predicate** Yes No
Why use filtered indexes? Disk space can be scare and filtered indexes helped us squeeze the most out of it.  Example : I've been using is a manufacturing production system that contains millions of work orders but only 2K are currently open work orders. A filtered index where status = 'open' would save a lot of space.
When inserting new data  to a table, the index cost will only be required if the INSERT matches the criteria for the index, increasing speed for OLTP with large numbers of inserts that may not require an index. Performance can be improved for frequent queries on a subset of data within a large table.  For example, Suppose that there are frequent queries for Accessories which have subcategories 27-36 in your products table. You can improve the performance of queries for Accessories by creating a filtered index on the Accessories subcategories, 27-36. It is a best practice to include a small number of key or included columns in a filtered index definition, and to incorporate only the columns that are necessary for the query optimizer to choose the filtered index for the query execution plan. The query optimizer can choose a filtered index for the query regardless of whether it does or does not cover the query. However, the query optimizer is more likely to choose a filtered index if it covers the query.
The big one! Unique with NULL If you need to constrain a column to a only allow unique values but also allow the column to allow nulls – this one is very important and happens all the time! create unique index production.nullidx  on production.product(code)   where production.product(code) is not null 

Filtered Indexes In Sql 2008

  • 1.
  • 2.
    What are filteredindexes? Indexes with a WHERE clause!
  • 3.
    When to UseFiltered Indexes? Filtered indexes are useful when columns contain well-defined subsets of data that queries reference in SELECT statements. Examples are: Sparse columns that contain only a few non-NULL values. Heterogeneous columns that contain categories of data. Columns that contain ranges of values such as dollar amounts, time, and dates. Table partitions that are defined by simple comparison logic for column values.
  • 4.
    Reduced maintenance costsfor filtered indexes are most noticeable when the number of rows in the index is small compared with a full-table index. If the filtered index includes most of the rows in the table, it could cost more to maintain than a full-table index. In this case, you should use a full-table index instead of a filtered index.
  • 5.
    How to usefiltered indexes Just add a WHERE clause CREATE INDEX filteredindex ON TABLE WHERE ...
  • 6.
    Indexed views? Thisfeature actually existed in FoxPro, over a decade ago! It also existed in another form in MS SQL server, the indexed view. Filtered indexes are defined on one table and only support simple comparison operators. If you need a filter expression that references multiple tables or has complex logic, you should create a view.
  • 7.
    Indexed views area complicated, and somewhat expensive, but more powerful than the filtered index. You can do everything in a filtered index with an indexed view.  Of course, indexed view matching is only supported in the Enterprise edition of the software, so perhaps not everyone has seen those benefits. 
  • 8.
    Allowed in expressions Views Filtered indexes Computed columns Yes No Joins Yes No Multiple tables Yes No Simple comparison logic in a predicate* Yes Yes Complex logic in a predicate** Yes No
  • 9.
    Why use filteredindexes? Disk space can be scare and filtered indexes helped us squeeze the most out of it. Example : I've been using is a manufacturing production system that contains millions of work orders but only 2K are currently open work orders. A filtered index where status = 'open' would save a lot of space.
  • 10.
    When inserting newdata to a table, the index cost will only be required if the INSERT matches the criteria for the index, increasing speed for OLTP with large numbers of inserts that may not require an index. Performance can be improved for frequent queries on a subset of data within a large table. For example, Suppose that there are frequent queries for Accessories which have subcategories 27-36 in your products table. You can improve the performance of queries for Accessories by creating a filtered index on the Accessories subcategories, 27-36. It is a best practice to include a small number of key or included columns in a filtered index definition, and to incorporate only the columns that are necessary for the query optimizer to choose the filtered index for the query execution plan. The query optimizer can choose a filtered index for the query regardless of whether it does or does not cover the query. However, the query optimizer is more likely to choose a filtered index if it covers the query.
  • 11.
    The big one!Unique with NULL If you need to constrain a column to a only allow unique values but also allow the column to allow nulls – this one is very important and happens all the time! create unique index production.nullidx on production.product(code)  where production.product(code) is not null