GoneCase123
GoneCase123

Reputation: 442

cosmos bulk read items query

Is it possible to make bulk query in cosmos db ?

I have a list of item_ids, is it possible to get output in one query? currently I am using loop which takes lot of time, because iIhave 5K to 20K IDs in one list, to speed up process time, any other solution can we use instead of using threads?

Example code that is using loop:

def read_item(self):
         for item_id in self.ids():
            response = self.container.read_item(item=item_id, partition_key=item_id)

Cloud: Azure DB: Cosmos DB API: SQL Lang: Python 3.x

Upvotes: 0

Views: 454

Answers (1)

frisko
frisko

Reputation: 867

You can use .query_items() method to return all the items with the specified IDs:

import json

def read_item(self):
    id_list_str = json.dumps(self.ids())
    response = self.container.query_items(
        f"SELECT * FROM c WHERE ARRAY_CONTAINS({id_list_str}, c.id)",
        enable_cross_partition_query=True
    )

Upvotes: 1

Related Questions