feat(sql): optimized ASOF JOIN on single symbol key where RHS symbol is low-frequency#6208
Merged
bluestreak01 merged 140 commits intomasterfrom Oct 28, 2025
Merged
feat(sql): optimized ASOF JOIN on single symbol key where RHS symbol is low-frequency#6208bluestreak01 merged 140 commits intomasterfrom
bluestreak01 merged 140 commits intomasterfrom
Conversation
puzpuzpuz
previously approved these changes
Oct 22, 2025
core/src/main/java/io/questdb/griffin/engine/join/AsOfJoinMemoizedRecordCursorFactory.java
Outdated
Show resolved
Hide resolved
bluestreak01
requested changes
Oct 23, 2025
Contributor
Author
The changes I see in I think the reason why this is showing up now isn't any change in this PR, but an improvement to the static analysis in IDEA. It can see that a few lines above we have a check |
Contributor
[PR Coverage check]😍 pass : 280 / 296 (94.59%) file detail
|
bluestreak01
approved these changes
Oct 28, 2025
This was referenced Oct 31, 2025
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Keyed ASOF JOIN on a non-indexed symbol column uses linear scan to find the matching RHS row. For each LHS row, it repeats the RHS search from scratch. This works fine as long as the RHS table has plenty occurrences of all symbols.
However, if some symbol occurs in the RHS table only rarely, a great many rows will have to be scanned to find it. Then, when we move on after finding it, and encounter the same symbol in a future LHS row, we'll have to repeat the entire scan, only to find the exact same RHS symbol.
This PR saves time by remembering the location of each symbol it encounters in its search for the matching RHS row, and then reusing that knowledge when scanning again. It also remembers where it didn't find a symbol, allowing it to skip over the already-scanned region of the RHS table.
Benchmark
The benchmark uses two tables,
pricesas the RHS in the join, andordersas LHS.We set up the data using the Zipf (long-tail) distribution of 5,000 symbols in
prices, but hand-pick only the 500 rarest symbols fororders. Each of these symbols occurs just 5-9 times among the table's 320 million rows.We set up the timestamps such that orders in
ordershappen later than almost all the data inprices, resulting in a large search space for the matching ASOF JOIN row.The query:
This query results in 1,201,000 rows.
SELECT /*+ asof_fast_search(t p)*/, the query times out.SELECT /*+ asof_index_search(t p)*/ ...., the query times out again. Now the search is able to skip over entire partitions where the symbol doesn't exist, but it must still go through the partitions one by one, repeatedly.