composite index

Yang Chow Mun 121 Reputation points
2023-08-09T12:08:56.3466667+00:00

I am working on composite index for cosmos db and have a few doubts on it.

Let said my composite index is


            {
                "path": "/id",
                "order": "ascending"
            },
            {
                "path": "/year",
                "order": "ascending"
            },
            {
                "path": "/date",
                "order": "descending"
            }


a. if my query is " Select * from database c where c.id='a' and c.year=2023 and c.month=8 and c.date=9", does the composite index help?

b. if my query is " Select * from database c where c.id='a' and c.year=2023 and c.date=8 and c.time=1", does the composite index help?

c. if my query is " Select * from database c where c.year=2023 and c.id='a' and c.date=8, does the composite index help?

d. if my query is " Select * from database c where c.id='a' and c.date=8 and c.year>2022 and c.year<2025, does the composite index help?

Appreciate your advice! thanks

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,843 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Oury Ba-MSFT 20,716 Reputation points Microsoft Employee
    2023-08-09T18:02:29.0266667+00:00

    @Yang Chow Mun Thank you for reaching out.

    Yes, the composite index will help with query a, c and d. But for b, the composite index might not help because the query has a property time which is not included in the composite index. The composite index only includes id, year and date.

    Please learn more with the below links.

    Composite indexed policy examples

    Composite index

    New ways to use composite indexes in Azure Cosmos DB

    Hope that helps.

    Regards,

    Oury


Your answer

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