

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# CTAS 查詢的考量事項與限制
<a name="ctas-considerations-limitations"></a>

下列各節會描述在 Athena 中使用 `CREATE TABLE AS SELECT` (CTAS) 查詢時要記住的注意事項和限制。

## 了解 CTAS 查詢語法
<a name="ctas-considerations-limitations-query-syntax"></a>

CTAS 查詢語法與用於建立資料表的 `CREATE [EXTERNAL] TABLE` 語法不同。請參閱 [CREATE TABLE AS](create-table-as.md)。

## 檢視與 CTAS 查詢之間的差異
<a name="ctas-considerations-limitations-queries-vs-views"></a>

CTAS 查詢會將新資料寫入到 Amazon S3 中的指定位置。檢視不會寫入任何資料。

## 指定 CTAS 查詢結果的位置
<a name="ctas-considerations-limitations-location-of-query-results"></a>

如果您的工作群組為查詢結果位置[覆寫用戶端設定](workgroups-settings-override.md)，則 Athena 會在位置 `s3://amzn-s3-demo-bucket/tables/<query-id>/` 中建立資料表。若要查看為工作群組指定的查詢結果位置，請[檢視工作群組的詳細資訊](viewing-details-workgroups.md)。

如果您的工作群組未覆寫查詢結果位置，您可以在 CTAS 查詢中使用語法 `WITH (external_location ='s3://amzn-s3-demo-bucket/')` 來指定 CTAS 查詢結果的儲存位置。

**注意**  
`external_location` 屬性必須指定空白的位置。CTAS 查詢會檢查儲存貯體中的路徑位置 (前綴) 是空的，並且如果該位置中已有資料，則不會加以覆寫。若要再次使用相同的位置，請刪除儲存貯體的金鑰前綴位置中的資料。

如果您省略 `external_location` 語法而且未使用工作群組設定，則 Athena 會使用查詢結果位置的[用戶端設定](query-results-specify-location-console.md)，並在位置 `s3://amzn-s3-demo-bucket/<Unsaved-or-query-name>/<year>/<month/<date>/tables/<query-id>/` 中建立資料表。

## 尋找孤立檔案
<a name="ctas-considerations-limitations-locating-orphaned-files"></a>

如果 `CTAS` 或 `INSERT INTO` 陳述式失敗，則失敗或取消的查詢所在的目標資料位置可能遺留孤立的資料檔案。由於 Athena 在某些案例中不會從查詢的目標儲存貯體中刪除資料，因此後續查詢中可能會包含部分資料。

若要尋找孤立檔案以進行檢測或刪除，您可以使用 Athena 提供的資料資訊清單檔案來追蹤要寫入的檔案清單。在某些極少數情況下，Athena 查詢會突然失敗，此時可能不存在資訊清單檔案。您可以手動檢查目標 S3 位置，以尋找孤立的檔案。如需詳細資訊，請參閱[識別查詢輸出檔案](querying-finding-output-files.md#querying-identifying-output-files)和 [DataManifestLocation](https://docs.aws.amazon.com/athena/latest/APIReference/API_QueryExecutionStatistics.html#athena-Type-QueryExecutionStatistics-DataManifestLocation)。

我們強烈建議使用 Apache Iceberg 來實現資料表的原子交易。如需詳細資訊，請參閱[查詢 Apache Iceberg 資料表](querying-iceberg.md)。

## 請記住，會忽略 ORDER BY 子句
<a name="ctas-considerations-limitations-order-by-ignored"></a>

在 CTAS 查詢中，Athena 會忽略查詢 `SELECT` 部分中的 `ORDER BY` 子句。

根據 SQL 規格 (ISO 9075 第 2 部分)，只有立即包含 `ORDER BY` 子句的查詢表達式，才能保證查詢表達式所指定之資料表資料列的順序。在任何情況下，SQL 中的資料表本身是無順序的，並且在子查詢子句中實作 `ORDER BY` 都會導致查詢執行不彰，並且將會導致無序輸出。因此，在 Athena CTAS 查詢中，無法保證在寫入資料時會保留 `ORDER BY` 子句指定的順序。

## 選擇存放查詢結果的格式
<a name="ctas-considerations-limitations-formats-for-query-results"></a>

您可以將 CTAS 結果存放在 `PARQUET`、`ORC`、`AVRO`、`JSON` 和 `TEXTFILE` 中。CTAS `TEXTFILE` 格式不支援多字元分隔符號。如果您不指定資料儲存格式，則依預設，CTAS 查詢的結果會存放在 Parquet 中。

CTAS 查詢不要求指定 SerDe 來解譯格式轉換。請參閱 [Example: Writing query results to a different format](ctas-examples.md#ctas-example-format)。

## 考慮壓縮格式
<a name="ctas-considerations-limitations-compression-formats"></a>

`GZIP` 壓縮用於 JSON 和 TEXTFILE 格式的 CTAS 查詢結果。對於 Parquet，您可以使用 `GZIP` 或 `SNAPPY`，預設值為 `GZIP`。對於 ORC，您可以使用 `LZ4`、`SNAPPY`、`ZLIB` 或 `ZSTD`，預設值為 `ZLIB`。如需指定壓縮的 CTAS 範例，請參閱 [Example: Specifying data storage and compression formats](ctas-examples.md#ctas-example-compression)。如需有關 Athena 中壓縮的資訊，請參閱 [在 Athena 中使用壓縮](compression-formats.md)。

## 分割和歸納結果
<a name="ctas-considerations-limitations-partition-and-bucket-limits"></a>

您可以分割和歸納 CTAS 查詢的結果資料。若要指定目標資料表的屬性，請在 `WITH` 子句的結尾包含分割和歸納述詞。如需詳細資訊，請參閱[使用分割和歸納](ctas-partitioning-and-bucketing.md)及[Example: Creating bucketed and partitioned tables](ctas-examples.md#ctas-example-bucketed)。

當您使用 CTAS 建立分割的資料表時，Athena 的寫入限制為 100 個分割區。如需解決 100 個分割區限制的相關資訊，請參閱 [使用 CTAS 和 INSERT INTO 來解決 100 個分割區限制](ctas-insert-into.md)。

## 加密結果
<a name="ctas-considerations-limitations-encryption"></a>

您可以在 Simple Storage Service (Amazon S3) 中加密 CTAS 查詢結果，類似於您在 Athena 中加密其他查詢結果的方式。如需詳細資訊，請參閱[加密 Amazon S3 中存放的 Athena 查詢結果](encrypting-query-results-stored-in-s3.md)。

## 預期的儲存貯體擁有者設定不適用於 CTAS
<a name="ctas-considerations-limitations-expected-bucket-owner"></a>

對於 CTAS 陳述式，預期的儲存貯體擁有者設定不適用於 Simple Storage Service (Amazon S3) 中的目的地資料表位置。預期的儲存貯體擁有者設定僅適用於您為 Athena 查詢結果指定的 Simple Storage Service (Amazon S3) 輸出位置。如需詳細資訊，請參閱[使用 Athena 主控台指定查詢結果位置](query-results-specify-location-console.md)。

## 保留資料欄資料類型
<a name="ctas-considerations-limitations-data-types"></a>

CTAS 查詢的資料欄資料類型與為原始查詢所指定的相同。