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

Enhance performance of open-data endpoint api/v2/open-data/<uuid>/data #2456

Merged
merged 10 commits into from
Jul 26, 2023

Conversation

kelvin-muchiri
Copy link
Contributor

@kelvin-muchiri kelvin-muchiri commented Jul 21, 2023

Changes / Features implemented

An improvement to the enhancements implemented here

  • Use raw SQL queries to improve the performance of endpoint GET api/v2/open-data/<uuid>/data
  • Remove ordering of the results by id in ascending order since when the number of submissions is large the query is extremely slow. There is also no reason to order the results by default. The consumers of the endpoint can carry out that functionality by themselves

Steps taken to verify this change does what is intended

  • QA

Side effects of implementing this change

Faster response times for endpoint GET api/v2/open-data/<uuid>/data

Before submitting this PR for review, please make sure you have:

  • Included tests
  • Updated documentation

Closes #

self.data_count = (
Instance.objects.filter(**qs_kwargs, deleted_at__isnull=True)
.only("pk")
.count()
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets avoid doing an extra count query and use xform.num_of_submissions instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KipSigei That would work, but we are applying filters so xform.num_of_submissions will not give us the correct result

use raw SQL queries to improve the performance
we are not using string formatting on raw queries or quote placeholders in the SQL strings as per the Django guidelines
order by is hurting the performance of the SQL query when then number of instances is very large
string interpolation exposes the query to SQL injection
@kelvin-muchiri kelvin-muchiri merged commit 81f0798 into main Jul 26, 2023
@kelvin-muchiri kelvin-muchiri deleted the improve-open-data-performance branch July 26, 2023 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants