Prefetch related properties in DeviceListView #6640

Closed
opened 2025-12-29 19:43:24 +01:00 by adam · 4 comments
Owner

Originally created by @hagbarddenstore on GitHub (Jul 10, 2022).

NetBox version

v3.2.5

Feature type

Change to existing functionality

Proposed functionality

Prefetch the related properties that are defined in the DeviceTable when used in the DeviceListView.

Call .prefetch_related() on the Device queryset defined in DeviceListView with the following arguments:

.prefetch_related('site', 'location', 'rack', 'device_role', 'manufacturer', 'device_type', 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'tenant')

Use case

Provides better performance when accessing lots of different related properties and lessens the load on the database.

Database changes

None

External dependencies

None

Originally created by @hagbarddenstore on GitHub (Jul 10, 2022). ### NetBox version v3.2.5 ### Feature type Change to existing functionality ### Proposed functionality Prefetch the related properties that are defined in the `DeviceTable` when used in the `DeviceListView`. Call `.prefetch_related()` on the Device queryset defined in `DeviceListView` with the following arguments: ``` .prefetch_related('site', 'location', 'rack', 'device_role', 'manufacturer', 'device_type', 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'tenant') ``` ### Use case Provides better performance when accessing lots of different related properties and lessens the load on the database. ### Database changes None ### External dependencies None
adam added the type: feature label 2025-12-29 19:43:24 +01:00
adam closed this issue 2025-12-29 19:43:25 +01:00
Author
Owner

@hagbarddenstore commented on GitHub (Jul 10, 2022):

Not entirely sure about my accuracy on the properties to prefetch, but those where the ones that are used in the table.

@hagbarddenstore commented on GitHub (Jul 10, 2022): Not entirely sure about my accuracy on the properties to prefetch, but those where the ones that are used in the table.
Author
Owner

@kkthxbye-code commented on GitHub (Jul 10, 2022):

Provides better performance when accessing lots of different related properties and lessens the load on the database.

Did you confirm this? I don't see any difference in queries generated.

Edit:

To expand, all related object queries are batched for me already. I couldn't find any cases where the related object is fetched one-by-one, which is the thing prefetch_related is supposed to fix. That is not to say that there is no cases like that, so if you find one on the device list, please let me know.

For fun I also tried using select_related, which will do joins on the main query for all the related object, but while reducing the number of queries, the total query time doubled.

Query time is such a small part of the total execution time on the DeviceList for me that I think it would be better if you explained where you are experiencing database related performance issues.

@kkthxbye-code commented on GitHub (Jul 10, 2022): > Provides better performance when accessing lots of different related properties and lessens the load on the database. Did you confirm this? I don't see any difference in queries generated. Edit: To expand, all related object queries are batched for me already. I couldn't find any cases where the related object is fetched one-by-one, which is the thing `prefetch_related` is supposed to fix. That is not to say that there is no cases like that, so if you find one on the device list, please let me know. For fun I also tried using` select_related`, which will do joins on the main query for all the related object, but while reducing the number of queries, the total query time doubled. Query time is such a small part of the total execution time on the DeviceList for me that I think it would be better if you explained where you are experiencing database related performance issues.
Author
Owner

@hagbarddenstore commented on GitHub (Jul 11, 2022):

I found something clever in NetBox...

https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/tables/tables.py#L76

So, things brings up another question, when should prefetch be used in NetBox?

@hagbarddenstore commented on GitHub (Jul 11, 2022): I found something clever in NetBox... https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/tables/tables.py#L76 So, things brings up another question, when should prefetch be used in NetBox?
Author
Owner

@jeremystretch commented on GitHub (Jul 11, 2022):

I found something clever in NetBox...

We added that when introducing configurable tables, to ensure that we're not prefetching everything while displaying only a subset of that data.

So, things brings up another question, when should prefetch be used in NetBox?

Everywhere it's needed when you're not rendering a table (e.g. for API endpoints). See the Django docs for further considerations.

@jeremystretch commented on GitHub (Jul 11, 2022): > I found something clever in NetBox... We added that when introducing configurable tables, to ensure that we're not prefetching everything while displaying only a subset of that data. > So, things brings up another question, when should prefetch be used in NetBox? Everywhere it's needed when you're not rendering a table (e.g. for API endpoints). See the [Django docs](https://docs.djangoproject.com/en/4.0/ref/models/querysets/#prefetch-related) for further considerations.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6640