Querying the Cosmos DB Change Feed using SQL queries - azure-cosmosdb

I need to access Cosmos DB data through a middleware API that gives access to SQL queries but not the change feed (i.e. DocumentClient.CreateDocumentQuery() but not DocumentClient.CreateDocumentChangeFeedQuery()). Is it possible to query the change feed using regular SQL queries?
I was thinking about filtering documents on recent _ts but I am not sure timestamps are guaranteed to be monotonically increasing across entire collections due to potential clock drift across the VMs Cosmos DB runs on.

You cannot query the Change Feed using a SQL query. The Change Feed contains documents that have been inserted / updated, and any filtering needs to be done client-side after receiving such changes.

Related

ReadFeeds vs Query vs Read in CosmosDB

I would like to verify if my understanding is correct about OperationType in Cosmos DB (Cassandra API), as I cannot find a good explanation in the documentation.
Basically I have run a few different cases on Cosmos DB, and I see that when I query data using partition key, then only ReadFeed is used. But when I am not using partition key, then OperationType "Query" is used. It means that apparently in the first case it doesn't use the query engine and goes directly to the storage and in the second case Query Engine is used. Does it sound right?
Read is when you Get a Document
The Get Document operation retrieves a document by its partition key and document key.
ReadFeed is when you List Documents
Performing a GET on the documents resource of a particular collection, i.e. the docs URI path, returns a list of documents under the collection. ReadFeed can be used to retrieve all documents, or just the incremental changes to documents within the collection.
Query is when you Query Documents (Search)
You can query arbitrary json documents in a collection by performing a post against the “colls” resource in Cosmos DB.
ReadFeed or change feed is an internal feature of Cosmos DB. Other DBs maintain a feed of changes as well. They may expose it and you can interact with it in the ways they make available. They will use the feed of changes internally for many things. Cosmos DB are probably able to satisfy your query when you filter by partition using the ReedFeed.

cosmos db support different schema in a collection?

I am trying to write a record with a different schema to an existing collection with records . I don't get a exception, but i don't see the new record.
Do I need to use a different collection?
DocumentDBRepository<ScheduleViewModel>.CreateItemAsync(task).GetAwaiter();
Cosmos DB doesn't care about what you put into to it (as almost any other nosql db), so this is supported from the Cosmos DB perspective. from the code perspective, I suppose you need to create a connection that would support the model your are using and create a document

is there any query profiler in cosmos db?

I'm a newbie at cosmos db. and i'll be glad if someone help me to find how to catch queries that are going on local emulator. the reason why i'm asking is that now query string is appended in a few steps via linq and i need to be sure that query builder returns correct query string.
thanks in advance!
By the sound of it you are using the C# SDK and LINQ to query your database.
Once your LINQ query is ready you should be doing queryable.AsDocumentQuery() to generate a DocumentQuery object and the use that to do while(documentQuery.HasMoreResults) documentQuery.ExecuteNextAsync<yourtype>().
If that's not the case then you are using the SDK in an non optimal way.
All you need to do to get the generated SQL query is to do a .ToString() on the documentQuery object and you gonna get the translated query back.
In order to see the interactions between your application and Cosmos DB database, you can use Cosmos DB Profiler tool. It shows queries that are sent to Cosmos DB and stacktrace of relevant code that generated them.
The profiler shows statistics for each operation like duration, response status code, query execution metrics etc and also the request units for each request to a database what allows to optimize query costs.
It also alerts users about common pitfalls when using Cosmos DB and recommends how to resolve them.
Available reports provided by the profiler allow to analyze Cosmos DB usage by an application.

Getting the size of each partition in a CosmosDB collection

Is it possible to get the size of every partition in a Cosmos DB collection? I know the portal will show the top few partitions in a collection, in the Metrics blade, but I'm interesting in seeing the size of every partition.
I believe you should be able to get this data through the Cosmos DB REST API.
It doesn't seem to be exposed through the .NET SDK so you'd need to write some C# or PowerShell yourself to access the data however, it should be available.
Link is:
https://learn.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/collectionpartition/listusages

Application data views in Azure Cosmos DB

Our application uses Cosmos DB to store data. The DB is partitioned based on User ID and all backend services work fine using the User ID.
However, we have a UI where only admins have access and they are different from the users stored in DB. They fetch data from DB based on time. That is, get reports from DB for last 3 days (doesn't matter what user ID). In this case, the query needs to fan out to all partitions. Moreover, stored procedures are scoped per partition and cannot be used in this scenario. Even though we are able to fetch data through query, it's a huge performance hit.
Can anybody please advise if there is a way in Cosmos DB to workaround this?

Resources