DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2 1

[20 Days of DynamoDB] Day 19 - PartiQL Batch Operations

Posted: 12/Feb/2024

You can use batched operations with PartiQL as well, thanks to BatchExecuteStatement. It allows you to batch reads as well as write requests.

Here is an example (note that you cannot mix both reads and writes in a single batch):

    //read statements
    client.BatchExecuteStatement(context.Background(), &dynamodb.BatchExecuteStatementInput{
        Statements: []types.BatchStatementRequest{
            {
                Statement: aws.String("SELECT * FROM url_metadata where shortcode=?"),
                Parameters: []types.AttributeValue{
                    &types.AttributeValueMemberS{Value: "abcd1234"},
                },
            },
            {
                Statement: aws.String("SELECT * FROM url_metadata where shortcode=?"),
                Parameters: []types.AttributeValue{
                    &types.AttributeValueMemberS{Value: "qwer4321"},
                },
            },
        },
    })

    //separate batch for write statements
    client.BatchExecuteStatement(context.Background(), &dynamodb.BatchExecuteStatementInput{
        Statements: []types.BatchStatementRequest{
            {
                Statement: aws.String("INSERT INTO url_metadata value {'longurl':?,'shortcode':?, 'active': true}"),
                Parameters: []types.AttributeValue{
                    &types.AttributeValueMemberS{Value: "https://github.com/abhirockzz"},
                    &types.AttributeValueMemberS{Value: uuid.New().String()[:8]},
                },
            },
            {
                Statement: aws.String("UPDATE url_metadata SET active=? where shortcode=?"),
                Parameters: []types.AttributeValue{
                    &types.AttributeValueMemberBOOL{Value: false},
                    &types.AttributeValueMemberS{Value: "abcd1234"},
                },
            },
            {
                Statement: aws.String("DELETE FROM url_metadata where shortcode=?"),
                Parameters: []types.AttributeValue{
                    &types.AttributeValueMemberS{Value: "qwer4321"},
                },
            },
        },
    })
Enter fullscreen mode Exit fullscreen mode

Just like BatchWriteItem, BatchExecuteStatement is limited to 25 statements (operations) per batch.

Recommended reading:

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay