AWS DynamoDB LSI(局部二级索引)对于每个分区键值最多只能存储10GB的数据,而当LSI项集合大小超过10GB时,查询操作将会失败。为了解决这个问题,我们可以通过以下步骤来进行调整:
1.收集LSI的信息,如分区键和排序键等。
2.使用DynamoDBMapper API或aws-sdk-js创建新的表。
3.将LSI的信息添加到新表的索引中,并将数据导入到新表中。
4.更新原始表中的代码,将查询指向新的表以进行查询操作。
下面是一个示例代码(使用Node.js 和 aws-sdk-js):
const AWS = require('aws-sdk');
const config = {
region: 'us-east-1', // your AWS region
};
const dynamodb = new AWS.DynamoDB(config);
const sourceTable = 'your-source-table';
const lsiIndexName = 'your-lsi-name';
const newTableName = `${sourceTable}-new`;
// Collect LSI info
const sourceTableRes = await dynamodb.describeTable({ TableName: sourceTable }).promise();
const lsi = sourceTableRes.Table.LocalSecondaryIndexes.find(
(index) => index.IndexName === lsiIndexName
);
const { KeySchema, Projection } = lsi;
// Create new table
const newTableParams = {
AttributeDefinitions: sourceTableRes.Table.AttributeDefinitions,
KeySchema: sourceTableRes.Table.KeySchema,
TableName: newTableName,
BillingMode: 'PAY_PER_REQUEST', // This creates a new table with on-demand pricing
LocalSecondaryIndexes: [
{
IndexName: lsiIndexName,
KeySchema,
Projection,
},
],
};
await dynamodb.createTable(newTableParams).promise();
// Wait for the new table to be active
while (true) {
const res = await dynamodb.describeTable({ TableName: newTableName }).promise();
if (res.Table.TableStatus === 'ACTIVE') break;
await new Promise((resolve) => setTimeout(resolve, 2000));
}
// Batch write items to the new table
const scanParams = {
TableName: sourceTable,
ProjectionExpression: lsi.Projection.NonKeyAttributes,
};
let lastEvaluatedKey;
while (lastEvaluatedKey !== undefined) {
if (lastEvaluatedKey) scanParams.ExclusiveStartKey = lastEvaluatedKey;
const res = await dynamodb.scan(scanParams).promise();
const batchWriteParams = {
RequestItems: {
[newTableName]: res.Items.map((item) => ({