Can we query first n rows from DynamoDB using PartiQL?

0

I'm trying to fetch first 5 rows from DynamoDB using PartiQL but couldnot find any syntax or maybe I'm missing something on that. My requirement is to only use it through PartiQL and not by any document model or client library.

질문됨 일 년 전2913회 조회
2개 답변
3
수락된 답변

To suit your needs you can use the Limit parameter for PartiQL where you can limit the results to n rows:

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ExecuteStatement.html#DDB-ExecuteStatement-request-Limit

JS Example:

const params = {
   "Limit": 3,
   "Statement": "SELECT * FROM mytable"
}
const result = client.executeStatement(params).promise()
profile pictureAWS
전문가
답변함 일 년 전
1

As of writing, PartiQL does not support a "LIMIT" clause. But if you'd like to use PartiQL only, you could run a """SELECT * FROM "<table_name>"""" that would return up to 1MB worth of items and then pick the first 5. This may not be super efficient until a LIMIT clause is supported.

PartiQL SELECT - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.select.html

Even when using PartiQL, you are directly interacting with DynamoDB via an AWS SDK (if not using the AWS Console). Using a Scan API on the SDK does support a LIMIT parameter which can be efficient as it would only return 5 items. Note that the behavior in terms of what items are returned are going to be same for the above mentioned PartiQL query or the Scan API.

If you use a WHERE clause in your PartiQL query with the table's partition key as a filter, then you'd get the results for that partition key sorted by the table's Sort key value (if the table has a Sort key). The non-PartiQL equivalent of this operation would be a Query API.

Query API - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

Scan API - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

Any of the ways listed above would return a maximum of 1MB worth of items. For Query/Scan APIs, there is a LIMIT parameter supported which can help be efficient in only returning the number of items you need. Hope this helps!

profile pictureAWS
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인