本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
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_id
和 sales_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