2017-09-27 9 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がリモートにある場合は時間がかかることがあります。

サブクエリを使用してdbに対して少ない数のクエリを実行し、時間を節約できますか?

答えて

1

はい。 .toSQL()メソッドを呼び出すと、クエリビルダーによって生成されたクエリを見ることができます。環境変数を設定して実行されたクエリをすべて表示するexport DEBUG=knex:*

関連する問題