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

prefetch for engagements all page #4033

Merged
merged 4 commits into from
Mar 22, 2021

Conversation

valentijnscholten
Copy link
Member

After updating to the latest dev I noticed the engagements_all page was slow, executing 300+ queries.
This PR adds some needed prefetching. Not sure if this removed at some point, but seems to work fine now.

@valentijnscholten valentijnscholten added performance performance improvement or bug report easy-review labels Mar 9, 2021
@valentijnscholten
Copy link
Member Author

wdyt @danielnaab

@danielnaab
Copy link
Contributor

@valentijnscholten sorry to hear about the problems - I tested this locally on a copy of our prod db. I had found that while those prefetches reduce the query count, they nonetheless increase the total time to render the page. But this could definitely be due to something specific about our database. This is what I'm seeing (via django-debug-toolbar):

Before prefetches:

  • Total CPU time: 4.8 sec
  • 49 queries / 3.1 sec

With prefetches:

  • Total CPU time: 21.8 sec
  • 17 queries / 10.9 sec

So despite reducing the query count, the total time is ~4.5 times slower. This is with 12 rows in the rendered /engagements_all table, and no tags on any of the engagements. How does your total render time compare?

@valentijnscholten
Copy link
Member Author

valentijnscholten commented Mar 9, 2021

To be sure, are you looking at the page at /engagements_all ?
You're numbers look extremely slow for that small amount of engagements.

My numbers are:

mysql 488 engagements (143 products)

with prefetch:
900ms cpu / 28queries(70ms)

without
8000ms cpu / 330 queries (327ms)
postgres 3661 engagement (45 products)

with
6979ms cpu / 23 querie (59ms)

without
61000ms cpu / 2973 queries (2957ms)

When disabling the debug toolbar the without scenario becomes a bit faster, but still slower than without prefetching.

@valentijnscholten
Copy link
Member Author

to be honest, that whole page should be rewritten because it does everything based on product instead of engagement.
so the paging doesn't work as expected and can render 100s of rows on a single page.

@danielnaab
Copy link
Contributor

Yeah it's odd. We definitely found the queries themselves weren't the main issue with our performance, but something in the ORM layer. We do have a lot of tests, so I suspect the ORM-layer is getting caught up trying to map all the prefetched objects' over all the various relations.

For an idea, this shows our test count per engagement:

image

@valentijnscholten
Copy link
Member Author

valentijnscholten commented Mar 9, 2021

Yes, with those amounts of tests it will go crazy. For we should prefetch the count by using annotate instead of doing e.test_set.count, after which we don't need to prefetch the test_set anymore. But I don't know how to do that when the main query is for products, so again we would need to make the main query based on engagements.

@valentijnscholten
Copy link
Member Author

OK, couldn't help myself and updated the PR. It now counts the tests using an SQL query instead of loading all tests into memory. Could you try this maybe?

@danielnaab
Copy link
Contributor

@valentijnscholten that works great - here's updated numbers:

Before prefetches:

  • Total CPU time: 4.8 sec
  • 49 queries / 3.1 sec

With prefetches

  • Total CPU time: 21.8 sec
  • 17 queries / 10.9 sec

With prefetches + prefetch count:

  • Total CPU time: 4.8 sec
  • 33 queries / 2.1 sec

@valentijnscholten
Copy link
Member Author

Not bad with that CRAZY amount of tests 😜

@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions
Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

@valentijnscholten valentijnscholten merged commit 665e4f3 into DefectDojo:dev Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy-review performance performance improvement or bug report
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants