使用 Amazon Keyspaces 中的查詢 - Amazon Keyspaces (適用於 Apache Cassandra)

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

使用 Amazon Keyspaces 中的查詢

本節介紹了在 Amazon Keyspaces 間查詢工作(對於 Apache 卡桑德拉)。可用於查詢、轉換和管理資料的 CQL 陳述式為SELECTINSERTUPDATE、和DELETE。下列主題概述了處理查詢時可用的一些較複雜的選項。如需具有範例的完整語言語法,請參閱Amazon Keyspaces 中的 DML 語句(數據操作語言)

使用IN運營商與 Amazon Keyspaces 的SELECT聲明

在中選擇

您可以使用陳述式查詢資料表中的資料,該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 space 會在根據集合讀取要處理的查詢數目之後進行分頁。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

這個範例說明如何在含有IN關鍵字的SELECT陳述式中使用ORDER BY子句。

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

在 Amazon Keyspaces 中訂購結果

ORDER BY句會指定SELECT陳述式中傳回之結果的排序順序。該語句將列名列表作為參數,並且您可以為每個列指定數據的排序順序。您只能在排序子句中指定叢集資料行,不允許使用非叢集資料行。

傳回結果的兩個可用排序順序選項分別ASC為遞增和DESC遞減排序順序。

SELECT * FROM my_keyspace.my_table ORDER BY (col1 ASC, col2 DESC, col3 ASC); col1 | col2 | col3 ------+------+------ 0 | 6 | a 1 | 5 | b 2 | 4 | c 3 | 3 | d 4 | 2 | e 5 | 1 | f 6 | 0 | g
SELECT * FROM my_keyspace.my_table ORDER BY (col1 DESC, col2 ASC, col3 DESC); col1 | col2 | col3 ------+------+------ 6 | 0 | g 5 | 1 | f 4 | 2 | e 3 | 3 | d 2 | 4 | c 1 | 5 | b 0 | 6 | a

如果您沒有在查詢陳述式中指定排序順序,則會使用叢集資料行的預設順序。

您可以在排序子句中使用的可能排序順序取決於在建立表格時指派給每個叢集資料行的排序順序。查詢結果只能按照在建立資料表時為所有叢集資料行定義的順序排序,或是定義的排序順序相反。不允許使用其他可能的組合。

例如,如果表的CLUSTERING ORDER是(COL1 ASC,COL2 描述,COL3 ASC),則的有效參數是(COL1 ASC,COL2 描述,COL3 ASC)或(COL1 描述,COL2 ASC,COL3 描述)。ORDER BY如需詳細資訊CLUSTERING ORDER,請參閱〈〉中的table_options〈〉CREATE TABLE

Amazon Keyspaces 中的分頁結果

當讀取以處理陳述式的資料超過 1 MB 時,Amazon Keyspaces 會自動分頁SELECT陳述式的SELECT結果。使用分頁時,SELECT陳述式結果會分成大小為 1 MB (或更小) 的資料「頁面」。應用程式可以處理結果的第一頁、第二頁,以此類推。在處理返回多行的SELECT查詢時,客戶端應始終檢查分頁令牌。

如果用戶端提供PAGE SIZE的需要讀取超過 1 MB 的資料,Amazon Keyspaces 會根據 1 MB 的資料讀取增量自動將結果分解為多個頁面。

例如,如果某個資料列的平均大小為 100 KB,而您指定PAGE SIZE的 A 值為 20,則 Amazon Keyspaces 會在讀取 10 個資料列 (讀取資料的 1000 KB) 之後自動分頁資料。

由於 Amazon Keyspaces 會根據其讀取來處理請求的資料列數,而不是結果集中傳回的資料列數目來分頁結果,因此如果您執行篩選的查詢,某些頁面可能不會包含任何資料列。

例如,如果您設定PAGE SIZE為 10,而 Keyspaces 會評估 30 列來處理您的SELECT查詢,Amazon Keyspaces 將會傳回三個頁面。如果只有一個資料列子集符合您的查詢,部分頁面的資料列可能少於 10 列。如需LIMIT查詢如何影響讀取容量PAGE SIZE的範例,請參閱限制查詢