Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better - auto-generated id or manual id assignment in couchdb documents?

Tags:

couchdb

Should I be generating the id of the documents in a CouchDB or should I depend on CouchDB to generate it? What are the advantages or disadvantages in these approaches? Is there any performance implications on any of these options?

like image 742
Madhusuthanan Seetharam Avatar asked Jan 07 '12 16:01

Madhusuthanan Seetharam


3 Answers

There is no difference as far as CouchDB is concerned. Frederick is right that sequential ids are slightly faster. If you query /_uuids?count=10 you will notice that the UUIDs are sequential (by default).

However, even with random IDs, once you run compaction, they will all be in the "right" order internally in the .couch file and at that point there is no difference. So in the long run, I don't usually worry about it.

like image 150
JasonSmith Avatar answered Nov 11 '22 20:11

JasonSmith


The main thing is that you should use mostly sequential ids. As this article and this bit of the couchdb book explain, using random ids results in a much less efficient structure internally, both speed wise and in terms of space used on disc.

like image 40
Frederick Cheung Avatar answered Nov 11 '22 19:11

Frederick Cheung


Self generated ids are almost impossible to deal with if you have two or more separated instances of your app. Because the synchronisation between the different instances is not instantaneous. A solution for this can be to have one server dedicated to generate (or check the availability of) the ids, for example using a SQL database, and acting as a gate for document creation.

On the other hand, if you have only one server and will never need more, there is one advantage I find interesting to self generated uids: since they have to be unique, you can use them in urls. For instance take the slug of the title of a blog post as the _id.

Performance-wise, the CouchDB's generated ids are pretty long so if your own ids are shorter, you will save significant disk space (assuming you have a looot of documents).

like image 45
Simon Avatar answered Nov 11 '22 19:11

Simon