1

I have the need to store modems and messages and associate them so that first they are just saved as new, not associated to any modem, when it comes to routing each message I have to first query which connected modem has sent the most messages to the number of the message I want to route. I need to order desc so that I get the one which has more messages, at this moment I don’t care if the messages are assigned, or sent (status) so I just look for all of the messages of the number that each modem has sent and send the message to the one which has the higher ammout. If I find that none of them has messages with that number I need to make another query, the modem which has the least 'assigned' messages then send to the one which has the least assigned messages. (edited)

can anybody please help me model the documents so that I can get the most speed out of the queries?

right now I have it like this:

modem
    {
    id:rethink internal,
    imei:primary,
    connected:boolean,
    messages:array of messages ids
    .. other less important args..
}

messages:
{
id:rethinkdb internan,
id_sms: I bring it from another system, I use it as primary,
message:the body,
status:new/assigned/sent
}

when I try the queries I that I have it will take up to a second to route a message, that is too long for what I need.

these are the queries I am using

bring the modems which has the most messages with the number of the message:

r.table('modems').getAll(true,{index:'connected'})
  .map(function(modem) {
    return {imei: modem('imei'), 
            count: r.table('messages')
            .getAll(r.args(modem('messages')))('number').count(msg.sms.number)}})
  .orderBy(r.desc('count'))
  .limit(1)

the modem which has the least 'assigned' messages

r.table('modems').getAll(true,{index:'connected'})
    .map(function(modem) {
        return {
            imei: modem('imei'), 
            count:  r.table('messages')
                    .getAll(r.args(modem('messages')))('status')
                    .count('asigned')
        }
    })
.orderBy(r.asc('count'))
.limit(1)

there must be a better way, I don't think rethinkdb is so slow even using indexes.

I have 3 modems and 19k messages at the moment. the first query takes around 400ms, and the second one more than 500.

Jose Andradez
  • 37
  • 2
  • 9

0 Answers0