Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple documents in CouchDB

I've a "best practice" question on CouchDB (actually I'm using TouchDB a CouchDB port to iOS), when using CouchCocoa framework.

I need to delete a bunch of documents that I get via a query. I know 3 ways to do this:

1) put all the documents into an NSArray, then use [CouchDatabase deleteDocuments:]

2) foreach query rows call the delete method, like: for (CouchQueryRow* row in query.rows) [row.document DELETE];

3) create a query that emit the _id, _rev properties and add the _deleted property, then use the bulk update, like: [couchDatabase putChanges:]

What's the better performance-wise? There's a better way to do it?

like image 638
il Malvagio Dottor Prosciutto Avatar asked Nov 14 '22 04:11

il Malvagio Dottor Prosciutto


1 Answers

At the HTTP API level, the fastest way to achieve this is to run a single batch request that provides the _id and current _rev of all documents to be removed.

Your job is to make sure that CouchCocoa actually does this — I know that CouchCocoa will try to cache the _rev of documents it reads, so if you are deleting documents that have just been read, [CouchDatabase deleteDocuments:] should be enough, otherwise you will have to [CouchDatabase getDocumentsWithIDs:] first.

If your documents are very large, it might become better to get the _rev using a view instead of a bulk fetch. This forces you to use [CouchDatabase putChanges:] to perform the bulk deletion. I don't know where the document size threshold lies, so you will have to benchmark this one.

Of course, you also need to decide what happens when a conflict occurs.

like image 193
Victor Nicollet Avatar answered Dec 15 '22 01:12

Victor Nicollet