Skip to content

[sql]: Optimize conditions - #6129

Draft
aquapi wants to merge 5 commits into
drizzle-team:rc5from
aquapi:rc5
Draft

[sql]: Optimize conditions#6129
aquapi wants to merge 5 commits into
drizzle-team:rc5from
aquapi:rc5

Conversation

@aquapi

@aquapi aquapi commented Aug 12, 2026

Copy link
Copy Markdown

Current progress:

  • Optimize multiple is() calls to multiple instanceof checks and one prototype tranversal.
  • Optimize and() and or() by caching common string chunks and directly join the chunks instead of using sql template or sql.join().

aquapi and others added 4 commits August 13, 2026 01:07
Change while loop to a for loop, cache type[entityKind] access, and add `isAnyKindIn(value, entityKinds)` method to only tranverse the prototype once when checking for entity kind
- Optimize `bindIfParam()` to use `isAnyKindIn()` to only tranverse the prototype chain once
- Cache separator chunks and open/close parenthesis chunks
- Allocate 1 SQL object with all the joined chunks instead of 2, which also speeds up `toSQL()` on the result object
Use cached string chunks directly instead of using `sql` template
@AlexBlokh

Copy link
Copy Markdown
Contributor

let's maybe since we already have DrizzleEntity have an additional int constant of entity type, which will cover most of the hot paths? wont work(will be hackie) for inheritance, though non the less

cc: @Sukairo-02 @aquapi

@AlexBlokh

Copy link
Copy Markdown
Contributor

also for mitata benchmarks make sure to use .gc('inner') and run with sudo and install mitata counters to see cache hit, cpu counter and mem usage on linux/macos

we can also expore benchmark solutions to count eventloop iterations per solution, didn't have time to explore that domain

@AlexBlokh

Copy link
Copy Markdown
Contributor

also I still think we shall do these kind of optimisations in a reverse order, aka we rebuild query builder path as a separate entry point gradually adding features

for example we start with db.select().from() and we build a separate optimised compile path for that, without sql\`` and then we gradually add features and benchmark use cases, etc.

…, `notInArray()` building performance

Manually construct the chunk array instead of using `sql` tagged template
@aquapi

aquapi commented Aug 13, 2026

Copy link
Copy Markdown
Author

we can also expore benchmark solutions to count eventloop iterations per solution, didn't have time to explore that domain

I think we can implement a node addon that calls libuv to get this.

@AlexBlokh

Copy link
Copy Markdown
Contributor

@aquapi that would be awesome, I primarily care about node+bun, if it will be compatible, if node only - still fine

@aquapi

aquapi commented Aug 14, 2026

Copy link
Copy Markdown
Author

@AlexBlokh oh wait there is a package: https://npmx.dev/event-loop-stats
I built one with napi-rs too if u want to check it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants