You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/relational-databases/indexes/create-filtered-indexes.md
+23-18Lines changed: 23 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
-
title: "Create filtered indexes"
2
+
title: "Create Filtered Indexes"
3
3
description: A filtered index is an optimized disk-based rowstore nonclustered index especially suited to cover queries that select from a well-defined subset of data.
This article describes how to create a filtered index using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] (SSMS) or [!INCLUDE[tsql](../../includes/tsql-md.md)]. A filtered index is an optimized disk-based rowstore nonclustered index especially suited to cover queries that select from a well-defined subset of data. It uses a filter predicate to index a portion of rows in the table. A well-designed filtered index can improve query performance and reduce index maintenance and storage costs compared with full-table indexes.
25
+
This article describes how to create a filtered index using [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] (SSMS) or [!INCLUDE [tsql](../../includes/tsql-md.md)]. A filtered index is an optimized disk-based rowstore nonclustered index especially suited to cover queries that select from a well-defined subset of data. It uses a filter predicate to index a portion of rows in the table. A well-designed filtered index can improve query performance and reduce index maintenance and storage costs compared with full-table indexes.
27
26
28
27
Filtered indexes can provide the following advantages over full-table indexes:
29
28
30
29
1. Improved query performance and plan quality.
31
30
32
-
A well-designed filtered index improves query performance and execution plan quality because it is smaller than a full-table nonclustered index and has filtered statistics. The filtered statistics are more accurate than full-table statistics because they cover only the rows in the filtered index.
31
+
A well-designed filtered index improves query performance and execution plan quality because it's smaller than a full-table nonclustered index and has filtered statistics. The filtered statistics are more accurate than full-table statistics because they cover only the rows in the filtered index.
33
32
34
33
1. Reduced index maintenance costs.
35
34
36
-
An index is maintained only when data manipulation language (DML) statements affect the data in the index. A filtered index reduces index maintenance costs compared with a full-table nonclustered index because it is smaller and is only maintained when the data in the index is changed. It is possible to have a large number of filtered indexes, especially when they contain data that is changed infrequently. Similarly, if a filtered index contains only the frequently modified data, the smaller size of the index reduces the cost of updating the statistics.
35
+
An index is maintained only when data manipulation language (DML) statements affect the data in the index. A filtered index reduces index maintenance costs compared with a full-table nonclustered index because it's smaller and is only maintained when the data in the index is changed. It's possible to have a large number of filtered indexes, especially when they contain data that is changed infrequently. Similarly, if a filtered index contains only the frequently modified data, the smaller size of the index reduces the cost of updating the statistics.
37
36
38
37
1. Reduced index storage costs.
39
38
40
39
Creating a filtered index can reduce disk storage for nonclustered indexes when a full-table index isn't necessary. You can replace a full-table nonclustered index with multiple filtered indexes without significantly increasing the storage requirements.
41
40
42
-
## <aid="Design"></a> Design considerations
41
+
<aid="Design"></a>
42
+
43
+
## Design considerations
43
44
44
-
When a column only has a few relevant values for queries, you can create a filtered index on the subset of values. The resulting index will be smaller and cost less to maintain than a full-table nonclustered index defined on the same key columns.
45
+
When a column only has a few relevant values for queries, you can create a filtered index on the subset of values. The resulting index will be smaller and cost less to maintain than a full-table nonclustered index defined on the same key columns.
45
46
46
47
For example, consider a filtered index in the following data scenarios. In each case, the `WHERE` clause of the query should be a subset of the `WHERE` clause of the filtered index, to benefit from the filtered index.
47
48
48
-
- When the values in a column are mostly NULL and the query selects only from the non-NULL values. You can create a filtered index for the non-NULL data rows.
49
+
- When the values in a column are mostly `NULL` and the query selects only from the non-NULL values. You can create a filtered index for the non-NULL data rows.
49
50
- When rows in a table are marked as processed by a recurring workflow or queue process. Over time, most rows in the table will be marked as processed. A filtered index on rows that aren't yet processed would benefit the recurring query that looks for rows that aren't yet processed.
50
51
- When a table has heterogeneous data rows. You can create a filtered index for one or more categories of data. This can improve the performance of queries on these data rows by narrowing the focus of a query to a specific area of the table. Again, the resulting index will be smaller and cost less to maintain than a full-table nonclustered index.
51
52
52
-
## <aid="Restrictions"></a> Limitations and restrictions
53
+
<aid="Restrictions"></a>
54
+
55
+
## Limitations
53
56
54
57
- You can't create a filtered index on a view. However, the query optimizer can benefit from a filtered index defined on a table that is referenced in a view. The query optimizer considers a filtered index for a query that selects from a view if the query results will be correct.
55
58
@@ -87,7 +90,9 @@ For example, consider a filtered index in the following data scenarios. In each
87
90
88
91
Requires ALTER permission on the table or view. The user must be a member of the **sysadmin** fixed server role or the **db_ddladmin** and **db_owner** fixed database roles. To modify the filtered index expression, use `CREATE INDEX WITH DROP_EXISTING`.
89
92
90
-
## <aid="SSMSProcedure"></a> Create a filtered index with SSMS
93
+
<aid="SSMSProcedure"></a>
94
+
95
+
## Create a filtered index with SSMS
91
96
92
97
1. In Object Explorer, select the plus sign to expand the database that contains the table on which you want to create a filtered index.
93
98
@@ -109,11 +114,13 @@ Requires ALTER permission on the table or view. The user must be a member of the
109
114
110
115
1. Select **OK**.
111
116
112
-
## <aid="TsqlProcedure"></a> Create a filtered index with Transact-SQL
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
123
+
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
117
124
118
125
1. On the Standard bar, select **New Query**.
119
126
@@ -133,7 +140,7 @@ CREATE NONCLUSTERED INDEX FIBillOfMaterialsWithEndDate
133
140
GO
134
141
```
135
142
136
-
The filtered index `FIBillOfMaterialsWithEndDate` is valid for the following query. [You can display the query execution plan](../performance/display-an-actual-execution-plan.md) to determine if the query optimizer used the filtered index.
143
+
The filtered index `FIBillOfMaterialsWithEndDate` is valid for the following query. [Display an actual execution plan](../performance/display-an-actual-execution-plan.md) to determine if the query optimizer used the filtered index.
137
144
138
145
```sql
139
146
USE AdventureWorks2022;
@@ -147,11 +154,9 @@ WHERE EndDate IS NOT NULL
147
154
GO
148
155
```
149
156
150
-
## Next steps
151
-
152
-
To learn more about creating indexes and related concepts, see the following articles:
157
+
## Related content
153
158
154
159
-[CREATE INDEX (Transact-SQL)](../../t-sql/statements/create-index-transact-sql.md)
155
160
-[SQL Server and Azure SQL index architecture and design guide](../sql-server-index-design-guide.md)
156
161
-[Display an actual execution plan](../performance/display-an-actual-execution-plan.md)
0 commit comments