Search code examples
amazon-web-servicesamazon-dynamodbdynamodb-queries

DynamoDB 1MB limit on full item or only requested attributes?


Suppose I make a Query in which I use KeyConditionExpression and I only want to return a subset of all attributes with ProjectionExpression. Does the full item size count toward the 1MB limit, or only what I'm fetching?

There is a slight ambiguity which I would like to clarify with respect to this question, because there it mentions read capacity, but not size limit.


Solution

  • That is a good question. I've decided to test it.

    It seems it doesn't change the page size, but it does help with performance. My objects have a dummy data field that is quite large, so it's normal to get much better response times, given the decrease in bytes that need to be transferred.

    Here is my code

    // Without Projection
    long start = System.currentTimeMillis();
    double avgPageSize = 0;
    
    for (var page : table.scan()) {
      avgPageSize = (avgPageSize + page.items().size()) / 2;
    }
    
    System.out.println("Without Projection ms " + (System.currentTimeMillis() - start));
    System.out.println("Average Page size " + avgPageSize);
    
    
    // With projection
    start = System.currentTimeMillis();
    avgPageSize = 0;
    scanEnhancedRequest = ScanEnhancedRequest.builder()
      .addAttributeToProject("PK")
      .build();
    
    for (var page : table.scan(scanEnhancedRequest)) {
      avgPageSize = (avgPageSize + page.items().size()) / 2;
    }
    
    System.out.println("With Projection ms " + (System.currentTimeMillis() - start));
    System.out.println("Average Page size " + avgPageSize);
    

    And the results

    Without Projection ms 13862
    Average Page size 3062.7655089609325
    
    With Projection ms 2241
    Average Page size 3062.7655089609325
    

    So it seems the sequence is query -> calculate capacity units -> filters -> pagination + projection

    The last one is just an assumption