Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimization: Extract sub-expressions in join conditions to value slots #62

Merged
merged 2 commits into from
Oct 2, 2024

Conversation

dallmair
Copy link
Contributor

@dallmair dallmair commented Feb 9, 2024

So far, NQuery already optimized "trivial case" joins very nicely, such as this one:

SELECT  *
FROM    Employees e
        <type> JOIN EmployeeTerritories et ON e.EmployeeID = et.EmployeeID

Regardless of <type>, these queries used a HashMatchIterator. Unfortunately, even simple changes makes it fall back to nested loops:

SELECT  *
FROM    Employees e
        <type> JOIN EmployeeTerritories et ON e.EmployeeID = TO_INT32(et.EmployeeID)

Bummer, as adding a simple conversion kills performance of big queries.

The proposed additional optimization step extracts computations in join conditions into dedicated value slots (if beneficial) to enable the already existing other optimizations. It even works with contrived examples such as this one:

SELECT  *
FROM    Employees e
        <type> JOIN EmployeeTerritories et ON e.EmployeeID + e.ReportsTo = TO_INT32(et.TerritoryID)

Not modified are conjunctions with any operator other than = as well as sides that refer to both input relations (e.g. e.EmployeeID - et.EmployeeID = 0).

@terrajobst
Copy link
Owner

We might want to add a test that checks we were able to use a hash match by having a table count how many times it was read. This way, we can check that both sides were only read once rather than having the inner read for reach row of the outer table.

@terrajobst terrajobst merged commit 04e81b6 into terrajobst:main Oct 2, 2024
2 checks passed
@dallmair dallmair deleted the fix/optimize-joins branch October 2, 2024 17:47
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.

2 participants