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

Reduce the number of SQL queries in updates of groups #255

Merged
merged 4 commits into from
Oct 4, 2022

Conversation

stephane
Copy link
Contributor

@stephane stephane commented Sep 5, 2022

After having read the PR #230, my initial intention was to focus on SQL queries. I think it's more efficient to reduce the number of SQL queries to speed up the group creation (round-trip latency could be significant with DB).
So my initial patch leveraged create_bulk of Django ORM but I found out about the signal issues in #220 and I changed my mind to provide something less intrusive.

I'll be happy to provide a create_bulk version if you want.

Changes:

  • only one query instead of one for each group when no MIRROR_GROUPS
  • the use of iterator() inside a list() creates a useless SQL cursor
  • avoid to create Django instances of groups by using values_list()
  • replace a frozenset by a tuple
  • add test for MIRROR_GROUPS

I had to disable Black in my editor to not mess the code ;)

- only one query instead of one for each group when no MIRROR_GROUPS
- the use of iterator() inside a list() creates a useless SQL cursor
- avoid to create Django instances of groups by using values_list()
- replace a frozenset by a tuple
@codecov
Copy link

codecov bot commented Sep 5, 2022

Codecov Report

Merging #255 (e440441) into master (de92fa1) will increase coverage by 0.8%.
The diff coverage is 100.0%.

@@           Coverage Diff            @@
##           master    #255     +/-   ##
========================================
+ Coverage    85.4%   86.3%   +0.8%     
========================================
  Files          11       8      -3     
  Lines         556     497     -59     
========================================
- Hits          475     429     -46     
+ Misses         81      68     -13     
Impacted Files Coverage Δ
django_auth_adfs/backend.py 86.4% <100.0%> (+2.3%) ⬆️
django_auth_adfs/views.py
django_auth_adfs/__init__.py
django_auth_adfs/urls.py

@sondrelg sondrelg requested review from JonasKs and tim-schilling and removed request for JonasKs September 6, 2022 08:28
@JonasKs
Copy link
Member

JonasKs commented Sep 9, 2022

Hi! Thank you so much for this. It all seems valid to me, and code looks good.

I’d like @tim-schilling to say his opinion before I merge though 😊

Copy link
Member

@tim-schilling tim-schilling left a comment

Choose a reason for hiding this comment

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

Sorry for taking a while. Thank you for opening this PR. I have one comment I believe should be easy to rectify. Let me know if I'm wrong.

Comment on lines 335 to 342
new_claimed_group_names = (name for name in claim_groups if name not in existing_claimed_group_names)
if settings.MIRROR_GROUPS:
new_groups = [
new_claimed_groups = [
Group.objects.get_or_create(name=name)[0]
for name in claim_groups
if name not in existing_group_names
for name in new_claimed_group_names
]
else:
for name in claim_groups:
if name not in existing_group_names:
try:
group = Group.objects.get(name=name)
new_groups.append(group)
except ObjectDoesNotExist:
pass
user.groups.set(existing_groups + new_groups)
new_claimed_groups = Group.objects.filter(name__in=new_claimed_group_names)
Copy link
Member

Choose a reason for hiding this comment

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

When settings.MIRROR_GROUPS is false, the only groups that should be set for the user are existing_claimed_groups. You can likely move the list comprehension of 335 into the if settings.MIRROR_GROUPS branch and define new_claimed_groups as an empty list in the else branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, indeed!
It does't make sense to filter again on new_claimed_group_names because the result won't change.
It avoids one more query, great!

I prefer to move user.groups.set() in the if branches instead of define new_claimed_groups as an empty list.

@stephane
Copy link
Contributor Author

Strange crash of GithHub Actions:
/home/runner/work/_temp/13a6f92d-c22a-414f-bd68-e59415335718.sh: /home/runner/work/django-auth-adfs/django-auth-adfs/.venv/bin/pip: /home/runner/work/django-auth-adfs/django-auth-adfs/.venv/bin/python: bad interpreter: No such file or directory

Let's cool down the servers before testing again... ;)

@stephane
Copy link
Contributor Author

I don't find the button to re-run jobs BTW...

@JonasKs
Copy link
Member

JonasKs commented Sep 24, 2022

Trying to re-run pipeline, but GitHub is giving me errors.. I’ll try later again.

@JonasKs
Copy link
Member

JonasKs commented Sep 25, 2022

Pipelines seem to have completed now 😊

@JonasKs
Copy link
Member

JonasKs commented Oct 1, 2022

@tim-schilling , If you'd like to look over again? 😊

Copy link
Member

@tim-schilling tim-schilling left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you.

@JonasKs JonasKs merged commit e51135c into snok:master Oct 4, 2022
@JonasKs
Copy link
Member

JonasKs commented Oct 4, 2022

Thank you, @stephane! 😊

I'll get this released after work today.

@stephane stephane deleted the speedup-group-creation branch October 4, 2022 20:24
@JonasKs
Copy link
Member

JonasKs commented Oct 5, 2022

@stephane
Copy link
Contributor Author

stephane commented Oct 5, 2022

Thank you all for the review and the release.

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.

3 participants