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

Azure Cosmos DB日期范围查询高耗时高RU的索引配置方法咨询

索引优化方案

针对你用到的等值过滤(age = 25)+ 时间范围过滤(datetime区间查询)的场景,优先推荐添加复合索引,优化效果远好于单独加范围索引。

1. 最优方案:添加复合索引

Cosmos DB的索引策略配置示例如下:

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/*"
    }
  ],
  "excludedPaths": [
    {
      "path": "/_etag/?"
    }
  ],
  "compositeIndexes": [
    [
      {
        "path": "/age",
        "order": "ascending"
      },
      {
        "path": "/datetime",
        "order": "ascending"
      }
    ]
  ]
}

注意:复合索引需要把等值过滤的字段放在前面,范围过滤的字段放在后面,才能命中索引。配置后查询的RU消耗可以降低70%以上,查询延迟也会明显缩短。

2. 单独范围索引方案(可选)

如果你暂时只需要针对两个字段加范围索引,配置示例如下:

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/age/?",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number",
          "precision": -1
        }
      ]
    },
    {
      "path": "/datetime/?",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "String",
          "precision": -1
        }
      ]
    }
  ],
  "excludedPaths": [
    {
      "path": "/*"
    }
  ]
}

注意:Cosmos DB中Datetime类型默认存储为字符串,所以范围索引的dataType要设置为String,precision设为-1表示最大精度。

内容的提问来源于stack exchange,提问作者Ajay

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 09:00:04