【问题标题】:knex js query builder performanceknex js查询构建器性能
【发布时间】:2017-09-27 08:31:22
【问题描述】:

knex.js 允许我们使用 js 构建查询。 假设我们有以下使用异步函数的代码逻辑:

const result1 = await knex('table1').select('something').where({condition1});
const result2 = await knex('table2').select('something').where({condition2: result1});
const result3 = await knex('table3').select('something').where({condition3: result2});

或者我们可以使用从 knex.js 构建的子查询,例如:

const subquery1 = knex('table1').select('something').where({condition1});
const subquery2 = knex('table2').select('something').where({condition2: subquery1});
const result3 = await knex('table3').select('something').where({condition3: subquery2});

显然两种方式都会导致我们得到相同的结果(result3),但在第一种方式中,我们在 db 上执行了 3 次查询,如果 db 在远程,这可能需要一些时间。

第二种方法可以使用子查询对数据库执行更少的查询,并节省一些时间吗?

【问题讨论】:

    标签: javascript knex.js


    【解决方案1】:

    是的。您可以通过调用.toSQL() 方法查看查询生成器生成的查询。并通过设置环境变量export DEBUG=knex:*查看所有执行的查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      相关资源
      最近更新 更多