knex js查询构建器性能

时间:2017-09-27 08:31:23

标签: javascript knex.js

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执行较少数量的查询,并节省一些时间?

1 个答案:

答案 0 :(得分:2)

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

来查看所有已执行的查询
相关问题