virtual techdays INDIA │ august 2010 Filtered Indexes – The unexplored index … Vinod Kumar M │ Microsoft India Technology Evangelist – DB and BI
Filtered Indexes What Why How Filtered Indexes vs. Indexed Views Plan Reuseability virtual techdays INDIA │ august 2010 A G E N D A
Filtered Indexes are an index on a subset of rows in a table WHERE clause in CREATE INDEX Filtered Statistics are stats over a subset of rows No auto-created filtered statistics virtual techdays INDIA │ august 2010 What is Filtered Index
Improve query performance Improve plan quality Statistics are more accurate Reduce index size Reduce index maintenance cost Large number of indexes are feasible Query optimizer will choose the right index virtual techdays INDIA │ august 2010 Why use Filtered Index
Sparse columns Most rows contain NULL Also when small subset of rows contain NULL (e.g. End Date) IsDeleted flag columns Categories of data in same table Bikes, Clothing, Accessories Sets of related properties IN clause Window Search - different types of properties Ranges of data values Dollar amounts, time ranges, date ranges SharePoint lists (AllUserData table) Table partitions virtual techdays INDIA │ august 2010 Why: Use Cases for Filtered Index
::= {Conjunct [AND Conjunct]} ::= { Disjunct | Comparison } ::= { column IN (constant, …)} ::= {column constant} ::= IS | IS NOT | = | | ! = | > | > = | ! > | < | < = | ! < } = NULL not supported (even with setting) virtual techdays INDIA │ august 2010 How: Syntax
age = 10 and category > 20 age = 10 and category > 20 and category < 50 age in (10, 20, 30) age in (10, 20) and category in (15, 25) virtual techdays INDIA │ august 2010 Examples of Predicates
Filtered indices can only be created as non-clustered indices. Filtered indexes are not allowed on views unless they are indexed views. Filtered indexes cannot be created on XML, spatial, or fulltext indexes. If a filtered index is marked Unique, it means for all the qualifying rows, the index values are unique. The filter expression cannot reference a computed column or UDT virtual techdays INDIA │ august 2010 Rules
No ALTER INDEX to alter where clause Use CREATE INDEX WITH DROP_EXISTING = ON Online operations work DTA recommends filtered indexes Missing indexes don't report filtered index virtual techdays INDIA │ august 2010 Behaviors
SET options required ANSI_NULLS - ON ANSI_PADDING - ON ANSI_WARNINGS - ON ARITHABORT - ON (set by ANSI_WARNINGS - ON) CONCAT_NULL_YIELDS_NULL - ON NUMERIC_ROUNDABORT - OFF QUOTED_IDENTIFIER - ON If not set correctly: Insert/update/delete don't work Optimizer doesn't consider plan Can’t create index virtual techdays INDIA │ august 2010 SET Options
Must match exactly Filtered index col > 500 Col - no Col and Col > yes Don't get used with autoparameterization If parameter could be outside the filter virtual techdays INDIA │ august 2010 Parameterized Queries
Filtered Stats updated based on colmodctr Filter predicate not considered Filtered Stats use same sampling algorithm Filter predicate not considered Filtered stats can become Out of date or updated too frequently Not representative of true cardinality Solution: Use scheduled stats update with FULLSCAN virtual techdays INDIA │ august 2010 Filtered Statistics And Updating
sys.indexes sys.stats sys.dependencies virtual techdays INDIA │ august 2010 Metadata
virtual techdays INDIA │ august 2010 DEMO: Filtered Index Explained and Explored !!! Vinod Kumar M
Filtered Indices work best on columns where the Filter index criteria is simple and filters out a major portion of the rows in the table. These work best in situations where columns are sparsely populated and index criteria is used to select only data that is populated with relevant values. Generally using filtered indexes are useful for Sparse columns where most of the values in the column are NULL. If the index needs to cover all rows in a table, use a regular index If not, and the filtering condition is simple (fits within the filtered index grammar set), use a filtered index. If filtering condition is complex, use an indexed view. virtual techdays INDIA │ august 2010 Best Practices
virtual techdays INDIA │ august 2010 Technology Comparison
Filtered Indexes: an alternative to indexed views Available in all SQL Server editions Only simple filtering predicates Variety of Uses Sparse columns the biggest Filtered Statistics: space saving over indexes But watch sampling and updating virtual techdays INDIA │ august 2010 Summary
virtual techdays THANKS │ august