@shelf/dynamodb-query-optimized
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

dynamodb-query-optimized CircleCI

2x faster DynamoDB queries when you need to query 2+ MB of data

Read the blog post article explaining how it works: https://vladholubiev.medium.com/how-to-speed-up-long-dynamodb-queries-by-2x-c66a2987d53a

Install

$ yarn add @shelf/dynamodb-query-optimized

Benchmark

Regular query: <1 MB of items: 650ms
Optimized query: <1 MB of items: 704ms

Regular query: ~21 MB of items: 9.023s
Optimized query: ~21 MB of items: 4.988s # almost 2x faster

Usage

For now, it supports aws-sdk v2. Feel free to submit a PR to support aws-sdk v3!

Optimized query for 2+ MB of data

Queries DDB from both ends of the query in parallel. Stops and returns results when the middle is reached.

import {queryOptimized} from '@shelf/dynamodb-query-optimized';
import DynamoDB from 'aws-sdk/clients/dynamodb';

const ddb = new DynamoDB.DocumentClient({region: 'us-east-1'});

const results = await queryOptimized({
  queryFunction: ddb.query.bind(ddb),
  queryParams: {
    TableName: 'example_table',
    ProjectionExpression: 'hash_key, range_key',
    KeyConditionExpression: '#hash_key = :hash_key AND begins_with(#range_key, :range_key)',
    ExpressionAttributeNames: {
      '#hash_key': 'hash_key',
      '#range_key': 'range_key',
    },
    ExpressionAttributeValues: {
      ':hash_key': hash_key,
      ':range_key': range_key,
    },
  },
});

console.log(results);
/*
  [{hash_key: 'foo', range_key: 'bar'}, {hash_key: 'foo', range_key: 'baz'}]
 */

Regular query for <2 MB of data

Queries DDB and continues to paginate through all results until query is exhausted.

import {queryRegular} from '@shelf/dynamodb-query-optimized';
import DynamoDB from 'aws-sdk/clients/dynamodb';

const ddb = new DynamoDB.DocumentClient({region: 'us-east-1'});

const results = await queryRegular({
  queryFunction: ddb.query.bind(ddb),
  queryParams: {
    TableName: 'example_table',
    ProjectionExpression: 'hash_key, range_key',
    KeyConditionExpression: '#hash_key = :hash_key AND begins_with(#range_key, :range_key)',
    ExpressionAttributeNames: {
      '#hash_key': 'hash_key',
      '#range_key': 'range_key',
    },
    ExpressionAttributeValues: {
      ':hash_key': hash_key,
      ':range_key': range_key,
    },
  },
});

console.log(results);
/*
  [{hash_key: 'foo', range_key: 'bar'}, {hash_key: 'foo', range_key: 'baz'}]
 */

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

License

MIT © Shelf

/@shelf/dynamodb-query-optimized/

    Package Sidebar

    Install

    npm i @shelf/dynamodb-query-optimized

    Weekly Downloads

    6,000

    Version

    2.1.0

    License

    MIT

    Unpacked Size

    15.1 kB

    Total Files

    17

    Last publish

    Collaborators

    • el_scrambone
    • yuliiakovalchuk
    • anton-russo
    • gemshelf
    • mykola.khytra
    • hartzler
    • olesiamuller
    • vladgolubev
    • hmelenok
    • knupman
    • maaraanas
    • terret
    • chapelskyi.slavik
    • ahavrysh
    • pihorb
    • i5adovyi
    • irynah
    • diana.kryskuv
    • andy.raven
    • rafler
    • sskalp88
    • mykhailo.yatsko
    • demiansua
    • yuriil
    • vadymaslovskyi
    • ktv18
    • drews_abuse
    • rostyslav-horytskyi
    • whodeen
    • andriisermiahin
    • mpushkin
    • ss1l
    • oles.zadorozhnyy
    • maksym.hayovets
    • dima-bond
    • duch0416
    • kristina.zhak
    • oleksii.dymnich
    • domovoj
    • batovpavlo
    • mateuszgajdashelf
    • bodyaflesh
    • dmytro.harazdovskiy
    • kchlon
    • mmazurowski
    • vladmarchuk
    • petro.bodnarchuk
    • marianna-milovanova
    • kateryna-kochina
    • andrii-nastenko
    • maksym.tarnavskyi
    • bogdan.kolesnyk
    • andrew214
    • monopotan
    • maciej.orlowski
    • yaroslav_korolchuk