Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure CosmosDB IS_DEFINED vs NOT IS_DEFINED

I was trying to query a collection, which had few documents. Some of the collections had "Exception" property, where some don't have.
My end query looks some thing like:

Records that do not contain Exception: 

**select COUNT(1) from doc c WHERE NOT IS_DEFINED(c.Exception)**  


Records that contain Exception: 

**select COUNT(1) from doc c WHERE IS_DEFINED(c.Exception)**

But this seems not be working. When NOT IS_DEFINED is returning some count, IS_DEFINED is returning 0 records, where it actually had data.

My data looks something like (some documents can contain Exception property & others don't):

[{
  'Name': 'Sagar',
  'Age': 26,
  'Exception: 'Object reference not set to an instance of the object', ...
},
{
  'Name': 'Sagar',
  'Age': 26, ...
}]
like image 600
Vidhya Sagar Reddy Avatar asked Dec 08 '17 15:12

Vidhya Sagar Reddy


People also ask

Should I use Cosmosdb?

Azure Cosmos DB is a global distributed, multi-model database that is used in a wide range of applications and use cases. It is a good choice for any serverless application that needs low order-of-millisecond response times, and needs to scale rapidly and globally.

Which Azure Cosmos DB API should you use?

API for Apache Cassandra This API for Cassandra is wire protocol compatible with native Apache Cassandra. You should consider API for Cassandra if you want to benefit from the elasticity and fully managed nature of Azure Cosmos DB and still use most of the native Apache Cassandra features, tools, and ecosystem.

What is the downside for setting strong consistency on a Cosmos DB database with a lot of replicas?

Additionally, there are no write latency benefits on using strong consistency with multiple write regions because a write to any region must be replicated and committed to all configured regions within the account. This results in the same write latency as a single write region account.

Is Cosmosdb fast?

Azure Cosmos DB is a fast and flexible distributed database that scales seamlessly with guaranteed latency and throughput. You don't have to make major architecture changes or write complex code to scale your database with Azure Cosmos DB.


1 Answers

Update

As Dax Fohl said in an answer NOT IS_DEFINED is implemented now. See the the cosmos dev blob April updates for more details.

To use it properly the queried property should be added to the index of the collection.

Excerpt from the blog post:

Queries with inequality filters or filters on undefined values can now be run more efficiently. Previously, these filters did not utilize the index. When executing a query, Azure Cosmos DB would first evaluate other less expensive filters (such as =, >, or <) in the query. If there were inequality filters or filters on undefined values remaining, the query engine would be required to load each of these documents. Since inequality filters and filters on undefined values now utilize the index, we can avoid loading these documents and see a significant improvement in RU charge.

Here’s a full list of query filters with improvements:

  • Inequality comparison expression (e.g. c.age != 4)
  • NOT IN expression (e.g. c.name NOT IN (‘Luis’, ‘Andrew’, ‘Deborah’))
  • NOT IsDefined
  • Is expressions (e.g. NOT IsDefined(c.age), NOT IsString(c.name))
  • Coalesce operator expression (e.g. (c.name ?? ‘N/A’) = ‘Thomas’)
  • Ternary operator expression (e.g. c.name = null ? ‘N/A’ : c.name)

If you have queries with these filters, you should add an index for the relevant properties.

like image 156
Reitffunk Avatar answered Jan 02 '23 06:01

Reitffunk