简体   繁体   English

将 DynamoDB 扫描操作替换为查询

[英]Replace DynamoDB Scan operation with Query

I am using DynamoDB Scan operations to get all items from DynamoDB table.我正在使用 DynamoDB 扫描操作从 DynamoDB 表中获取所有项目。 The code is as below:代码如下:

"use strict";
const AWS = require("aws-sdk");

module.exports.get = async () => {
  try {
    const dynamodb = new AWS.DynamoDB.DocumentClient();

    console.log("getting items");

    const params = {
      TableName: "ProductsTable",
    };

    const result = await dynamodb.scan(params).promise();

    console.log("got results", result.Items);

    return { body: JSON.stringify(result.Items) };
  } catch (error) {
    console.error(error);
    return {
      status: 500,
      message: error.message,
      body: JSON.stringify(error),
    };
  }
};

But scan is not an efficient operation an is not recommended.但扫描不是一种有效的操作,不推荐使用。 How can I use Query operation to get all items from the table ?如何使用查询操作从表中获取所有项目? Can I replace scan Operation with Query to get all items ?我可以用查询替换扫描操作来获取所有项目吗? Is there any other way to get all items from the table ?有没有其他方法可以从表中获取所有项目? Please let me know.请告诉我。

thanks谢谢

Scan is only inefficient because it has to look at everything.扫描效率低下,因为它必须查看所有内容。

If you want to return everything anyways, there's no reason not to use Scan.如果您无论如何都想返回所有内容,则没有理由不使用 Scan。

In fact, you could use the built in parallel scan to speed up the process.事实上,您可以使用内置的并行扫描来加速该过程。

Query() has to be run in parallel manually by your application code. Query() 必须由您的应用程序代码手动并行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM