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

Prerun input query in append_pipes #63

Merged
merged 3 commits into from
Dec 9, 2024
Merged

Prerun input query in append_pipes #63

merged 3 commits into from
Dec 9, 2024

Conversation

albireox
Copy link
Member

@albireox albireox commented Oct 25, 2024

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 PR append_to_queries runs the initial query into a temporary table. Then it joins the temporary table to sdssid_to_pipes. This should never be less efficient than doing the whole things as a single query since joining to sdssid_to_pipes doesn't filter down any of the results from the original query, just adds columns.

With this the route

query/carton-program?name=manual_mwm_tess_ob&name_type=carton&observed=true&release=DR17

goes from taking about 13 seconds to 5 seconds. When run with

query/carton-program?name=manual_mwm_tess_ob&name_type=carton&observed=true&release=DR17&limit=50

it takes ~200 milliseconds. Note that this requires not setting

database.execute_sql('SET LOCAL enable_seqscan=false;')

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 the LIMIT is hit.

I've also checked other queries that use append_pipes in Zora, but more checking would be good.

Fixes #58

@albireox albireox requested a review from havok2063 as a code owner October 25, 2024 19:20
@albireox
Copy link
Member Author

@havok2063 Just a quick ping in case you had not seen that this was ready for review.

Copy link
Contributor

@havok2063 havok2063 left a 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.

Comment on lines +61 to +63
# Run initial query as a temporary table.
temp = create_temporary_table(query, indices=['sdss_id'])

Copy link
Contributor

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?

Copy link
Member Author

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.

@@ -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)
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

@albireox albireox left a 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.

Copy link
Contributor

@havok2063 havok2063 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.

@havok2063 havok2063 merged commit 93a500f into main Dec 9, 2024
2 checks passed
@albireox albireox deleted the albireox/issue58 branch December 9, 2024 18:28
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.

Add new option for limiting query results
2 participants