Thursday, 18 May 2017

The most efficient way of obtaining multiple averages and counts from rethinkdb

I am creating a web application which collects information from a form, inserts them (e.g. age, height, and weight) into a rethinkdb database, and then displays simple descriptive statistics via socket.io.

Currently, I'm using r.expr() to run multiple queries, e.g.

r.expr({
mean1: r.db(…).table(…).avg(…),
mean2: r.db(…).table(…).group(…).avg(…).mul(10).round().div(10).ungroup(),
count: r.db(…).table(…).count()
}).run(conn, (err, res) => {
...

to construct an object which contains the counts and averages I'm interested in and then use io.emit() to send them to the client for display, which is updated every time a new entry is added.

This doesn't look very efficient to me and I'm guessing there is a better way of doing this—perhaps with just one query—but I'm very inexperienced with ReQL and query languages in general.

Moreover, is there a better way of doing rounding with arbitrary precision or more complex numerical operations after grouping than like I did above?



via Harold Cavendish

No comments:

Post a Comment