-
Notifications
You must be signed in to change notification settings - Fork 16
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
Feature/optimize member queries #22
Conversation
@akariv Anything missing here apart from the broken build? |
Well, this is a optimisation patch meant to tackle the fact that postgres |
ca4c401
to
3b78074
Compare
@akariv The tests were failing because we were trying doing queries like SELECT
cap_or_cur.code AS "cap_or_cur.code",
max(cap_or_cur.label) AS "cap_or_cur.label"
FROM cap_or_cur
GROUP BY cap_or_cur.code
ORDER BY cap_or_cur.label DESC NULLS LAST; -- This column wasn't part of the select This raises the error:
The
I expected that we would have a unique index between for If that's not the case, and there can be multiple |
I think that if we make the query to look like SELECT
cap_or_cur.code AS "cap_or_cur.code",
max(cap_or_cur.label) AS "cap_or_cur.label"
FROM cap_or_cur
GROUP BY cap_or_cur.code
ORDER BY "cap_or_cur.label" DESC NULLS LAST; The point of this query is to retrieve only one row per code - just grouping by code and label might bring multiple instances for each code. |
So, my aim is to change the query to: SELECT *
FROM (
SELECT
cap_or_cur.code AS "cap_or_cur.code",
max(cap_or_cur.label) AS "cap_or_cur.label"
FROM cap_or_cur
GROUP BY cap_or_cur.code
) AS cap_or_cur
ORDER BY "cap_or_cur.label" DESC NULLS LAST; This avoids having to add def members(self, ref, cuts=None, order=None, page=None, page_size=None):
""" List all the distinct members of the given reference, filtered and
paginated. If the reference describes a dimension, all attributes are
returned. """
q = select()
bindings = []
cuts, q, bindings = Cuts(self).apply(q, bindings, cuts)
fields, q, bindings = Fields(self).apply(q, bindings, ref, distinct=True)
q = select([q.alias('cap_or_cur')]) # <---- Subquery (This is the only line added)
ordering, q, bindings = Ordering(self).apply(q, bindings, order)
q = self.restrict_joins(q, bindings)
count = count_results(self, q)
# ... This generates the query (it fails before pagination, so that wasn't added yet): SELECT
cap_or_cur."cap_or_cur.code",
cap_or_cur."cap_or_cur.label"
FROM (
SELECT
cap_or_cur.code AS "cap_or_cur.code",
cap_or_cur.label AS "cap_or_cur.label"
FROM cap_or_cur
GROUP BY cap_or_cur.code, cap_or_cur.label
) AS cap_or_cur
ORDER BY cap_or_cur.label DESC NULLS LAST Which fails because the actual colum codes are |
540a99c
to
558880c
Compare
558880c
to
95eb17c
Compare
@vitorbaptista I think I got it - take a look |
1 similar comment
@akariv Cool! If I understood correctly, your patch makes the order use Wow! Super interesting. The original query that was raising the error was: SELECT
cap_or_cur.code AS "cap_or_cur.code",
max(cap_or_cur.label) AS "cap_or_cur.label"
FROM cap_or_cur
GROUP BY cap_or_cur.code
ORDER BY cap_or_cur.label DESC NULLS LAST; The query created now after you modified the code is: SELECT
cap_or_cur.code AS "cap_or_cur.code",
max(cap_or_cur.label) AS "cap_or_cur.label"
FROM cap_or_cur
GROUP BY cap_or_cur.code
ORDER BY "cap_or_cur.label" DESC NULLS LAST; -- The only difference are the double quotes here The difference is just the double quotes in the Your solution works, although I'm not sure I understand why 😅 I tried to understand how your changes resulted in this new SQL query, but couldn't. Regardless, all tests are passing and it worked with a few examples I tried locally, so LGTM 👍 |
No description provided.