开发者

Is remove an expensive operation in MongoDB?

Part of my site migration开发者_如何转开发 to MongoDB is migration of messaging system. I'm trying to figure out whether I should be actually removing the records from the collection or mark them as removed using some bool field, when the user wants to remove the message - the site will be high load so I'm concerned about performance (and not concerned about disk space).

Any ideas?


Edit: this answer is a historical artifact from 2011 and it refers to the old MMAPv1 engine which was removed in 2019. However, as the comments note, remove is a one time operation and will be always cheaper than always checking a flag so remove is still the best option regardless of the storage engine internals.

tl;dr: remove.

MongoDB stores the data in a double linked list and so removing results is adjusting two links, the next link of the previous document and the previous link of the next document. There is no autocompacting. Updating, if you have a value already stored, happens in place, changing one value. Now... you think, great, update one int instead of two pointers, surely faster! Not so -- you now need to index on this flag and creating indexes is "slow".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜