reThinkDB join/where-exists等价物太慢



我正在做一些与reThinkDB SQL备忘单中的最后一个SELECT示例非常相似的事情:

SQL:

SELECT *
  FROM posts
  WHERE EXISTS
    (SELECT * FROM users
     WHERE posts.author_id
         = users.id)

反思:

r.table("posts")
  .filter(function (post) {
    return r.table("users")
      .filter(function (user) {
        return user("id").eq(post("authorId"))
      }).count().gt(0)
    })

以下是我正在做的确切查询(尽管我认为这无关紧要):

// Sample data :
// app table :
// [{id : 123, city : 'abc' }, { id : 234 }, ...]
// weather table :
// [{id : 1, city : 'abc', data : 'some data' }, { id : 2 }, ...]
// ex. rWeather(123) should return [{id : 1, city : 'abc', data : 'some data' }]
// by finding the city 'abc', first in the app table, then in the weather table
/**
 * Returns the weather for a given app
 */
export function rWeather (id) {
    var appCity = function(weather) {
        return r.db('gfi')
            .table('app')
            .withFields(['city'])
            .filter(function (app) {
                return app('id').eq(weather('appId'));
            });
    };
    return r.db('gfi')
        .table('weather')
        .filter(function(weather) {
            return  appCity(weather).count().gt(0);
        });
}

所以问题是:我该如何加快速度?

我应该更改查询的形式吗?我应该在哪里添加索引吗?

注意:我无法在web界面中对其进行评测,查询只会运行很长时间。

您的代码注释与代码不匹配。在您的代码中,似乎是使用weather表上的appId字段加入的。在rWeather中,不使用变量id。。。

所以我会重写它以匹配你的评论

//例如,rWeather(123)应返回[{id:1,city:"abc",data:'some data'}]//通过首先在应用程序表中查找城市'abc',然后在天气表

创建索引:

r.table('weather').indexCreate('city')

这里有一个函数:

export function rWeather (id) {
  return r.db('gfi')
        .table('app').get(id).do(function(app) {
          return r.table('weather').getAll(app('city').default(''), {index: 'city'})
        })
 }

最新更新