Cache counts of related objects for recursively-nested models #11424

Open
opened 2025-12-29 21:45:04 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Jul 29, 2025).

NetBox version

v4.3.4

Feature type

Change to existing functionality

Proposed functionality

There are many instances throughout the application where we attach a count of related objects to a queryset. For example, consider the locations REST API endpoint. It extends the base queryset to attach rack_count and device_count for each location. (This occurs regardless of whether a subset of fields was specified in the API request.)

c736ce3179/netbox/dcim/api/views.py (L137-L150)

We should explore opportunities where these complex queries can be obviated, potentially through the use of CounterCacheField. This is already utilized in a few places; most notably on the Device and DeviceType models to store counts of child components:

c736ce3179/netbox/dcim/models/devices.py (L641-L649)

It remains to be determined whether CounterCacheField can be adapted for this purpose, or whether a different approach is needed to handle the cumulative nature of such counts. (The count of related objects should include both direct and indirect children.)

Use case

Adopting cached counters, if proven viable, would obviate the need for complex queries to count related objects at query time, and instead allow us to return pre-calculated counts directly. This would substantially improve query performance in many scenarios.

Database changes

In the example above concerning the Location model, this would involve introducing two new fields on the model:

class Location:
    rack_count = CounterCacheField( 
        to_model='dcim.Rack', 
        to_field='location' 
    ) 
    device_count = CounterCacheField( 
        to_model='dcim.Device', 
        to_field='location' 
    )

External dependencies

N/A

Originally created by @jeremystretch on GitHub (Jul 29, 2025). ### NetBox version v4.3.4 ### Feature type Change to existing functionality ### Proposed functionality There are many instances throughout the application where we attach a count of related objects to a queryset. For example, consider the locations REST API endpoint. It extends the base queryset to attach `rack_count` and `device_count` for each location. (This occurs regardless of whether a subset of fields was specified in the API request.) https://github.com/netbox-community/netbox/blob/c736ce3179a8bb7bbcbf29324a08a62efa8747b6/netbox/dcim/api/views.py#L137-L150 We should explore opportunities where these complex queries can be obviated, potentially through the use of [`CounterCacheField`](https://github.com/netbox-community/netbox/blob/c736ce3179a8bb7bbcbf29324a08a62efa8747b6/netbox/utilities/fields.py#L156). This is already utilized in a few places; most notably on the Device and DeviceType models to store counts of child components: https://github.com/netbox-community/netbox/blob/c736ce3179a8bb7bbcbf29324a08a62efa8747b6/netbox/dcim/models/devices.py#L641-L649 It remains to be determined whether `CounterCacheField` can be adapted for this purpose, or whether a different approach is needed to handle the cumulative nature of such counts. (The count of related objects should include both direct and indirect children.) ### Use case Adopting cached counters, if proven viable, would obviate the need for complex queries to count related objects at query time, and instead allow us to return pre-calculated counts directly. This would substantially improve query performance in many scenarios. ### Database changes In the example above concerning the Location model, this would involve introducing two new fields on the model: ```python class Location: rack_count = CounterCacheField( to_model='dcim.Rack', to_field='location' ) device_count = CounterCacheField( to_model='dcim.Device', to_field='location' ) ``` ### External dependencies N/A
adam added the type: featurenetboxstatus: backlogcomplexity: high labels 2025-12-29 21:45:04 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11424