Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is partition key needed in queries even though JSON is indexed

I'm planning on using Cosmos Db (Document Db) and I'm trying to understand how the queries, indexing and partitions relate to each other.

How to partition and scale in Azure Cosmos Db talks about the partition key and other documentation indicates that partition key + id = unique id for the document. But then SQL Query and SQL syntax in Azure Cosmos Db says it provides automatic indexing of JSON documents without requiring explicit schema or creation of secondary indexes.

I understand that partition key is important for scalability and how data is stored. But if we think about searching is the partition key kind of like extra filter/where clause? All the documents are indexed so I can execute query like:

SELECT * 
FROM Families
WHERE Families.address.state = "NY"

Should I still specify the partition key or indicate some how that cross partition queries are allowed when using this SQL query syntax?

like image 735
Toni Parviainen Avatar asked May 23 '17 15:05

Toni Parviainen


People also ask

What is the use of partition key in Cosmos DB?

Azure Cosmos DB hashes the partition key value of an item. The hashed result determines the physical partition. Then, Azure Cosmos DB allocates the key space of partition key hashes evenly across the physical partitions.

Which of the below correctly lists the two components of a partition key?

A partition key has two components: partition key path and the partition key value.

What is cross partition query?

Cross-partition query Each physical partition has its own index. Therefore, when you run a cross-partition query on a container, you are effectively running one query per physical partition. Azure Cosmos DB will automatically aggregate results across different physical partitions.


Video Answer


1 Answers

Your first link gives the answer for this:

For partitioned collections, you can use PartitionKey to run the query against a single partition (though Cosmos DB can automatically extract this from the query text), and EnableCrossPartitionQuery to run queries that may need to be run against multiple partitions.

So, yes, you either need to specify the WHERE clause which will make query run against a single partition, or set EnableCrossPartitionQuery to true in query options.

like image 104
Mikhail Shilkov Avatar answered Oct 10 '22 18:10

Mikhail Shilkov