-
Notifications
You must be signed in to change notification settings - Fork 2
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
Prerun input query in append_pipes
#63
Conversation
@havok2063 Just a quick ping in case you had not seen that this was ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good and is promising. Since the zora search form will be the primary entry point, is it feasible to add the limit
option to the /query/main
endpoint? If we can easily add it to the end, just before the query.dicts()
call, that would be ideal. That way, the limit can be used for any query.
I think this is the only other endpoint right now where it'd be useful.
# Run initial query as a temporary table. | ||
temp = create_temporary_table(query, indices=['sdss_id']) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How long do these temp tables stick around after creation? We're using append_pipes
in most, if not all, of the other endpoints. Will we need to worry about the number and size of these tables hanging around the db?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. Temporary tables should only stick around while a connection or a transaction are open. But since valis
never closes the connection that could be an issue. I think adding an atomic
to the Peewee query to force a transaction should delete the temporary table after the query is done, but I'll check that. Worst case I think I can manually delete the table.
python/valis/db/queries.py
Outdated
@@ -284,7 +292,7 @@ def carton_program_search(name: str, | |||
""" | |||
|
|||
if query is None: | |||
query = vizdb.SDSSidStacked.select(vizdb.SDSSidStacked).distinct() | |||
query = vizdb.SDSSidStacked.select(vizdb.SDSSidStacked) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the removal of the distinct
, does this still return a single item per unique sdss_id? Or now multiple items for a given sdss_id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me check. We may still want the distinct. I may have removed while doing tests to try to understand why the query with LIMIT
is so much slower.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the distinct
is needed but slows things down again, I think we can replace it with a group by. I saw similar performance between using group by
and distinct
to get the unique ids.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, the last commit reinstates the .distinct()
in carton_program_search()
. It also adds some special settings for that query. Not a perfect solution but probably the best we can do generically for all cartons/programs if the query requires multiple joins. I added some notes to remind ourselves what we did here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good.
This PR changes the way that
append_pipes
runs to avoid the query planner making bad decisions when executing the query.Currently
append_pipes
joins the pipelines tables to an existing query, extending its select. But in some cases this could make the query planner execute the query inefficiently. With this PRappend_to_queries
runs the initial query into a temporary table. Then it joins the temporary table tosdssid_to_pipes
. This should never be less efficient than doing the whole things as a single query since joining tosdssid_to_pipes
doesn't filter down any of the results from the original query, just adds columns.With this the route
goes from taking about 13 seconds to 5 seconds. When run with
it takes ~200 milliseconds. Note that this requires not setting
when we are using the limit. I'm not completely sure why that is the case, but it seems that the
LIMIT
condition does not work well if only index scans are used, and in that case a sequential scan is fast because it will stop as soon as theLIMIT
is hit.I've also checked other queries that use
append_pipes
in Zora, but more checking would be good.Fixes #58