Skip to content

Conversation

saule1508
Copy link
Contributor

@saule1508 saule1508 commented Sep 12, 2025

Describe your changes

The policy rules are loaded one by one, for each policy. In a set-up with 480 policies, it generates 480 round trip to the database and a latency of about 500 ms. The fist change I propose here is therefore to load all the rules in one query, using a IN clause with the list of policy id's. This is very low risk and was tested "live" on my set-up.

The second change is low impact on the performance, it is just to avoid two times the same query which is silly. The ORM generates a select for all policies rules with the IN clause (exactly the same as my select). However the result of the select is not used, for some reason the ORM cannot attach it to the slice of rules pointers (hence the reason why it is done manually, as commented in the original code).

Because the code uses a generic "Preload(clause.Associations)" it is not possible to exclude one of the association to avoid this double select. Furthermore it seems a good practice to explicitly list the associations that should be preloaded, to have a more reliable behavior and better documented.

The refactored code produce the same account data on my set-up (large setup), I serialized the account in json and compared.

If requested I can a test in place for the GetAccount, the current seems very limitied (does not mirror the current schema).

I realize this change is important for large number of policies only, which might be an hedge case, but it is very low risk.

The saving is significant on my setup: from 600 -700 ms to 60 - 80ms. Of course I would prefer to have the change upstream than only in my setup.

Issue

4488

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • [ X] Documentation is not needed for this change (explain why)

it is an internal optimization

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

@CLAassistant
Copy link

CLAassistant commented Sep 12, 2025

CLA assistant check
All committers have signed the CLA.

@saule1508 saule1508 force-pushed the feat/optimize-GetAccount-large-setup branch from 52bd294 to 782baba Compare September 12, 2025 13:23
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
1 New issue
1 New Code Smells (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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.

2 participants