Has anyone found an efficient way to store BSON Ob

2019-02-25 04:39发布

问题:

We are migrating some data from MySQL to Mongo and have been adding extra fields to some of our DB tables for the ObjectId value of the migrated data objects.

At the moment we are storing them as varchar(24) latin1_general_ci which works fine.

However, for efficient storage/indexing we probably should convert back to the 12-byte binary value. Unfortunately, MySQL only seems to have native integer support up to 8 bytes. Is there another option - binary perhaps?

Update: I am migrating from MySQL but only certain tables at the moment. So I first make duplicates of the objects in Mongo of a certain table. Then I go back to any other tables left in MySQL that have foreign key references to that old MySQL table ID and add a new reference field that will store the Mongo object's ID. I will then remove the original reference field the BIGINT.

Update 2: The reason I bring this up is that it may be 12 months more (or never) before we move this other data, so it will have a performance impact on the live web application until then.

回答1:

The range of a 12-byte unsigned int would be 0..79228162514264337593543950335 (29 digits max); according to docs (same URL you cited), a DECIMAL/NUMERIC column packs each group of nine base10 digits into four bytes (adding up to floor(29 / 9) * 4 = 3 * 4 = 12 bytes for the first 27 digits), with the remaining 2 digits taking an additional byte, coming up to 13 bytes of storage for a DECIMAL(29) column.

Or, you can store them as BINARY(12).