Skip to content

Instantly share code, notes, and snippets.

@Siddhant-K-code
Created May 8, 2024 04:34
Show Gist options
  • Save Siddhant-K-code/ee16cd382bfc8d98f2ac596c9d6af16c to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/ee16cd382bfc8d98f2ac596c9d6af16c to your computer and use it in GitHub Desktop.
TIL (05/08/2024): DynamoDB Query Evaluation Order Differs from SQL

DynamoDB Query Evaluation Order Differs from SQL

Summary:

When using LIMIT in DynamoDB queries, the order of evaluation can differ from SQL queries, potentially leading to unexpected results depending on the search criteria used.

SQL Query Evaluation Order:

SQL queries follow a specific order of evaluation:

FROM -> ON -> JOIN -> WHERE -> GROUP BY -> HAVING -> SELECT -> DISTINCT -> ORDER BY -> LIMIT

LIMIT is evaluated last, ensuring that only records meeting the specified criteria are retrieved and limited.

DynamoDB Query Evaluation Order:

In DynamoDB, the evaluation order depends on the type of search condition used:

  1. Key Condition Expression (key_condition_expression):

    • Behaves similar to SQL, where LIMIT is evaluated after filtering records based on the key condition.
  2. Filter Expression (filter_expression):

    • LIMIT is evaluated first, and then the filter_expression is applied to the resulting set of records.
    • This means that records not meeting the filter condition may still be retrieved, up to the specified LIMIT.

Implications:

We must be cautious when implementing LIMIT in queries involving filter_expression in DynamoDB. This behavior might lead to retrieving an insufficient number of items that actually meet the criteria, as LIMIT might discard relevant items before they are even evaluated against the filter. Awareness and appropriate query design adjustments are essential to ensure that the application logic remains robust and performs as intended.

Recommendations:

  • Always verify the behavior of LIMIT in your specific use case when working with DynamoDB.
  • Consider fetching more items initially or restructuring your queries to ensure that all necessary data is correctly retrieved and filtered.
  • If possible, use key_condition_expression instead of filter_expression when combined with LIMIT, as it behaves more predictably like SQL.
  • Implement thorough testing and validation to ensure the expected results are obtained, especially when using LIMIT and filter_expression together.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment