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

Further optimization of subqueries with comparators #21

Open
janetzki opened this issue Mar 6, 2019 · 0 comments
Open

Further optimization of subqueries with comparators #21

janetzki opened this issue Mar 6, 2019 · 0 comments

Comments

@janetzki
Copy link
Collaborator

janetzki commented Mar 6, 2019

SELECT c_custkey
FROM customer
WHERE c_acctbal < (
  SELECT MAX(o_totalprice)
  FROM orders
  WHERE o_custkey = c_custkey
)

returns all customers who have at least one order but cannot afford any of them.
We currently optimize it to an inner join that has c_acctbal < o_totalprice as its predicate.
However, the query could also be written in an even more efficient way that uses an equi join:

SELECT c_custkey
FROM customer
JOIN orders ON o_custkey = c_custkey
GROUP BY c_custkey, c_acctbal
HAVING c_acctbal < MAX(o_totalprice)

Specifically for this case, it is possible to boost it even further:

SELECT DISTINCT c_custkey
FROM customer
JOIN orders ON o_custkey = c_custkey
WHERE c_acctbal < o_totalprice

The SubqueryToJoinRule should apply these reformulation techniques when possible.

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

No branches or pull requests

1 participant