1

I have a table Items with ~20k records. I need to be able to filter on multiple tags (or) and maintain order of the items, sorted by sticky then created-date.

For example I may fetch items tagged with tagA, tagB or tagC etc. If an item is sticky (bool), it is sorted first, then the rest of the items are sorted by created-date.

Currently my query looks like this:

Item.orderBy({ index: r.desc('priority') }) // priority = doc => [doc('sticky'), doc('created').toEpochTime()]
    .filter(row => r.expr(['tagA', 'tagB','tagC']).setIntersection(row('tags')).count().ne(0))
    .slice(offset, offset + limit); // 0 to 10 - pagination

But I feel this performs quite a few reads. Is there a way to optimize it further?

I've tried using a multi-index on the tags, and the do regular sort without an index. This seemed to do fewer reads, but was much slower.

nifty
  • 45
  • 4
  • 1
    Using an index instead of `filter` is usually a good way to make queries faster. How did you build and query your multi-index for the tags? Another possible way to speed up your query is to [implement pagination using `between`](https://www.rethinkdb.com/docs/cookbook/javascript/#pagination) – Etienne Laurin Nov 14 '16 at 01:38

0 Answers0