【发布时间】: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