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

获取文档数量、大小及分区统计的高效技术方案咨询

Solution for Getting Cosmos DB Document Count & Total Size Without Extra Overhead

Great question! I’ve dealt with this exact Cosmos DB quirk before—those query metric tradeoffs are definitely frustrating. Let’s break down the best ways to get the metrics you need without scanning every document or bloating your output size:


1. Use Cosmos DB’s Built-in Management Metrics (No Queries Needed)

Cosmos DB maintains metadata about your containers automatically, and you can access this through either the Azure Portal or management APIs, no document scans required.

Azure Portal Quick Access

  • Navigate to your Cosmos DB account → Go to the Metrics blade
  • Select your target container, then choose these pre-built metrics:
    • Document Count: Gives you the total number of items in the container (or per partition, if you filter by partition key)
    • Used Storage: Shows the total size of all documents in the container (again, filterable by partition)

These metrics are updated near-real-time, free to access, and don’t generate any query costs or output overhead. Perfect for quick monitoring or tenant quota checks.

Programmatic Access via Management SDK

If you need to pull these metrics into your application (for example, to enforce tenant limits), use the Cosmos DB Management SDK. Here’s a quick C# example:

using Azure.ResourceManager.CosmosDB;
using Azure.ResourceManager.CosmosDB.Models;

// Initialize your Cosmos DB management client
var cosmosClient = new CosmosDBManagementClient(new DefaultAzureCredential());

// Get container details (replace with your resource group, account, DB, and container names)
var container = await cosmosClient.DatabaseContainers.GetAsync(
    "your-resource-group",
    "your-cosmos-account",
    "your-database",
    "your-container");

// Extract the metrics you need
long totalDocumentCount = container.Resource.DocumentCount.Value;
long totalStorageSizeBytes = container.Resource.Size.Value;

This pulls the precomputed metadata directly from Cosmos DB, so no document reads are involved.


2. Precompute Tenant-Level Stats (For Partition-Based Tenants)

If you’re using partition keys to isolate tenants and need per-tenant counts/sizes, a precomputed stats document is your best bet. Here’s how to set it up:

  • Create a dedicated tenant-stats container where each document maps to a tenant, with fields like tenantId, documentCount, and totalSizeBytes.
  • Use a Change Feed Trigger on your main tenant container: every time a document is added, updated, or deleted, the trigger updates the corresponding tenant’s stats document. For example:
    • On document create: increment documentCount by 1, add the document’s size to totalSizeBytes
    • On document delete: decrement documentCount by 1, subtract the document’s size from totalSizeBytes
    • On document update: subtract the old size, add the new size to totalSizeBytes

This way, you can get per-tenant metrics with a single, tiny query (just fetch the tenant’s stats document) — no scanning thousands of documents, no bloated output size.


Why Your Original Queries Have Tradeoffs

Just to clarify why you saw those odd metric values:

  • SELECT VALUE COUNT(1) FROM c: Cosmos DB uses its index to compute the count directly, so it never reads actual documents. That’s why Retrieved document size is 0 — it’s just using index metadata.
  • SELECT c.id FROM c: This forces Cosmos DB to read the id field from every document, hence the accurate Retrieved document size, but it also sends all those id values back as output, bloating Output document size.

The methods above avoid both issues entirely by leveraging precomputed metadata or targeted stats updates.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 07:32:52