查询多个本地二级索引 Dynamodb [英] Querying with multiple local Secondary Index Dynamodb

查看:22
本文介绍了查询多个本地二级索引 Dynamodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表中有 2 个 LSI,主分区键和主排序键

I have 2 LSI in my table with a primary partition Key with primary sort key

Org-ID - 主分区键

Org-ID - primary partition Key

ClientID- 主排序键

ClientID- primary sort Key

性别 - LSI

部分 - LSI

我对使用一个 LSI 查询表没有问题,但是如何在表架构中提及 2 LSI.

I have no issue with querying a table with one LSI, but how to mention 2 LSI in a table schema.

 var params = {
 TableName:"MyTable",
 IndexNames: ['ClientID-Gender-index','ClientID-Section-index'], 

KeyConditionExpression : '#Key1 = :Value1 and #Key2=:Value2 and #Key3=:Value3', 

ExpressionAttributeNames:{
"#Key1":"Org-ID",
"#Key2":"Gender",
"#Key3":"Section"
},

ExpressionAttributeValues : {
':Value1' :"Microsoft",
':Value2':"Male",
':Value3':"Cloud Computing"
}};

谁能解决 IndexName(第 3 行)或 KeyConditionExpression(第 4 行)中的问题,我不确定.

Can anyone fix the issue in IndexName(line 3) or KeyConditionExpression(line 4), I'm not sure about it.

问题

条件的长度只能是 1 或 2

Condition can be of length 1 or 2 only

推荐答案

一次只能查询一个 DynamoDB 索引.您不能在同一个查询中使用多个索引.

You can only query a single DynamoDB index at a time. You cannot use multiple indexes in the same query.

一种简单的替代方法是使用单个索引并应用查询过滤器,但这可能需要扫描大量记录,而过滤器只会减少通过网络传输的数据量.

A simple alternative is to use a single index and apply a query filter, but this will potentially require a lot of records to be scanned and the filter only reduces the amount of data transferred over the network.

更高级的替代方法是制作复合键.对于此用例,您很可能希望使用 GSI,而不是 LSI.通过创建一个新列,即 Key1、Key2 和 Key3 的字符串连接,您可以使用此 GSI 同时搜索所有三个键.这将通过重复数据使每个单独的记录更大,但它允许更复杂的查询模式.

A more advanced alternative is to make a compound key. You would most likely want to use a GSI, rather than an LSI for this use case. By making a single new column that is the string concatenation of Key1, Key2, and Key3 you can use this GSI to search all three keys at the same time. This will make each individual record bigger by repeating data but it allows for a more complex query pattern.

这篇关于查询多个本地二级索引 Dynamodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆