Share via

Cosmos DB is not working correctly

Vinod Pal 20 Reputation points
2026-03-06T18:11:05.9833333+00:00

I am running into a query performance issue with Azure Cosmos DB using the SQL API and I am not sure if I am missing something in the indexing configuration. My container stores documents with a nested array field (for example items[]), and I am querying it using ARRAY_CONTAINS to filter documents based on a value inside that array. Even though indexing is enabled with the default policy, the query is consuming a lot of RUs and the query metrics show a high index lookup and document load count. The docs talk about indexing and query operators, but I could not find clear guidance about how nested arrays affect index usage or whether a specific indexing path needs to be added for this scenario.

Has anyone seen this behavior before? Is there a recommended indexing policy or query pattern that works better for filtering inside arrays in Cosmos DB?

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.

0 comments No comments

Answer accepted by question author
  1. Manoj Kumar Boyini 11,440 Reputation points Microsoft External Staff Moderator
    2026-03-10T12:39:42.1433333+00:00

    Hi Vinod Pal,

    Thanks for sharing the details—querying nested arrays with ARRAY_CONTAINS can indeed get expensive if the index isn’t tuned exactly to your data shape. To better recommend an indexing policy or query pattern, could you share a bit more information?

    Follow-up questions:

    1. Can you post a small example of one of your documents (especially the items[] array), including its partition key path?
    2. What does your current indexing policy look like? Are you using the default “include all” policy, or have you overridden any include/exclude paths?
    3. What’s your partition key, and are you issuing a cross-partition query or targeting a single logical partition?
    4. How many elements do you typically have in items[]? Any idea of its average and maximum cardinality?
    5. Could you share the exact SQL query you’re running and the RU charge reported in the query metrics?
    6. From the query metrics, how many of the RUs are coming from “IndexLookup” vs. “DocumentLoad”?
    7. Have you tried adding a composite index on the array path or excluding other properties to reduce indexing overhead?
    8. Are you on provisioned or serverless (autoscale) throughput?

    With those details, we can:

    • Suggest a tailored indexing policy (for example, explicitly including the /items/* path or a composite index on /items/* plus your filter property).
    • Recommend query rewrites or pagination strategies that limit document scans.
    • Point you to best practices for indexing arrays in Cosmos DB.

    Reference docs:


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-06T18:11:21.6466667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The description of the problem is too general to identify a specific root cause. However, several common areas can be checked when Azure Cosmos DB “is not working correctly.” These steps focus on connectivity, performance, and client/trigger behavior.

    1. Check Azure Cosmos DB metrics in the portal
    • Open the Azure Cosmos DB account in the Azure portal and review Metrics.
    • Look for:
      • High rate of HTTP 429 (throttling) – indicates request rate too large.
      • Request failures or timeouts – helps distinguish client vs. service issues.
    • If metrics look healthy but the application still fails, the issue is likely on the client side.
    1. Verify client environment and resources
    • Ensure the application is running in the same Azure region as the Cosmos DB account when possible to reduce latency.
    • Monitor CPU and memory on the client (VM, App Service, Functions, etc.). High load can cause connectivity and availability issues.
    1. Check networking configuration
    • If the app runs in a VNet without public IP, verify SNAT limits and consider:
      • Adding a service endpoint for Azure Cosmos DB to the subnet.
      • Or assigning a public IP to the VM.
    • If a firewall is configured on Cosmos DB, ensure:
      • Required public IPs or subnets are allowed.
      • If service endpoints are enabled, add the subnet via Virtual Network ACLs.
    • If HTTP 407 errors appear, verify proxy configuration and set CosmosClientOptions.WebProxy correctly if a proxy is required.
    1. Use a single Cosmos DB client instance
    • Ensure the application uses one shared client instance (singleton pattern) instead of creating a new client per operation. Multiple clients can cause socket exhaustion and connectivity issues.
    1. If using Azure Functions with Cosmos DB trigger
    • Make sure the Cosmos DB Functions extension (Microsoft.Azure.WebJobs.Extensions.CosmosDB) is up to date.
    • Enable trigger-specific logs and review them for errors.
    • Be aware that:
      • The same batch of changes can be delivered multiple times if retries occur or leases are rebalanced.
      • Multiple events for the same item are expected if the item is updated frequently; use _lsn to distinguish versions.
    • If changes appear “missing,” first verify what the Function actually receives (ingestion point), not only what appears in downstream systems.
    1. If using the Azure Cosmos DB .NET SDK
    • Follow any aka.ms links in exception messages.
    • If seeing high latency, use the latency troubleshooting guide referenced in the SDK docs.
    • If the error mentions Microsoft.Azure.Cosmos.ServiceInterop.dll on Windows, upgrade to the latest Windows version.
    1. If using the Azure Cosmos DB Emulator
    • If the emulator misbehaves after an update:
      • Try Reset Data from the emulator tray icon.
      • If that fails, uninstall the emulator (and older versions), delete %ProgramFiles%\Azure Cosmos DB Emulator and %LOCALAPPDATA%\CosmosDBEmulator, then reinstall.
      • If the emulator stops responding, collect dumps from %LOCALAPPDATA%\CrashDumps and open a support ticket.
      • If Microsoft.Azure.Cosmos.ComputeServiceStartupEntryPoint.exe hangs, run lodctr /R to repair performance counters.

    If more detailed help is needed, provide:

    • Exact error messages or HTTP status codes.
    • SDK/language and hosting environment (Functions, VM, App Service, emulator, etc.).
    • Whether the issue is connectivity, performance, or data/trigger behavior.

    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.