How fast is it to look up by ObjectId in Mongodb?
I'm looki开发者_JAVA百科ng up ObjectIds everywhere, as if they're cake.
Is this OK? _id
fields are supposed to be looked up like crazy, right?
A more precise answer: MongoDB uses B-Tree indexes. Searching for a particular value in a B-Tree has O(log n) complexity in the average and worst case, which can be considered reasonably fast (i.e. a binary search). It is not constant complexity = O(1) though, so you still might have some slowdown effects if the index size grows larger than available RAM. (MongoDB tries to keep the indexes in RAM, and every IO needed to look up an index on disk will slow down your query considerably).
_id is the primary key. It's indexed. Of course it's fast.
ObjectIds
, if your primary method of data access, will be the fastest way to retrieve your stuff from MongoDB. We utilize our MongoDB as a keyed repository for most of our data access. You'll have great results doing what you're doing.
Index on _id field is auto created by mongo and default primary key. Speedwise, it will be super fast to access docs by _id field.
What concerns do you have?
精彩评论