2017-09-27 78 views
0

knex.js允許我們使用js構建查詢。 使用異步函數假設我們有下面的代碼邏輯:knex 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

是的。您可以通過調用.toSQL()方法來查看由查詢構建器生成的查詢。並通過設置環境變量export DEBUG=knex:*