[PR #4489] feat: optimize GetAccount for large number of policies and rules #20510

Open
opened 2026-08-05 05:07:21 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/4489
Author: @saule1508
Created: 9/12/2025
Status: 🔄 Open

Base: mainHead: feat/optimize-GetAccount-large-setup


📝 Commits (1)

  • 782baba feat: optimize GetAccount for large number of policies and rules

📊 Changes

1 file changed (+35 additions, -13 deletions)

View changed files

📝 management/server/store/sql_store.go (+35 -13)

📄 Description

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/__


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/4489 **Author:** [@saule1508](https://github.com/saule1508) **Created:** 9/12/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/optimize-GetAccount-large-setup` --- ### 📝 Commits (1) - [`782baba`](https://github.com/netbirdio/netbird/commit/782baba5a2746c0f5e8df32b75746e5293dbe0ef) feat: optimize GetAccount for large number of policies and rules ### 📊 Changes **1 file changed** (+35 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `management/server/store/sql_store.go` (+35 -13) </details> ### 📄 Description ## 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](https://github.com/netbirdio/netbird/issues/4488) ## Stack <!-- branch-stack --> ### Checklist - [ ] Is it a bug fix - [ ] Is a typo/documentation fix - [ ] Is a feature enhancement - [X] 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](https://github.com/netbirdio/netbird/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT.md). ## 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/__ --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 05:07:21 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#20510