Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 59bd373

Browse files
authored
Merge pull request #20917 from LitKnd/index-secret-columns
Improve documentation of row locators, make index design guide more inclusive of Azure SQL
2 parents 60848d0 + f1bfb8d commit 59bd373

2 files changed

Lines changed: 311 additions & 270 deletions

File tree

Lines changed: 76 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
description: "Create Filtered Indexes"
3-
title: "Create Filtered Indexes | Microsoft Docs"
2+
description: "Create filtered indexes"
3+
title: "Create filtered indexes"
44
ms.custom: ""
5-
ms.date: "06/02/2016"
5+
ms.date: "01/15/2022"
66
ms.prod: sql
77
ms.prod_service: "table-view-index, sql-database"
88
ms.reviewer: ""
@@ -19,50 +19,32 @@ author: MikeRayMSFT
1919
ms.author: mikeray
2020
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
2121
---
22-
# Create Filtered Indexes
23-
[!INCLUDE [SQL Server Azure SQL Database](../../includes/applies-to-version/sql-asdb.md)]
22+
# Create filtered indexes
23+
[!INCLUDE[SQL Server Azure SQL Database Synapse Analytics PDW ](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
2424

25-
This topic describes how to create a filtered index in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. A filtered index is an optimized 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 as well as reduce index maintenance and storage costs compared with full-table indexes.
26-
27-
Filtered indexes can provide the following advantages over full-table indexes:
28-
29-
- **Improved query performance and plan quality**
30-
31-
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.
32-
33-
- **Reduced index maintenance costs**
34-
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 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.
36-
37-
- **Reduced index storage costs**
38-
39-
Creating a filtered index can reduce disk storage for nonclustered indexes when a full-table index is not necessary. You can replace a full-table nonclustered index with multiple filtered indexes without significantly increasing the storage requirements.
40-
41-
**In This Topic**
42-
43-
- **Before you begin:**
44-
45-
[Design Considerations](#Design)
46-
47-
[Limitations and Restrictions](#Restrictions)
48-
49-
[Security](#Security)
50-
51-
- **To create a filtered index, using:**
52-
53-
[SQL Server Management Studio](#SSMSProcedure)
54-
55-
[Transact-SQL](#TsqlProcedure)
25+
26+
This topic 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 as well as reduce index maintenance and storage costs compared with full-table indexes.
5627

57-
## <a name="BeforeYouBegin"></a> Before You Begin
28+
Filtered indexes can provide the following advantages over full-table indexes:
5829

59-
### <a name="Design"></a> Design Considerations
30+
1. Improved query performance and plan quality.
31+
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.
33+
34+
1. Reduced index maintenance costs.
35+
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.
37+
38+
1. Reduced index storage costs.
39+
40+
Creating a filtered index can reduce disk storage for nonclustered indexes when a full-table index is not necessary. You can replace a full-table nonclustered index with multiple filtered indexes without significantly increasing the storage requirements.
41+
## <a name="Design"></a> Design considerations
6042

61-
- When a column only has a small number of relevant values for queries, you can create a filtered index on the subset of values. For example, 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. The resulting index will be smaller and cost less to maintain than a full-table nonclustered index defined on the same key columns.
43+
When a column only has a small number of relevant values for queries, you can create a filtered index on the subset of values. For example, 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. The resulting index will be smaller and cost less to maintain than a full-table nonclustered index defined on the same key columns.
6244

63-
- 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.
45+
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.
6446

65-
### <a name="Restrictions"></a> Limitations and Restrictions
47+
## <a name="Restrictions"></a> Limitations and restrictions
6648

6749
- You cannot 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.
6850

@@ -86,102 +68,79 @@ monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||
8668

8769
- A column in the filtered index expression should be a key or included column in the filtered index definition if the column is in the query result set.
8870

89-
- The clustered index key of the table does not need to be a key or included column in the filtered index definition. The clustered index key is automatically included in all nonclustered indexes, including filtered indexes.
71+
- The clustered index key of the table does not need to be a key or included column in the filtered index definition. The clustered index key is automatically included in all nonclustered indexes, including filtered indexes. Learn more in the [index architecture and design guide](../sql-server-index-design-guide.md#nonclustered-index-architecture).
9072

9173
- If the comparison operator specified in the filtered index expression of the filtered index results in an implicit or explicit data conversion, an error will occur if the conversion occurs on the left side of a comparison operator. A solution is to write the filtered index expression with the data conversion operator (CAST or CONVERT) on the right side of the comparison operator.
9274

9375
- Review the required SET options for filtered index creation in [CREATE INDEX &#40;Transact-SQL&#41;](../../t-sql/statements/create-index-transact-sql.md) syntax
9476

95-
### <a name="Security"></a> Security
96-
97-
#### <a name="Permissions"></a> Permissions
98-
Requires ALTER permission on the table or view. 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.
99-
100-
## <a name="SSMSProcedure"></a> Using SQL Server Management Studio
101-
102-
#### To create a filtered index
77+
## Permissions
78+
79+
Requires ALTER permission on the table or view. 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.
80+
## <a name="SSMSProcedure"></a> Create a filtered index with SSMS
10381

104-
1. In Object Explorer, click the plus sign to expand the database that contains the table on which you want to create a filtered index.
82+
1. In Object Explorer, click the plus sign to expand the database that contains the table on which you want to create a filtered index.
10583

106-
2. Click the plus sign to expand the **Tables** folder.
84+
1. Click the plus sign to expand the **Tables** folder.
10785

108-
3. Click the plus sign to expand the table on which you want to create a filtered index.
86+
1. Click the plus sign to expand the table on which you want to create a filtered index.
10987

110-
4. Right-click the **Indexes** folder, point to **New Index**, and select **Non-Clustered Index...**.
88+
1. Right-click the **Indexes** folder, point to **New Index**, and select **Non-Clustered Index...**.
11189

112-
5. In the **New Index** dialog box, on the **General** page, enter the name of the new index in the **Index name** box.
90+
1. In the **New Index** dialog box, on the **General** page, enter the name of the new index in the **Index name** box.
11391

114-
6. Under **Index key columns**, click **Add...**.
92+
1. Under **Index key columns**, click **Add...**.
11593

116-
7. In the **Select Columns from**_table\_name_ dialog box, select the check box or check boxes of the table column or columns to be added to the unique index.
94+
1. In the **Select Columns from**_table\_name_ dialog box, select the check box or check boxes of the table column or columns to be added to the unique index.
11795

118-
8. Click **OK**.
96+
1. Click **OK**.
11997

120-
9. On the **Filter** page, under **Filter Expression**, enter SQL expression that you'll use to create the filtered index.
98+
1. On the **Filter** page, under **Filter Expression**, enter SQL expression that you'll use to create the filtered index.
12199

122-
10. Click **OK**.
100+
1. Click **OK**.
123101

124-
## <a name="TsqlProcedure"></a> Using Transact-SQL
125-
126-
#### To create a filtered index
127-
128-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
129-
130-
2. On the Standard bar, click **New Query**.
131-
132-
3. Copy and paste the following example into the query window and click **Execute**.
133-
134-
```
135-
USE AdventureWorks2012;
136-
GO
137-
-- Looks for an existing filtered index named "FIBillOfMaterialsWithEndDate"
138-
-- and deletes it from the table Production.BillOfMaterials if found.
139-
IF EXISTS (SELECT name FROM sys.indexes
140-
WHERE name = N'FIBillOfMaterialsWithEndDate'
141-
AND object_id = OBJECT_ID (N'Production.BillOfMaterials'))
142-
DROP INDEX FIBillOfMaterialsWithEndDate
143-
ON Production.BillOfMaterials
144-
GO
145-
-- Creates a filtered index "FIBillOfMaterialsWithEndDate"
146-
-- on the table Production.BillOfMaterials
147-
-- using the columms ComponentID and StartDate.
102+
## <a name="TsqlProcedure"></a> Create a filtered index with Transact-SQL
148103

149-
CREATE NONCLUSTERED INDEX FIBillOfMaterialsWithEndDate
150-
ON Production.BillOfMaterials (ComponentID, StartDate)
151-
WHERE EndDate IS NOT NULL ;
152-
GO
153-
```
104+
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
154105

155-
The filtered index above is valid for the following query. You can display the query execution plan to determine if the query optimizer used the filtered index.
106+
1. On the Standard bar, click **New Query**.
156107

157-
```
158-
USE AdventureWorks2012;
159-
GO
160-
SELECT ProductAssemblyID, ComponentID, StartDate
161-
FROM Production.BillOfMaterials
162-
WHERE EndDate IS NOT NULL
163-
AND ComponentID = 5
164-
AND StartDate > '01/01/2008' ;
165-
GO
166-
```
108+
1. Copy and paste the following example into the query window and click **Execute**.
167109

168-
#### To ensure that a filtered index is used in a SQL query
169-
170-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
171-
172-
2. On the Standard bar, click **New Query**.
173-
174-
3. Copy and paste the following example into the query window and click **Execute**.
175-
176-
```
177-
USE AdventureWorks2012;
178-
GO
179-
SELECT ComponentID, StartDate FROM Production.BillOfMaterials
180-
WITH ( INDEX ( FIBillOfMaterialsWithEndDate ) )
181-
WHERE EndDate IN ('20000825', '20000908', '20000918');
182-
GO
183-
```
110+
```sql
111+
USE AdventureWorks2019;
112+
GO
113+
114+
DROP INDEX IF EXISTS FIBillOfMaterialsWithEndDate
115+
ON Production.BillOfMaterials
116+
GO
117+
118+
CREATE NONCLUSTERED INDEX FIBillOfMaterialsWithEndDate
119+
ON Production.BillOfMaterials (ComponentID, StartDate)
120+
WHERE EndDate IS NOT NULL ;
121+
GO
122+
```
184123

185-
For more information, see [CREATE INDEX &#40;Transact-SQL&#41;](../../t-sql/statements/create-index-transact-sql.md).
124+
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.
186125

126+
```sql
127+
USE AdventureWorks2019;
128+
GO
129+
130+
SELECT ProductAssemblyID, ComponentID, StartDate
131+
FROM Production.BillOfMaterials
132+
WHERE EndDate IS NOT NULL
133+
AND ComponentID = 5
134+
AND StartDate > '01/01/2008' ;
135+
GO
136+
```
137+
138+
## Next steps
187139

140+
To learn more about creating indexes and related topics, see the following articles:
141+
142+
- [CREATE INDEX &#40;Transact-SQL&#41;](../../t-sql/statements/create-index-transact-sql.md)
143+
- [SQL Server and Azure SQL index architecture and design guide](../sql-server-index-design-guide.md)
144+
- [Display an actual execution plan](../performance/display-an-actual-execution-plan.md)
145+
- [Index types](indexes.md)
146+

0 commit comments

Comments
 (0)