0

Let's say that I need to maintain an index on a table where multiple documents can relate do the same item_id (not primary key of course).

Can one secondary compound index based on the result of a function which of any item_id returns the most recent document based on a condition, update itself whenever a newer document gets inserted?

This table already holds 1.2 million documents in just 25 days, so it's a big-data case here as it will keep growing and must always keep the old records to build whatever pivots needed over the years.

DevLounge
  • 8,313
  • 3
  • 31
  • 44

1 Answers1

0

I'm not 100% sure I understand the question, but if you have a secondary index and insert a new document or change an old document, the document will be in the correct place in the index once the write completes. So if you had a secondary index on a timestamp, you could write r.table('items').orderBy(index: r.desc('timestamp')).limit(n) to get the most recent n documents (and you could also subscribe to changes on that).

mlucy
  • 5,249
  • 1
  • 17
  • 21
  • Or max(index=r.desc('timestamp')) – DevLounge Jan 12 '16 at 18:06
  • But here the max must be the max timestamp for each unique item_id (multiple docs with same item_id but must return always the most recent) – DevLounge Jan 12 '16 at 18:07
  • Ah, OK, so you want to write a query that returns the most recent row for *every* `item_id`? If the number of `item_id`s is small enough that you can return them all at once, you could use `group` for that. Otherwise I don't think that's easily accomplishable right now. – mlucy Jan 12 '16 at 18:19