Pregunta

I am currently migrating from couchdb to mongodb, still learning this stuff and have a problem anytime I do some queries with sorting in my webpage. I am using codeigniter as the framework and using alexbilbie lib for mongodb php library.

So, here is my problem: I intend to do queries from sensors in a room that updated each second (thus, already saved thousands docs in collection) and to get each latest sensor value I use this query from model:

function mongoGetDocLatestDoc($sensorid){
    $doc = $this->mongo_db->where(array('SensorId'=>$sensorid))->limit(1)->order_by(array('_id'=>'DESC'))->get('my_mongo');
    return $doc;

}

if I called this with my controller, it took a lot of time to process the query and even worse if I change the sort by timestamp. and it is double the latency each time I called this again for the second sensor, let alone I have more than 10 sensors that need this query in the same page. Am I doing it wrong or there is some more efficient way to get the latest data from collection?

edit: @Sammaye : I tried making an index based on your suggestion and here is the explain generated after I executed the query:

"cursor" : "BtreeCursor timestamp_desc",
    "nscanned" : 326678,
    "nscannedObjects" : 326678,
    "n" : 50,
    "millis" : 4402,
    "nYields" : 7,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
            "timestamp" : [
                    [
                            {
                                    "$maxElement" : 1
                            },
                            {
                                    "$minElement" : 1
                            }
                    ]
            ]
    }

as per comparison, this explain the first query without using index (that executed faster) :

"cursor" : "BasicCursor",
    "nscanned" : 385517,
    "nscannedObjects" : 385517,
    "n" : 50,
    "millis" : 1138,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {

    }
¿Fue útil?

Solución

Okay thank you all for your great response :) Here is my finding after trying with indexes:

  1. Update the mongodb to the latest version. I found this slightly improve my query. I am updating from default 2.0.3 that provided as default in ubuntu 12.04.02 to mongodb version 2.4.3
  2. Build indexes/compound indexes based exactly on how you mostly needed in your query. as an example, in my question, my query based on SensorId and _id: DESCENDING so the best strategy to optimize my query would be something like : db.your_collection.ensureIndex({ "SourceId" : 1, "_id" : -1 },{ "name" : "sourceid_idx", "background" : true }); or in other case if I needed it based on timestamp:

    db.your_collection.ensureIndex({ "SourceId" : 1, "timestamp" : -1 },{ "name" : "source_idx", "background" : true });

I found a very good explanation about mongodb indexes here

Hope this will help other people that stumbled upon similar problem...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top