Last Updated: February 25, 2016
·
1.995K
· javierg

Reduce view indexing time for couchdb

When first started using couchdb It was great to be able to use JS for generating the indexed views for our DB. The our Db grew a lot, and re-indexing after a large update was causing timeouts from the app side. So I tried this: change the views into native Erlang.

JS/Coffee View:

(d)->
  emit d.type.toLowerCase(), d._id if d.type

This view in a 11845 documents database was taking 33+ secs.

Erlang ported will take 3~ secs.

fun({Doc}) ->
  Id = proplists:get_value(<<"_id">>, Doc, null),
  NameSpace = binary:bin_to_list(Id),
  case re:split(NameSpace, "[/]") of
     [_] -> null;
     [T, BID] -> Emit(T, BID)
   end
end.

To be able to do this I follow couchdb wiki http://wiki.apache.org/couchdb/EnableErlangViews

Which in summary is.

Stop couchdb, change your config to add:

[native_query_servers]
erlang = {couch_native_process, start_link, []}

Restart couchdb.Check the temporary view on Futon, in the erlang will be on languages dropdown options.

Now my TODo is to setup couch to be able to use Elixir language.