为索引指定填充因子Specify Fill Factor for an Index

适用于: SQL Server 2016 及更高版本 Azure SQL 数据库 Azure Synapse Analytics 并行数据仓库

本主题说明什么是填充因子以及如何使用 SQL ServerSQL ServerSQL Server Management StudioSQL Server Management StudioTransact-SQLTransact-SQL中指定索引的填充因子值。This topic describes what fill factor is and how to specify a fill factor value on an index in SQL ServerSQL Server by using SQL Server Management StudioSQL Server Management Studio or Transact-SQLTransact-SQL.

提供填充因子选项是为了优化索引数据存储和性能。The fill-factor option is provided for fine-tuning index data storage and performance. 当创建或重新生成索引时,填充因子的值可确定每个叶级页上要填充数据的空间百分比,以便在每一页上保留一些剩余空间作为以后扩展索引的可用空间。When an index is created or rebuilt, the fill-factor value determines the percentage of space on each leaf-level page to be filled with data, reserving the remainder on each page as free space for future growth. 例如,指定填充因子的值为 80 表示每个叶级页上将有 20% 的空间保留为空,以便随着向基础表中添加数据而为扩展索引提供空间。For example, specifying a fill-factor value of 80 means that 20 percent of each leaf-level page will be left empty, providing space for index expansion as data is added to the underlying table. 在索引行之间保留可用空间,而不是在索引的末尾保留。The empty space is reserved between the index rows rather than at the end of the index.

填充因子的值是 1 到 100 之间的百分比,服务器范围的默认值为 0,这表示将完全填充叶级页。The fill-factor value is a percentage from 1 to 100, and the server-wide default is 0 which means that the leaf-level pages are filled to capacity.

备注

填充因子的值 0 和 100 在所有方面都是相同的。Fill-factor values 0 and 100 are the same in all respects.

本主题内容In This Topic

开始之前Before You Begin

性能注意事项Performance Considerations

页拆分Page Splits

正确选择填充因子值可提供足够的空间,以便随着向基础表中添加数据而扩展索引,从而降低页拆分的可能性。如果向已满的索引页添加新行, 数据库引擎Database Engine 将把大约一半的行移到新页中,以便为该新行腾出空间。A correctly chosen fill-factor value can reduce potential page splits by providing enough space for index expansion as data is added to the underlying table.When a new row is added to a full index page, the 数据库引擎Database Engine moves approximately half the rows to a new page to make room for the new row. 这种重组称为页拆分。This reorganization is known as a page split. 页拆分可为新记录腾出空间,但是执行页拆分可能需要花费一定的时间,此操作会消耗大量资源。A page split makes room for new records, but can take time to perform and is a resource intensive operation. 此外,它还可能造成碎片,从而导致 I/O 操作增加。Also, it can cause fragmentation that causes increased I/O operations. 如果经常发生页拆分,可通过使用新的或现有的填充因子值来重新生成索引,从而重新分发数据。When frequent page splits occur, the index can be rebuilt by using a new or existing fill-factor value to redistribute the data. 有关详细信息,请参阅 重新组织和重新生成索引For more information, see Reorganize and Rebuild Indexes.

虽然采用较小的非零填充因子值可减少随着索引的增长而拆分页的需求,但是索引将需要更多的存储空间,并且会降低读取性能。Although a low, nonzero fill-factor value may reduce the requirement to split pages as the index grows, the index will require more storage space and can decrease read performance. 即使对于面向许多插入和更新操作的应用程序,数据库的读取次数一般也会超过数据库写入次数的 5 到 10 倍。Even for an application oriented for many insert and update operations, the number of database reads typically outnumber database writes by a factor of 5 to 10. 因此,指定一个不同于默认值的填充因子会降低数据库的读取性能,而降低量与填充因子设置的值成反比。Therefore, specifying a fill factor other than the default can decrease database read performance by an amount inversely proportional to the fill-factor setting. 例如,当填充因子的值为 50 时,数据库的读取性能会降低两倍。For example, a fill-factor value of 50 can cause database read performance to decrease by two times. 读取性能降低是因为索引包含较多的页,因此增加了检索数据所需的磁盘 I/O 操作。Read performance is decreased because the index contains more pages, therefore increasing the disk IO operations required to retrieve the data.

将数据添加到表的末尾Adding Data to the End of the Table

如果新数据在表中均匀分布,则不是 0 或 100 的非零填充因子对性能有利。A nonzero fill factor other than 0 or 100 can be good for performance if the new data is evenly distributed throughout the table. 但是,如果所有数据都添加到表的末尾,则不会填充索引页中的可用空间。However, if all the data is added to the end of the table, the empty space in the index pages will not be filled. 例如,如果索引键列是 IDENTITY 列,则新行的键将总是增加,并且索引行在逻辑意义上将添加到索引的末尾。For example, if the index key column is an IDENTITY column, the key for new rows is always increasing and the index rows are logically added to the end of the index. 如果将用加长行的大小的数据来更新现有行,则请使用小于 100 的填充因子。If existing rows will be updated with data that lengthens the size of the rows, use a fill factor of less than 100. 每页上的额外字节将有助于把行中的额外长度造成的页拆分降低到最小限度。The extra bytes on each page will help to minimize page splits caused by extra length in the rows.

SecuritySecurity

权限Permissions

要求对表或视图具有 ALTER 权限。Requires ALTER permission on the table or view. 用户必须是 sysadmin 固定服务器角色的成员,或者是 db_ddladmindb_owner 固定数据库角色的成员。User must be a member of the sysadmin fixed server role or the db_ddladmin and db_owner fixed database roles.

使用 SQL Server Management StudioUsing SQL Server Management Studio

使用表设计器指定填充因子To specify a fill factor by using Table Designer

  1. 在对象资源管理器中,单击加号以便展开包含你要指定索引填充因子的表的数据库。In Object Explorer, click the plus sign to expand the database that contains the table on which you want to specify an index's fill factor.

  2. 单击加号以便展开 “表” 文件夹。Click the plus sign to expand the Tables folder.

  3. 右键单击你要指定索引的填充因子的表,然后选择“设计”。Right-click the table on which you want to specify an index's fill factor and select Design.

  4. 在“表设计器”菜单上,单击“索引/键”。On the Table Designer menu, click Indexes/Keys.

  5. 选择您要指定填充因子的索引。Select the index with the fill factor that you want to specify.

  6. 展开 “填充规范”,选择 “填充因子” 行并在行中输入所需的填充因子。Expand Fill Specification, select the Fill Factor row and enter the fill factor you want in the row.

  7. 单击“关闭” 。Click Close.

  8. 在“文件”菜单上,选择“保存”以保存 table_nameOn the File menu, select Savetable_name.

使用对象资源管理器为索引指定填充因子To specify a fill factor in an index by using Object Explorer

  1. 在对象资源管理器中,单击加号以便展开包含你要指定索引填充因子的表的数据库。In Object Explorer, click the plus sign to expand the database that contains the table on which you want to specify an index's fill factor.

  2. 单击加号以便展开 “表” 文件夹。Click the plus sign to expand the Tables folder.

  3. 单击加号以展开要指定索引的填充因子的表。Click the plus sign to expand the table on which you want to specify an index's fill factor.

  4. 单击加号以便展开 “索引” 文件夹。Click the plus sign to expand the Indexes folder.

  5. 右键单击要指定填充因子的索引,然后选择“属性”。Right-click the index with the fill factor that you want to specify and select Properties.

  6. “选择页” 下,选择 “选项”Under Select a page, select Options.

  7. “填充因子” 行中,输入所需的填充因子。In the Fill factor row, enter the fill factor that you want.

  8. 单击“确定”。Click OK.

使用 Transact-SQLUsing Transact-SQL

在现有索引中指定填充因子To specify a fill factor in an existing index

  1. “对象资源管理器” 中,连接到 数据库引擎Database Engine的实例。In Object Explorer, connect to an instance of 数据库引擎Database Engine.

  2. 在标准菜单栏上,单击 “新建查询”On the Standard bar, click New Query.

  3. 将以下示例复制并粘贴到查询窗口中,然后单击“执行” 。Copy and paste the following example into the query window and click Execute. 该示例重新生成现有索引,并在重新生成操作过程中应用指定的填充因子。The example rebuilds an existing index and applies the specified fill factor during the rebuild operation.

    USE AdventureWorks2012;  
    GO  
    -- Rebuilds the IX_Employee_OrganizationLevel_OrganizationNode index   
    -- with a fill factor of 80 on the HumanResources.Employee table.  
    
    ALTER INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee  
    REBUILD WITH (FILLFACTOR = 80);   
    GO  
    

为索引指定填充因子的其他方法Another way to specify a fill factor in an index

  1. “对象资源管理器” 中,连接到 数据库引擎Database Engine的实例。In Object Explorer, connect to an instance of 数据库引擎Database Engine.

  2. 在标准菜单栏上,单击 “新建查询”On the Standard bar, click New Query.

  3. 将以下示例复制并粘贴到查询窗口中,然后单击“执行” 。Copy and paste the following example into the query window and click Execute.

    USE AdventureWorks2012;  
    GO  
    /* Drops and re-creates the IX_Employee_OrganizationLevel_OrganizationNode index on the HumanResources.Employee table with a fill factor of 80.  
    */  
    
    CREATE INDEX IX_Employee_OrganizationLevel_OrganizationNode ON HumanResources.Employee  
       (OrganizationLevel, OrganizationNode)   
    WITH (DROP_EXISTING = ON, FILLFACTOR = 80);   
    GO  
    

有关详细信息,请参阅 ALTER INDEX (Transact-SQL)For more information, see ALTER INDEX (Transact-SQL).