選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

在 Amazon Keyspaces 的查詢中使用 IN運算子搭配 SELECT陳述式

焦點模式
在 Amazon Keyspaces 的查詢中使用 IN運算子搭配 SELECT陳述式 - Amazon Keyspaces (適用於 Apache Cassandra)

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

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

SELECT IN

您可以使用 SELECT陳述式從資料表查詢資料,該陳述式會讀取資料表中一或多個資料列的一或多個資料欄,並傳回包含符合請求之資料列的結果集。SELECT 陳述式包含 select_clause,決定要在結果集中讀取和傳回哪些資料欄。子句可以包含在傳回資料之前轉換資料的指示。選用WHERE的子句指定哪些資料列必須查詢,並由屬於主索引鍵的欄上的關係組成。Amazon Keyspaces 支援 WHERE子句中的IN關鍵字。本節使用範例來示範 Amazon Keyspaces 如何使用 IN關鍵字處理SELECT陳述式。

此範例示範 Amazon Keyspaces 如何將具有 IN關鍵字的SELECT陳述式分解為子查詢。在此範例中,我們使用名為 的資料表my_keyspace.customers。資料表有一個主索引鍵欄 department_id、兩個叢集欄 sales_region_idsales_representative_id,以及一個包含欄中客戶名稱的customer_name欄。

SELECT * FROM my_keyspace.customers; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 0 | 0 | 0 | a 0 | 0 | 1 | b 0 | 1 | 0 | c 0 | 1 | 1 | d 1 | 0 | 0 | e 1 | 0 | 1 | f 1 | 1 | 0 | g 1 | 1 | 1 | h

使用此資料表,您可以執行下列SELECT陳述式,在感興趣的部門和銷售區域中,使用 WHERE子句中的IN關鍵字來尋找客戶。下列陳述式是此範例。

SELECT * FROM my_keyspace.customers WHERE department_id IN (0, 1) AND sales_region_id IN (0, 1);

Amazon Keyspaces 將此陳述式分成四個子查詢,如下列輸出所示。

SELECT * FROM my_keyspace.customers WHERE department_id = 0 AND sales_region_id = 0; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 0 | 0 | 0 | a 0 | 0 | 1 | b SELECT * FROM my_keyspace.customers WHERE department_id = 0 AND sales_region_id = 1; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 0 | 1 | 0 | c 0 | 1 | 1 | d SELECT * FROM my_keyspace.customers WHERE department_id = 1 AND sales_region_id = 0; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 1 | 0 | 0 | e 1 | 0 | 1 | f SELECT * FROM my_keyspace.customers WHERE department_id = 1 AND sales_region_id = 1; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 1 | 1 | 0 | g 1 | 1 | 1 | h

使用IN關鍵字時,Amazon Keyspaces 會在下列任何情況下自動分頁結果:

  • 每處理第 10 次子查詢之後。

  • 處理 1MB 邏輯 IO 之後。

  • 如果您設定 PAGE SIZE,Amazon Keyspaces 會在讀取查詢數目後分頁,以根據集 進行處理PAGE SIZE

  • 當您使用 LIMIT關鍵字來減少傳回的資料列數時,Amazon Keyspaces 會在讀取查詢數目後分頁,以便根據集 進行處理LIMIT

下表用於以 範例來說明這一點。

如需分頁的詳細資訊,請參閱在 Amazon Keyspaces 中分頁結果

SELECT * FROM my_keyspace.customers; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 2 | 0 | 0 | g 2 | 1 | 1 | h 2 | 2 | 2 | i 0 | 0 | 0 | a 0 | 1 | 1 | b 0 | 2 | 2 | c 1 | 0 | 0 | d 1 | 1 | 1 | e 1 | 2 | 2 | f 3 | 0 | 0 | j 3 | 1 | 1 | k 3 | 2 | 2 | l

您可以在此表格上執行下列陳述式,以查看分頁的運作方式。

SELECT * FROM my_keyspace.customers WHERE department_id IN (0, 1, 2, 3) AND sales_region_id IN (0, 1, 2) AND sales_representative_id IN (0, 1);

Amazon Keyspaces 會將此陳述式處理為 24 個子查詢,因為此查詢中包含之所有IN詞彙的笛卡斯文產品基數為 24。

department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 0 | 0 | 0 | a 0 | 1 | 1 | b 1 | 0 | 0 | d 1 | 1 | 1 | e ---MORE--- department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 2 | 0 | 0 | g 2 | 1 | 1 | h 3 | 0 | 0 | j ---MORE--- department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 3 | 1 | 1 | k

此範例示範如何在 SELECT 陳述式中使用 ORDER BY子句與 IN關鍵字。

SELECT * FROM my_keyspace.customers WHERE department_id IN (3, 2, 1) ORDER BY sales_region_id DESC; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 3 | 2 | 2 | l 3 | 1 | 1 | k 3 | 0 | 0 | j 2 | 2 | 2 | i 2 | 1 | 1 | h 2 | 0 | 0 | g 1 | 2 | 2 | f 1 | 1 | 1 | e 1 | 0 | 0 | d

子查詢的處理順序是分割區索引鍵和叢集索引鍵資料欄在查詢中呈現的順序。在下面的範例中,會先處理分割區索引鍵值 ”2” 的子查詢,接著處理分割區索引鍵值 ”3” 和 ”1” 的子查詢。指定子查詢的結果會根據查詢的排序子句,如果有,或資料表建立期間定義的資料表叢集順序來排序。

SELECT * FROM my_keyspace.customers WHERE department_id IN (2, 3, 1) ORDER BY sales_region_id DESC; department_id | sales_region_id | sales_representative_id | customer_name ---------------+-----------------+-------------------------+-------------- 2 | 2 | 2 | i 2 | 1 | 1 | h 2 | 0 | 0 | g 3 | 2 | 2 | l 3 | 1 | 1 | k 3 | 0 | 0 | j 1 | 2 | 2 | f 1 | 1 | 1 | e 1 | 0 | 0 | d

下一個主題:

訂單結果

上一個主題:

使用 CQL 查詢
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。