

# 使用 AWS Glue 分区索引和筛选来优化查询
<a name="glue-best-practices-partition-index"></a>

在 Athena 查询分区表时，其会检索可用的表分区并筛选出与查询相关的子集。随着新数据和分区的添加，处理分区需要更多时间，查询运行时间可能会增加。如果您的表包含随着时间的推移而增长的大量分区，请考虑使用 AWS Glue 分区索引和筛选。使用分区索引，Athena 可以优化分区处理并提升高度分区表的查询性能。在表的属性中设置分区筛选包括两个步骤：

1. 在 AWS Glue 中创建分区索引。

1. 为表启用分区筛选。

## 创建分区索引
<a name="glue-best-practices-partition-index-creating"></a>

有关在 AWS Glue 中创建分区索引的步骤，请参阅《AWS Glue 开发人员指南》中的[使用分区索引](https://docs.aws.amazon.com/glue/latest/dg/partition-indexes.html)。有关 AWS Glue 中对分区索引的限制，请参阅该页面上的[关于分区索引](https://docs.aws.amazon.com/glue/latest/dg/partition-indexes.html#partition-index-1)部分。

## 启用分区筛选
<a name="glue-best-practices-partition-filtering-enabling"></a>

要为表启用分区筛选，必须在 AWS Glue 中设置新的表属性。有关如何在 AWS Glue 中设置表属性的步骤，请参阅[设置分区投影](https://docs.aws.amazon.com/athena/latest/ug/partition-projection-setting-up.html)页面。当您在 AWS Glue 中编辑表详细信息时，将以下键值对添加到 **Table properties**（表属性）部分：
+ 对于 **Key**（键），请添加 `partition_filtering.enabled`
+ 对于 **Value**（值），请添加 `true`

您可以随时通过将 `partition_filtering.enabled` 值设置为 `false` 来对此表禁用分区筛选。

完成上述步骤后，您可以返回 Athena 控制台查询数据。

有关使用分区索引和筛选的更多信息，请参阅 *AWS 大数据博客*中的[使用 AWS Glue Data Catalog 分区索引提高 Amazon Athena 查询性能](https://aws.amazon.com/blogs/big-data/improve-amazon-athena-query-performance-using-aws-glue-data-catalog-partition-indexes/)。