Performance gains to using UUID instead of its string representation as _id on MongoDB?

三世轮回 提交于 2019-12-10 18:54:46

问题


In REST applications, I need to convert UUID string representation (received in json) to UUID Object to store it on MongoDB's _id field. I'm doing that as I heard there is a performance gain in the lookup/insertion time when using UUID vs String.

Is there really a performance gain when using UUID as _id on MongoDB instead of its string representation? (even small performance gains can have a big impact for me)

PS: I saw this post that says performance is better for ObjectID (not exactly the same, not sure it applies to UUID), but the only reason seems to be the potential smaller size of ObjectID vs String.


回答1:


I am using PyMongo which encode UUID as a BSON::Binary.

We can see from the MongoDB doc that:

For a more efficient storage of the UUID values in the collection and in the _id index, store the UUID as a value of the BSON BinData type. Index keys that are of the BinData type are more efficiently stored in the index if: the binary subtype value is in the range of 0-7 or 128-135, and the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.

My UUID is in that range, so the performance will be increased over the String representation.



来源:https://stackoverflow.com/questions/46327072/performance-gains-to-using-uuid-instead-of-its-string-representation-as-id-on-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!