terrajobst / nquery-vnext

A Roslyn inspired rewrite of NQuery
MIT License
72 stars 16 forks source link

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

Open dallmair opened 7 months ago

dallmair commented 7 months ago

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).