0

我有以下查询可以正常工作但速度很慢,但是我无法弄清楚如何正确索引它:

r.db('my_db')
.table('messages')
.filter({ community_id : community.id})
.filter(function(row){
    return row('mentions').contains(user.id);
})
.filter(function(row){
    return row('channels').contains(channel.id);
})
.orderBy(r.desc('created_at'))
.skip(0)
.limit(50);

我尝试使用以下索引(使用 Thinky.js):

Model.ensureIndex("user_mentions", function(message){
    return message("mentions").map(function(user_id){
        return message("channels").map(function(channel_id){
            return [ 
                message("community_id"), 
                message("mentions").contains(user_id),
                message("channels").contains(channel_id), 
                message('created_at') 
            ];
        }); 
    });

}, {multi: true});

然后查询它我试过这个:

r.db('my_db')
.table('messages')
.between(
    [community.id, user.id, channel.id, r.minval], 
    [community.id, data.user.id, channel.id, r.maxval], 
    { index : 'user_mentions' }
)
.orderBy({index:r.desc("user_mentions")})
.skip(0)
.limit(50);

消息表如下所示:

id | community_id | mentions (array of user_ids) | channels (array of channel_ids) | created_at

但我最终得到零结果。我非常感谢任何建议!

4

1 回答 1

0

我认为该索引将使between您在上面编写的查询起作用:

.indexCreate(function(message) {
  return message('channels').concatMap(function(channel) {
    return message('mentions').map(function(mention) {
      return [
        message('community_id'),
        mention,
        channel,
        message('created_at')
      ];
    });
  });
}, {multi: true});
于 2015-11-17T01:48:14.923 回答