Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CosmosDB: How does SELECT TOP work?

I have a 450GB database... with millions of records.

Here is an example query:

SELECT TOP 1 * FROM c WHERE c.type='water';

To speed up our queries, I thought about just taking the first one but we have noticed that the query still takes quite a while, despite the very first record in the database matching our constraints.

So, my question is, how does the SELECT TOP 1 really work? Does it:

A) Select ALL records and then return just the first (top) one where type='water'

B) Return the first record which is encountered where type='water'

like image 672
pookie Avatar asked Jul 18 '17 15:07

pookie


People also ask

How does Cosmos DB Upsert work?

With an upsert operation we can either insert or update an existing record at the same time. To compare to the existing records, upsert statement use key column(s) to determine if this is a new or existing record. We will use Azure SQL database as a source and CosmosDB as a sink.

How do you write a select query in Cosmos database?

In the Azure Cosmos DB blade, locate and select the Data Explorer link on the left side of the blade. In the Data Explorer section, expand the NutritionDatabase database node and then expand the FoodCollection container node. Within the FoodCollection node, select the Items link. View the items within the container.

Is Cosmos DB any good?

Cosmos DB is a very good modern platform for cloud native and mobile applications. I work with different teams which are using Cosmos DB. I found that the service is excellent in some areas like scalability, DR, availability.


3 Answers

Try this line, noting the offset limit:

SELECT * FROM c WHERE c.type='water' OFFSET 0 LIMIT 1

For more information about the offset limit:
https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-offset-limit

like image 50
Arjun Bhalodiya Avatar answered Sep 23 '22 11:09

Arjun Bhalodiya


Cosmos db Explorer doesn't work with the TOP Command, It's an existing issue. It works fine in SDK Call.

Check some Top command usage below

https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-subquery

like image 26
Arpan Saini Avatar answered Sep 23 '22 11:09

Arpan Saini


Assuming you aren't sorting your results (which you query isn't) then TOP 1 will return the first result as soon as it finds one. This should then end the query.

like image 40
Jacobm001 Avatar answered Sep 22 '22 11:09

Jacobm001