none
Azure-CosmosDB-MongoDBAPI RRS feed

  • Question

  • We have following Collection Raw_Readings,Down_Sampled

    Raw_Readings ---> It will save our all sensor data, Every minute.

    Down_Sampled  ----> It will save our all sensor Min/Max value , Every hour.

     

    As per our debug retrieve last 200 records. our Fixed throughput 2500RU

     

    if we run below query Ascending(sort:1) order : Results in : 3 Seconds ---> Usage 50RU

    if we run below query Descending(sort:-1) order : Results in : 23 to 35 Seconds ---> Usage 2382RU

     

    public function get_logdata_by_device($device_id, $start, $limit, $start_date, $end_date) {
            $filter = array('DID' => $device_id, 'DT' => ['$gte' => $start_date, '$lte' => $end_date]);
            $options = [
                'projection' => [
                    'DT' => 1,
                    'RD' => 1,
                    'DV' => 1,
                    'EV' => 1
                ],
                'skip' => (int) $start,
                'limit' => 200,
                'maxTimeMS' => MAXTIMEOUT,
                'sort' => ['DT' => -1]];
            $query = new MongoDB\Driver\Query($filter, $options);
            $field_rows = $this->m->executeQuery(DATABASE_NAME . "." . RAW_READINGS_COLLECTION, $query);
            return $field_rows;

    In descending order consume more RU and time consume . Please let me know how to reduce processing time .

    1. How can I see if a query uses an index?

    2. How to check whether a certain field is indexed in mongodb or not?

    3. If i create index in collection by query it will apply to old data or only new data?

    Thursday, October 17, 2019 1:51 PM

Answers

  • Hi Prabu,

    To answer your specific questions, the following information taken from Indexing using Azure Cosmos DB's API for MongoDB should provide clarity around indexes. 

    "Azure Cosmos DB's API for MongoDB leverages automatic index management capabilities of Cosmos DB. As a result, users have access to the default indexing policies of Cosmos DB. So, if no indexes have been defined by the user, or no indexes have been dropped, then all fields will be automatically indexed by default when inserted into a collection. For most scenarios, we recommend using the default indexing policy set on the account."

    As for Creating unique indexes, these can only be created when the collection is empty so the question about new data or old data would not be an issue. If you did not create any unique indexes and if the default indexing policy is used and not modified, every field will be automatically indexed. 

    If you created the collection and started populating it with data, you likely have the default indexing applied. Unique indexes are useful for enforcing that no two or more documents contain the same value for the indexed field(s).

    As for the sort order results, can you specify a value to sort by? Please see the MongoDB cursor.sort() documentation. See also: Sort with a Single Field Index

    db.records.find().sort( { a: -1 } )

    Again, if you haven't changed/modified the default indexing policy, it is likely that all fields are indexed and you just need to pass in value from the Json that you want to sort on.

    Friday, October 18, 2019 11:38 PM
    Moderator

All replies

  • Hi Prabu,

    To answer your specific questions, the following information taken from Indexing using Azure Cosmos DB's API for MongoDB should provide clarity around indexes. 

    "Azure Cosmos DB's API for MongoDB leverages automatic index management capabilities of Cosmos DB. As a result, users have access to the default indexing policies of Cosmos DB. So, if no indexes have been defined by the user, or no indexes have been dropped, then all fields will be automatically indexed by default when inserted into a collection. For most scenarios, we recommend using the default indexing policy set on the account."

    As for Creating unique indexes, these can only be created when the collection is empty so the question about new data or old data would not be an issue. If you did not create any unique indexes and if the default indexing policy is used and not modified, every field will be automatically indexed. 

    If you created the collection and started populating it with data, you likely have the default indexing applied. Unique indexes are useful for enforcing that no two or more documents contain the same value for the indexed field(s).

    As for the sort order results, can you specify a value to sort by? Please see the MongoDB cursor.sort() documentation. See also: Sort with a Single Field Index

    db.records.find().sort( { a: -1 } )

    Again, if you haven't changed/modified the default indexing policy, it is likely that all fields are indexed and you just need to pass in value from the Json that you want to sort on.

    Friday, October 18, 2019 11:38 PM
    Moderator
  • If you are still having issues with the descending sort, we can make a deeper investigation of the issue. 

    Thank you,

    Mike

    Saturday, October 19, 2019 12:35 AM
    Moderator
  • Hello Mike ,

    Thanks for support.

    Regards

    Prabu.R

    Wednesday, October 23, 2019 4:47 AM