I'm setting up the server side of a Google Cloud Messaging mechanism, using MySQL to store the registration ids provided by the mobile app. Giving that Google can issue up to 4k registration ids, I'm forced to store them in a TEXT field. All good so far, the problem is that I have to handle situations like this:
So the problem is that I have to ensure a uniqueness for the registration id in the database but I cannot add a UNIQUE index for that TEXT field.
Possible solutions that I could think of:
I'm sure that I'm not the only one facing that problem, but I can't find good solutions out there. Any thoughts on how can I solve this?
For storing the registration ID itself it's better to use VARBINARY(4096) column. It's more efficient than TEXT if you encode the registration ID with an efficient character set (such as UTF-8).
For efficient search, you should still have an additional indexed hash column (BINARY(32)) - we use the SHA-256
digest algorithm to get the 32-bytes hash from the registration ID. The hash column doesn't have to be unique. Collisions should be very rare, and even if they occur, your query will give you a small number of registration IDs which share the same hash, so it won't hurt performance to test in your Java code which one of them (if any) actually matches the registration ID you are looking for.
If you choose to store a unique device ID and search based on it, I suggest you assign your own identifier to each device. That identifier can be (for example) BIGINT (long in java). You can require that the application to call your server to get a unique identifier when it is first launched. You can store it on external storage of the device, so that a device where the app is uninstalled and then re-installed will still have the same identifier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With