-
Notifications
You must be signed in to change notification settings - Fork 39
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
optimize map marker fetching #1171
base: main
Are you sure you want to change the base?
Conversation
|
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.
Great writeup. Had a look at the code, LGTM.
Converting this to a draft, I uncovered an underlying issue with this approach. The reason the earlier queries performed so quickly was that the challenge ID filter was applied before the location filter. This significantly slows down queries that lack the challenge ID filter. To address this, I need to find a way to prioritize the challenge ID filter over the location filter exclusively. |
This pull request refactors the code for the tasks/box and tasks/markers endpoints, optimizing the underlying SQL query for better performance. The original query combined spatial filtering with other conditions directly in the WHERE clause, often resulting in inefficient execution plans—such as full-table scans or the creation of a large bitmap hash map spanning the entire tasks table within the specified map bounds. This inefficiency is evident in the staging query, which took approximately 1.1 seconds to execute and processed an excessive number of rows during the spatial filter, as shown in its execution plan.
Example of old query in staging:
This is its reads/workflow (take not of the number of rows read in the location filter):

And takes roughly 1.1seconds to complete.
This is the new query:
This is its reads/workflow:

and it takes roughly 150-200ms.
The refactored approach introduces a Common Table Expression (CTE) to streamline the process. By pre-filtering tasks based on non-spatial conditions (e.g., status, priority, and review criteria) in the CTE, the query reduces the dataset before applying the spatial filter. This significantly cuts down the rows processed by the spatial operation, leading to a more efficient execution plan. The updated query, now completing in 150–200 milliseconds, leverages a CTE scan to ensure the spatial filter (ST_MakeEnvelope) operates only on the pre-filtered subset of tasks, avoiding the bloated bitmap index issue seen previously. The new execution plan confirms this optimization, showing a marked reduction in rows read compared to the original.