Skip to content
wade.one

wade.one

wade womersley – york based software engineer

  • Home
  • 2013
  • June
  • 23
  • Cloudant’s Rather Awesome dbcopy for re-reducing the output of existing map/reduce data.

Cloudant’s Rather Awesome dbcopy for re-reducing the output of existing map/reduce data.

Posted on June 23, 2013June 23, 2013 By Wade No Comments on Cloudant’s Rather Awesome dbcopy for re-reducing the output of existing map/reduce data.
Programming

I love Cloudant, simple as, their implementation of CouchDB as a DDaaS (as opposed to DBaaS) has no real competitor – as CouchDB develop’s, that may, of course, become a problem as it gives dominance in a market, but for now, we’re good.

One of their best features is the dbcopy command, this copies the output of a map/reduce view to either the same database or another database entirely. Why would you want to do this when you can just create another view? Simple really, you already have the data you need and you don’t want to re-build a view across a huge dataset. The output of a map/reduce view often is a single number or statistics on those numbers. For CouchDB to iterate over large documents is significantly slower than iterating over numbers or the output of _stats – as an example, the rate at which a view was updated recently on our database took a week when I added dbcopy to it, vs near immediately for the view on the dbcopy database (based on a constant request to update the view as I have set up for our database).

Or to put it another way – imagine having two books: one is a book where each page is full with a story, another book summarises that page’s story with a bullet point – how much quicker can you read the second book? A LOT quicker! Yes the meat of the story is lost, but so what, you don’t want meat, you want analysis.

Right so, how is this done? Let’s use an example (I’ve abstracted this from a use case at Media Skunk Works, it may look a bit convoluted at times, but I can’t give away the real use case!)

Let’s imagine you have a database of users who search your site for certain keywords; during registration you ask for their country, so you have that information.

In your map, you emit, as the key: [country, user, keyword], so you can find all keywords for a user in a country, all users in a country and all countries, but what if you want to find all users who searched for a particular keyword ?

You could add a new view, but, if you add “dbcopy”: “user-stats” (for example, “user-stats” is the database to copy the reduce output to) to your view definition, you can use that new database to re-work that data.

So, in the “user-stats” database, add a view that does the following: “if(doc.key.length === 3) emit([doc.key[2], doc.key[1]], 1)”.
And voila, your new database now has a lookup that outputs [Keyword, User] = 1 for each document allowing you to re-group and re-stat data based on Keyword and User. The bonus is, that’s a huge performance gain. But not only that, you could also add a view that has “emit([doc.key[2], doc.key[0]], 1)” to see what user’s from each country lookup with minimal performance detriment – in fact 0 performance detriment to your primary database.

Share:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Post navigation

❮ Previous Post: Country/Currency List for C#, JSON, PHP and HTML select
Next Post: C# MySqlDataReader – “one line” function to return a row as an instance of a class. ❯

You may also like

Programming
Regular Expression (Regex) For Date part extraction/array split
February 5, 2012
Programming
Automating “Implemented methods” for a web based API
April 25, 2011
Programming
PHP FUNdamentals
September 1, 2009
Programming
Adobe Browser Labs – Better than Microsoft Expression
October 28, 2009

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Copyright © 2025 wade.one.

Theme: Oceanly News Dark by ScriptsTown