

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

# 使用 Aurora Serverless v1 叢集比較 Aurora Serverless v2 和佈建叢集的 Amazon RDS 資料 API 行為
<a name="data-api.differences"></a>

Amazon RDS 資料 API 的最新增強功能可讓使用最新版 PostgreSQL 或 MySQL 引擎的叢集使用資料 API。這些叢集可設定為使用 Aurora Serverless v2 或佈建執行個體類別，例如 `db.r6g` 或 `db.r6i`。

下列各節說明 Aurora Serverless v2 和佈建資料庫叢集和 Aurora Serverless v1 資料庫叢集之間的 Amazon RDS 資料 API 差異。Aurora Serverless v1資料庫叢集使用 `serverless` 引擎模式。佈建的資料庫叢集使用 `provisioned` 引擎模式。Aurora Serverless v2 資料庫叢集也會使用 `provisioned` 引擎模式，且包含一或多個具有 `db.serverless` 執行個體類別的 Aurora Serverless v2 資料庫執行個體。

## 每秒請求數上限
<a name="data-api.differences-requests"></a>

**Aurora Serverless v1**

資料 API 每秒最多可提出 1,000 個請求。

**Aurora Serverless v2**

資料 API 每秒可以提出不限數量的請求。

## 在現有資料庫啟用或停用 Amazon RDS 資料 API
<a name="data-api.differences-enable-disable"></a>

**Aurora Serverless v1**
+ **使用 Amazon RDS API**：使用 `ModifyCluster` 操作，然後指定 `EnableHttpEndpoint` 參數的 `True`或 `False` (如適用)。
+ **搭配 AWS CLI**：使用 `modify-db-cluster` 操作搭配 `--enable-http-endpoint` 或 `--no-enable-http-endpoint` 選項 (如適用)。

**Aurora Serverless v2**
+ **搭配 Amazon RDS API**：使用 `EnableHttpEndpoint` 和 `DisableHttpEndpoint` 操作。
+ **搭配 AWS CLI**：使用 `enable-http-endpoint` 和 `disable-http-endpoint` 操作。

## CloudTrail 事件
<a name="data-api.differences-ct-events"></a>

**Aurora Serverless v1**

資料 API 呼叫中的事件是管理事件。根據預設，這些事件會自動包含在線索中。如需更多詳細資訊，請參閱 [從 AWS CloudTrail 追蹤中排除資料 API 事件 (Aurora Serverless v1僅限 )](logging-using-cloudtrail-data-api.md#logging-using-cloudtrail-data-api.excluding-cloudtrail-events)。

**Aurora Serverless v2**

資料 API 呼叫中的事件是資料事件。預設會在線索中自動排除這些事件。如需更多詳細資訊，請參閱 [在 AWS CloudTrail 追蹤中包含資料 API 事件](logging-using-cloudtrail-data-api.md#logging-using-cloudtrail-data-api.including-cloudtrail-events)。

## 多陳述式支援
<a name="data-api.differences-multistatement"></a>

**Aurora Serverless v1**
+ 對於 Aurora MySQL，不支援多陳述式。
+ 對於 Aurora PostgreSQL，多陳述式只會傳回第一個查詢回應。

**Aurora Serverless v2**

不支援多陳述式。嘗試在單一 API 呼叫中執行多個陳述式會傳回 `“An error occurred (ValidationException) when calling the ExecuteStatement operation: Multistatements aren't supported.”`。若要執行多個陳述式，請分別進行 `ExecuteStatement` API 呼叫或使用 `BatchExecuteStatement` 進行批次處理。

下列範例顯示 API 呼叫所產生的錯誤訊息，此 API 呼叫會嘗試執行多陳述式。

```
 aws rds-data execute-statement \    
    --resource-arn "arn:aws:rds:region:account:cluster:cluster-name" \    
    --secret-arn "arn:aws:secretsmanager:region:account:secret:secret-name" \    
    --database "your_database" \
    --sql "SELECT * FROM your_table; Select * FROM next_table;
                                
                                "An error occurred (ValidationException) when calling the ExecuteStatement operation: Multistatements aren't supported.
```

下列範例會使用不同的 `ExecuteStatement` API 呼叫執行多個陳述式。

```
aws rds-data execute-statement \
    --resource-arn "arn:aws:rds:region:account:cluster:cluster-name" \
    --secret-arn "arn:aws:secretsmanager:region:account:secret:secret-name" \
    --database "your_database" \
    --sql "SELECT * FROM your_table;"

aws rds-data execute-statement \
    --resource-arn "arn:aws:rds:region:account:cluster:cluster-name" \
    --secret-arn "arn:aws:secretsmanager:region:account:secret:secret-name" \
    --database "your_database" \
    --sql "SELECT * FROM next_table;"
```

## 相同交易 ID 的並行請求
<a name="data-api.differences-concurrent-requests-transaction"></a>

**Aurora Serverless v1**

後續請求會等到目前的請求完成。如果等待期間過長，應用程式需要處理逾時錯誤。

**Aurora Serverless v2**

當資料 API 收到具有相同交易 ID 的多個請求時，其會立即傳回此錯誤：

`DatabaseErrorException: Transaction is still running a query`

此錯誤會在兩種情況下發生：
+ 應用程式會使用相同的交易 ID 提出異步請求 (例如 JavaScript 承諾)。
+ 具有該交易 ID 的先前請求仍在處理中。

下列範例顯示與 `promise.all()` 平行執行的所有請求。

```
const api_calls = [];
for (let i = 0; i < 10; i++) {
api_calls.push(
    client.send(
    new ExecuteStatementCommand({
        ...params,
        sql: `insert into table_name values (i);`,
        transactionId
    })
    )
);
}
await Promise.all(api_calls);
```

若要解決此錯誤，請等待目前的請求完成，再傳送另一個具有相同交易 ID 的請求，或移除交易 ID 以允許平行請求。

下列範例顯示的 API 呼叫會使用具有相同交易 ID 的循序執行。

```
 for (let i = 0; i < 10; i++) {
    await client.send(
    new ExecuteStatementCommand({
        ...params,
        sql: `insert into table_name values (i);`,
        transactionId
    })
    ).promise()
);
}
```

## BatchExecuteStatement 行為
<a name="data-api.differences-batchExecuteStatement"></a>

如需 `BatchExecuteStatement` 的詳細資訊，請參閱 [BatchExecuteStatement](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_BatchExecuteStatement.html)。

**Aurora Serverless v1**

更新結果中產生的欄位物件包含插入的值。

**Aurora Serverless v2**
+ 對於 Aurora MySQL，更新結果中產生的欄位物件包含插入的值。
+ 對於 Aurora PostgreSQL，產生的欄位物件是空的。

## ExecuteSQL 行為
<a name="data-api.differences-ExecuteSQL"></a>

如需 `ExecuteSQL` 的詳細資訊，請參閱 [ExecuteSQL](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_ExecuteSql.html)。

**Aurora Serverless v1**

`ExecuteSQL` 操作已棄用。

**Aurora Serverless v2**

不支援 `ExecuteSQL` 操作。

## ExecuteStatement 行為
<a name="data-api.differences-ExecuteStatement"></a>

如需 `ExecuteStatement` 的詳細資訊，請參閱 [ExecuteStatement](https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_ExecuteStatement.html)。

**Aurora Serverless v1**

`ExecuteStatement` 參數支援擷取多維陣列欄和所有進階資料類型。

**Aurora Serverless v2**

`ExecuteStatement` 參數不支援多維陣列欄。其也不支援某些 PostgreSQL 資料類型 (包括幾何和貨幣類型)。當資料 API 遇到不支援的資料類型時，其會傳回此錯誤：`UnsupportedResultException: The result contains the unsupported data type data_type`。

若要解決此問題，請將不支援的資料類型轉換為 `TEXT`。下列範例會將不支援的資料類型轉換為 `TEXT`。

```
SELECT custom_type::TEXT FROM my_table;-- 
ORSELECT CAST(custom_type AS TEXT) FROM my_table;
```

如需每個 Aurora 資料庫引擎支援的資料類型清單，請參閱[資料 API 操作參考](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html#data-api-operations)。

## 結構描述參數行為
<a name="data-api.differences-schema-parameter"></a>

**Aurora Serverless v1**

不支援 `Schema` 參數。當您在 API 呼叫中包含 `Schema` 參數時，資料 API 會忽略此參數。

**Aurora Serverless v2**

`Schema` 參數已棄用。當您在 API 呼叫中包含 `Schema` 參數時，資料 API 會傳回此錯誤：`ValidationException: The schema parameter isn't supported`。下列範例顯示傳回 `ValidationException` 錯誤的資料 API 呼叫。

```
aws rds-data execute-statement \
--resource-arn "arn:aws:rds:region:account:cluster:cluster-name" \
--secret-arn "arn:aws:secretsmanager:region:account:secret:secret-name" \
--database "your_database" \
--schema "your_schema" \
--sql "SELECT * FROM your_table LIMIT 10"
```

若要解決此問題，請從 API 呼叫中移除 `Schema` 參數。

下列範例顯示已移除 `Schema` 參數的資料 API 呼叫。

```
aws rds-data execute-statement \   
--resource-arn "arn:aws:rds:region:account:cluster:cluster-name" \    
--secret-arn "arn:aws:secretsmanager:region:account:secret:secret-name" \    
--database "your_database" \    
--sql "SELECT * FROM your_table LIMIT 10"
```