Query building optimization question

Hello, Id like some input from Kysely advanced users about my approach to using the eb. If I have a conditional select query, is this efficiently using the eb object for my query? Is it ok to use multiple levels of ebs ?
// some input
args = {
param: ['foo', 'bar']
};

await kysely
.selectFrom('my_table')
.innerJoin(
'other',
'other.id',
'my_table.other_id',
)
.selectAll()
.$if(!!args.param && args.param.length > 0, (eb) => {
return eb.where((eb) => {
if (!args.param) throw Error('param is required');
return eb(
eb.ref('data', '->').key('param'),
'in',
args.param,
);
});
})
// some input
args = {
param: ['foo', 'bar']
};

await kysely
.selectFrom('my_table')
.innerJoin(
'other',
'other.id',
'my_table.other_id',
)
.selectAll()
.$if(!!args.param && args.param.length > 0, (eb) => {
return eb.where((eb) => {
if (!args.param) throw Error('param is required');
return eb(
eb.ref('data', '->').key('param'),
'in',
args.param,
);
});
})
Solution:
Creating an expression builder instance is in the order of microseconds. Running the query is in the order of milliseconds or hundreds of milliseconds. You don't need to worry about expression builder instances unless you create a thousand of them per query.
Jump to solution
3 Replies
Solution
koskimas
koskimas13mo ago
Creating an expression builder instance is in the order of microseconds. Running the query is in the order of milliseconds or hundreds of milliseconds. You don't need to worry about expression builder instances unless you create a thousand of them per query.
koskimas
koskimas13mo ago
Always benchmark things before you optimize them. If optimizing means otherwise worse code or takes time away from doing meaningful things
bombillazo
bombillazoOP13mo ago
thanks for the insights, yeah it was pretty performant as is just wondering if the Kysely convention was different. Cant overstate how great the library is tbh
Want results from more Discord servers?
Add your server