Performance gains to using UUID instead of its str

2019-07-07 07:52发布

问题:

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.