A Python service must fetch one product by its id from a Cosmos DB container partitioned by /category, as cheaply as possible. Which call fits?
- Exam domain
- Develop AI solutions by using Azure data management services25–30% of the exam
- Difficulty
- Medium
- In this mock
- 18 questions on this domainPractise this domain
Why the exam asks this
Objective in the exam guide: Connect to Azure Cosmos DB for NoSQL by using the SDK and run queries.
The SDK call you pick decides latency and cost. The exam checks you know the cheapest read.
Why D is the answer
container.read_item(item=id, partition_key=category)
A point read by id and partition key goes straight to the item in one partition and is the cheapest way to read a single item.
Remember: One item with known id and partition key: point read.
How it works
The SDK connects through a CosmosClient, then gets a database and container client.
read_item with the id and partition key value is a point read: the cheapest, fastest read.
query_items runs SQL-like queries; include the partition key to keep them in one partition, and use parameters for values.
Clues in the question
The words that point to the answer, and what each one tells you.
- One product by its id
- A single known item.
- Partitioned by /category
- Supply the partition key.
- As cheaply as possible
- Point read.
In detail
- Create one CosmosClient per application and reuse it.
- Use parameterized queries for anything that is not a point read.
Side by side with look-alikes
| Call | Partitions | Cost |
|---|---|---|
| read_item(id, partition_key) | One | Lowest |
| query_items with partition key | One | Low |
| Cross-partition query | All | Higher |
| SELECT * then filter in code | All | Highest |
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. A query SELECT * FROM c with filtering in PythonWrong
Reading everything costs RUs for every item.
B. container.query_items with enable_cross_partition_query=True and no partition keyWrongMost tempting
A cross-partition query fans out to every partition.
Why it tempts: It works and returns the right item. But it searches every partition to find one document.
C. Reading the whole container through the change feedWrong
The change feed is for processing changes, not lookups.
D. container.read_item(item=id, partition_key=category)Right
One item with known id and partition key: point read.
In practice
- Authenticate the client with DefaultAzureCredential and a data-plane role instead of the account key.
Use case
An illustrative scenario, not a real incident.
A product page loads in milliseconds with a point read, while a cross-partition query cost many times more RUs.
See how it works
flowchart LR C["CosmosClient(endpoint, credential)"] --> D["get_database_client(db)"] D --> K["get_container_client(products)"] K --> R["read_item(id, partition_key)"] K --> Q["query_items(query, parameters)"]
How to read this chart
- "CosmosClient(endpoint, credential)" leads to "get_database_client(db)".
- "get_database_client(db)" leads to "get_container_client(products)".
- "get_container_client(products)" leads to "read_item(id, partition_key)" and "query_items(query, parameters)".
Related questions
Learn more
- Our lesson: Data services for AI
- Practise Develop AI solutions by using Azure data management services questions in the mock exam
- Source: Quickstart: Azure SDK for Python with Azure Cosmos DB
- Source: Partitioning and horizontal scaling in Azure Cosmos DB
- Cosmos DB indexing policies
- Cosmos DB change feed
- pgvector on Azure PostgreSQL