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

如何快速获取Azure Cosmos DB账户中所有Mongo API集合的已配置吞吐量信息

Faster Ways to Fetch Cosmos DB MongoDB Collection Autoscale Throughput Data

Your original script is slow because it makes individual REST API calls for every single collection's throughput—with 94 collections, that's 94 separate requests, plus additional calls to fetch databases and collections. This adds up to significant latency, especially in Cloud Shell where network overhead can stack. Here are two optimized approaches to get your data in seconds instead of minutes:

Azure Resource Graph (ARG) lets you query all your Azure resources in a single, batch request—this is by far the quickest way to pull the throughput data you need. It avoids the per-collection API call bottleneck entirely.

First, make sure you have the Az.ResourceGraph module installed (it's pre-installed in Cloud Shell, but if you're using a local environment, run Install-Module Az.ResourceGraph -Force). Then use this script:

$subscriptionId = "your-subscription-id"
$rgName = "your-resource-group-name"
$accountName = "your-cosmosdb-account-name"

# Azure Resource Graph query to fetch all MongoDB collection throughput settings
$graphQuery = @"
resources
| where type == "microsoft.documentdb/databaseaccounts/mongodbdatabases/collections/throughputsettings"
| where resourceGroup == "$rgName"
| where name startswith "$accountName/"
| split name by '/' into account, _, dbName, _, collName, _
| project 
    DatabaseName = dbName,
    CollectionName = collName,
    Throughput = properties.resource.throughput,
    MinimumThroughput = properties.resource.minimumThroughput,
    AutoscaleMaxThroughput = properties.resource.autoscaleSettings.maxThroughput
"@

# Run the query and retrieve results
$throughputData = Invoke-AzGraph -Query $graphQuery -Subscription $subscriptionId

# Format the output (adjust as needed for your use case)
$throughputData | Format-Table DatabaseName, CollectionName, Throughput, MinimumThroughput, AutoscaleMaxThroughput -AutoSize

Why this works so fast:

  • ARG is designed for bulk resource inventory queries—one request returns all matching resources, no loops needed.
  • It parses the resource name path to extract database and collection names directly, so you don't need separate calls to fetch databases/collections first.
  • For your 94-collection scenario, this should return results in 2-5 seconds instead of minutes.

2. Parallelize Az Module Calls (Alternative)

If you prefer to stick with the Az.CosmosDB module, you can use PowerShell's parallel processing to run multiple throughput requests at the same time. This cuts down on total latency by overlapping API calls (requires PowerShell 7+, which is the default in Cloud Shell).

Set-AzContext -Subscription "your-subscription-id"
$rgName = "your-resource-group-name"
$accountName = "your-cosmosdb-account-name"

# First, get a list of all databases and collections (single batch of calls)
$allCollections = Get-AzCosmosDBMongoDBDatabase -ResourceGroupName $rgName -AccountName $accountName | ForEach-Object {
    $db = $_
    Get-AzCosmosDBMongoDBCollection -ResourceGroupName $rgName -AccountName $accountName -Database $db.Name | ForEach-Object {
        [PSCustomObject]@{
            DatabaseName = $db.Name
            CollectionName = $_.Name
        }
    }
}

# Fetch throughput data in parallel
$throughputResults = $allCollections | ForEach-Object -Parallel {
    # Pass external variables into the parallel scope
    $rg = $using:rgName
    $account = $using:accountName
    
    Get-AzCosmosDBMongoDBCollectionThroughput -ResourceGroupName $rg -AccountName $account -DatabaseName $_.DatabaseName -Name $_.CollectionName |
        Select-Object -Property Throughput, MinimumThroughput,
            @{Name = 'DatabaseName'; Expression = {$_.DatabaseName}},
            @{Name = 'CollectionName'; Expression = {$_.Name}},
            @{Name = 'AutoscaleMaxThroughput'; Expression = {$_.AutoscaleSettings.MaxThroughput}}
}

# View results
$throughputResults | Format-Table DatabaseName, CollectionName, Throughput, MinimumThroughput, AutoscaleMaxThroughput -AutoSize

Why this is faster than your original script:

  • Instead of processing collections one after another, it runs multiple throughput requests simultaneously. For 94 collections, this can reduce runtime from ~100 seconds to ~20-30 seconds.
  • It fetches all database/collection metadata first in a single pass, avoiding redundant calls in the loop.

Final Notes

  • The Azure Resource Graph method is hands-down the best choice for large-scale inventory tasks like this—it's faster, more efficient, and easier to maintain.
  • If you need to filter further (e.g., only collections with autoscale enabled), you can add a clause to the ARG query like | where properties.resource.autoscaleSettings != "".

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 19:37:35