DynamoDB PartiQL - The Ultimate Guide (with Examples)
Written by Rafal Wilinski
Published on 2020-11-29
AWS recently announced that DynamoDB will now support PartiQL - an SQL-like language for querying, modifying, and inserting data. This is huge because of few reasons:
- It makes the learning curve a little less steep - You can use well-known language to start using DynamoDB.
- It makes migrations a bit easier - no need to learn and implement DynamoDB query language from the very beginning.
- Choice - it gives you two options/languages to interact with DynamoDB. You don't like
FilterExpressions? Fine, use PartiQL.
How to use PartiQL in DynamoDB:
- Using Dynobase
- The DynamoDB console.
- The AWS Command Line Interface (AWS CLI).
- The DynamoDB APIs.
Querying the data
Keep in mind that even if DynamoDB supports this SQL-like language, it is still paradoxically a NoSQL database, and it is not changing the core principles of how it works. Data is still shared across partitions, GSIs and LSIs are still needed, scans and queries are not going anywhere. Your
SELECT statements are going to be converted into Scan or Query operations depending on the
WHERE clause. If you add a condition on an indexed field, the PartiQL query will be translated to a Query. In other cases, if you will execute something like
SELECT * from my-ddb-table;, it will be translated to a Scan.
Similar to updates, deleting records in bulk is not possible. You can only delete one record in one operation. If you need to remove multiple records or truncate the whole table, consider using Dynobase's Terminal or Truncate Table option.
Just like with traditional DynamoDB query syntax, making transactions using PartiQL is possible. Keep in mind that inside one transactions block, you can only contain either read or write type. Mixing these two in one block is not possible.
Executing transactions is possible via SDK or CLI by passing an array of objects with Statements and Parameters:
Save this JSON as a file and reference using command:
PartiQL DynamoDB update adds four new IAM permissions:
dynamodb:PartiQLSelect- needed to perform read operations on the table
dynamodb:PartiQLInsert- needed to perform put operations on the table
dynamodb:PartiQLUpdate- needed to perform update operations on the table
dynamodb:PartiQLDelete- needed to perform delete operations on the table
If you want to allow all operations on your DynamoDB table, attach the following policy to your role:
- Not suited for analytics, e.g.
SELECT COUNTor complex operations. If you need it, use Athena or write your own aggregation functions based on DynamoDB Streams.
- Amazon DynamoDB supports only a subset of the PartiQL language.
- You cannot use it for
UPDATE X WHERE Ylogic. If you need to conditionally update multiple items at once, head to our guide on how to conditionally update collections in DynamoDB
- Is DynamoDB with SQL support still a NoSQL database? 🙃
© 2021 Dynobase