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

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

问题描述

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

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

组织ID-主分区键

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.

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

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