我有以下查询,它运行良好,但速度较慢,但我不知道如何正确索引:
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
但我最终没有得到任何结果。非常感谢您的建议!
我认为这个索引将使您在上面编写的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});