简体   繁体   中英

knex js query builder performance

knex.js allow us to build query using js. Suppose we have the following code logic using async functions:

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});

Or we can user the subquery building from knex.js, something like:

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});

Apparently both ways will lead us to have the same result (result3), but in the first approach, we performed queries for 3 times on the db, which might take some time if the db is at remote.

Can second approach perform less number of queries on db using the subqueries, and save some time?

Yes. You can see queries generated by the query builder by calling .toSQL() method. And to see all executed queries by setting environment variable export DEBUG=knex:*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM