

# 将查询历史记录保留 45 天以上
<a name="querying-keeping-query-history"></a>

如果要将查询历史记录保留超过 45 天，您可以检索查询历史记录并将其保存到 Amazon S3 等数据存储中。要自动执行此过程，您可以使用 Athena 和 Amazon S3 API 操作和 CLI 命令。以下过程总结了这些步骤。

**以编程方式检索和保存查询历史记录**

1. 使用 Athena [ListQueryExecutions](https://docs.aws.amazon.com/athena/latest/APIReference/API_ListQueryExecutions.html) API 操作或 [list-query-executions](https://docs.aws.amazon.com/cli/latest/reference/athena/list-query-executions.html) CLI 命令检索查询 ID。

1. 使用 Athena [GetQueryExecution](https://docs.aws.amazon.com/athena/latest/APIReference/API_GetQueryExecution.html) API 操作或 [get-query-execution](https://docs.aws.amazon.com/cli/latest/reference/athena/get-query-execution.html) CLI 命令，根据查询的 ID 检索有关各个查询的信息。

1. 使用 Amazon S3 [PutObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html) API 操作或 [put-object](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html) CLI 命令将信息保存到 Amazon S3 中。