Tim Griffiths
Tim Griffiths

Reputation: 994

Are Cosmos DB indexes range by default?

I have a cosmos db that's around 4gb which when it was small could perform date filters relatively quickly with a low RU value (3 - 15ish) but as the DB has grown to contain millions of records it now has slowed right down and the RU value is up in the thousands.

Looking at the documentation for date https://learn.microsoft.com/en-us/azure/cosmos-db/working-with-dates is says

To execute these queries efficiently, you must configure your collection for Range indexing on strings

However reading the linked index policy doc (https://learn.microsoft.com/en-us/azure/cosmos-db/index-policy) it sounds like by default every field has a range index created

The default indexing policy for newly created containers indexes every property of every item, enforcing range indexes for any string or number, and spatial indexes for any GeoJSON object of type Point

Do I need to configure the indexs to anything other than the default?

{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
    {
        "path": "/*"
    }
],
"excludedPaths": [
    {
        "path": "/\"_etag\"/?"
    }
]

}

Upvotes: 1

Views: 650

Answers (2)

4c74356b41
4c74356b41

Reputation: 72141

So if you are concerned with query costs indexing won't really help. Writes cost depend on indexing, not read. If you are seeing thousands of RU per request I suspect you are either using cross-partition queries or you are not having partitions at all (or a single partition for everything). To cut these costs down you need to either stop using cross-partition queries or (if the former is not possible) rearchitect your data in such a fashion that you do not need to use cross-partition queries.

I think range is the default index in cosmos db

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222657

When it comes to indexing you can see the best practices from here,

You should exclude unused paths from indexing for faster writes. You should leverage IndexingPolicy with IncludedPaths and ExcludedPaths

for ex:

var collection = new DocumentCollection { id = "excludedPathCollection"};
collection.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" });
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/nonIndexedContent/*");

Upvotes: 1

Related Questions