You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DynamoDB GSI带FilterExpression查询无结果(存在符合条件记录)问题排查求助

DynamoDB GSI带FilterExpression查询无结果(存在符合条件记录)问题排查求助

各位大佬好,我最近在使用DynamoDB的GSI查询时遇到了一个棘手的问题,想请大家帮忙排查下。

我需要查询指定parentUploadId下第一条状态不是"COMPLETED"的记录,用的是名为parentUploadId-index的GSI。我写了异步方法QueryFirstIncompleteRecordAsync,但奇怪的是,明明数据库里存在status为"PENDING"的有效记录,这个方法却总是返回(0, parentUploadId)。

以下是我的代码实现:

public async Task<(int, int)> QueryFirstIncompleteRecordAsync(int parentUploadId)
{
    Dictionary<string, AttributeValue> lastEvaluatedKey = null;
    do
    {
        var queryRequest = new QueryRequest
        {
            TableName = _tableName,
            IndexName = "parentUploadId-index",
            KeyConditionExpression = "parentUploadId = :parentId",
            FilterExpression = "#status <> :completedStatus",
            ExpressionAttributeNames = new()
            {
                { "#status", "status" }
            },
            ExpressionAttributeValues = new()
            {
                { ":parentId", new AttributeValue { S = parentUploadId.ToString() } },
                { ":completedStatus", new AttributeValue { S = "COMPLETED" } }
            },
            ExclusiveStartKey = lastEvaluatedKey
        };

        var response = await _db.QueryAsync(queryRequest);

        var record = response.Items
            .Select(i => new
            {
                UploadId = int.Parse(i["uploadId"].S),
                ParentUploadId = int.Parse(i["parentUploadId"].S)
            })
            .OrderBy(r => r.UploadId)
            .FirstOrDefault();

        if (record != null)
            return (record.UploadId, record.ParentUploadId);

        lastEvaluatedKey = response.LastEvaluatedKey;
    } while (lastEvaluatedKey != null);

    return (0, parentUploadId);
}

我现在有几个疑问点想请教大家:

  • 是不是FilterExpression里的<>操作符用法有问题?有没有更合适的过滤方式来实现“排除指定状态”的需求?
  • 另外一个关键点是:我是在插入记录毫秒级之后就调用这个查询,会不会是GSI的传播延迟导致查询时数据还没同步到索引里?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:27:58