Kludge
Kludge

Reputation: 2835

RethinkDB: Can I group by fields between dates efficiently?

I'd like to group by multiple fields, between two timestamps. I tried something like:

r.table('my_table').between(r.time(2015, 1, 1, 'Z'), r.now(), {index: "timestamp"}).group("field_a", "field_b").count()

Which takes a lot of time since my table is pretty big. I started thinking about using index in the 'group' part of the query, then I remembered it's impossible to use more than one index in the same rql.

Can I achieve what I need efficiently?

Upvotes: 1

Views: 317

Answers (1)

mlucy
mlucy

Reputation: 5289

You could create a compound index, and then efficiently compute the count for any of the groups without computing all of them:

r.table('my_table').indexCreate('compound', function(row) {
  return [row('field_a'), row('field_b'), row('timestamp')];
})
r.table('my_table').between(
  [field_a_val, field_b_val, r.time(2015, 1, 1, 'Z')],
  [field_a_val, field_b_val, r.now]
).count()

Upvotes: 1

Related Questions