I have a notes app where a user creates notes and they get synced to a database with column noteId, which is an auto_incrementing field. The problem is that it gets a little complicated when the user creates a not when not connected to the internet, so I have to assign a temporary noteId, and do a bunch of other stuff related to the hierarchy of the note system structure.
So I’m thinking of dropping the auto_increment option all together and having the local device (iPhone/iPad – Objective C) create a unique ID (possibly a timestamp) that could be used in the database as the noteId. This way if the user is offline and reconnects at a later time, syncing would be as easy as sending the unique ID.
Two questions:
- How does this affect performance? Let’s say I have 500,000 notes in the database, and the user makes a change to one of his notes. With the auto_incrementing field I imagine it would be easier for the system to locate a noteId of 256,000, but with a general noteId system, would finding a noteId of 88689034 be a much lengthier process?
- What are good possibilities for generating a unique ID for a note locally on the user’s device? Time stamp?
One of the most common ways of generating unique IDs is to use UUID/GUIDs. MySQL has a UUID function, .NET has GUID functions, and it’s easy to write a function to create one in PHP or JavaScript… you don’t specify what language the client app is in. 😉 But yes, it can definitely affect performance. The best way to increase performance of UUIDs would be to convert the hex string into a binary number and store it across multiple INT columns.
EDIT: Just thought of a better idea… why do you even need to generate an ID at the device? Couldn’t you just create the note, then when the device has connection, send the note to the server and let the server generate an ID at that point? Maybe on the device you would generate a temp ID for the device to keep track of it, but the real ID would still be generated by the server.