RestrictedQuerySet should skip secondary filtering when no constraints are in place #11949

Closed
opened 2025-12-29 21:51:48 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Dec 23, 2025).

Originally assigned to: @jeremystretch on GitHub.

NetBox Edition

NetBox Community

NetBox Version

v4.4.8

Python Version

3.12

Steps to Reproduce

  1. Create 100 IP addresses (for example)
  2. Grant a user permission to view IP addresses, without any constraints.
  3. Authenticated as the user, make a REST API request for 10 IP addresses:
GET /api/ipam/ip-addresses/?limit=10

This issue was raised during a recent performance analysis.

Expected Behavior

The primary SQL query resulting from the API request should not have any filters applied.

Observed Behavior

The SQL query is executed using a subquery which matches against all IDs in the table:

SELECT ... FROM "ipam_ipaddress" WHERE "ipam_ipaddress"."id" IN (
    SELECT U0."id" FROM "ipam_ipaddress" U0
) ORDER BY INET(HOST("ipam_ipaddress"."address")) ASC

The root cause lies here:

ee2aa35cba/netbox/utilities/querysets.py (L64-L68)

The allowed_objects subquery is used to avoid duplicated objects where complex constraints are in use (see #8715). However, it is extraneous in cases where no constraints are in place, and can be discarded.

Originally created by @jeremystretch on GitHub (Dec 23, 2025). Originally assigned to: @jeremystretch on GitHub. ### NetBox Edition NetBox Community ### NetBox Version v4.4.8 ### Python Version 3.12 ### Steps to Reproduce 1. Create 100 IP addresses (for example) 2. Grant a user permission to view IP addresses, without any constraints. 3. Authenticated as the user, make a REST API request for 10 IP addresses: ``` GET /api/ipam/ip-addresses/?limit=10 ``` This issue was raised during a recent performance analysis. ### Expected Behavior The primary SQL query resulting from the API request should not have any filters applied. ### Observed Behavior The SQL query is executed using a subquery which matches against all IDs in the table: ```sql SELECT ... FROM "ipam_ipaddress" WHERE "ipam_ipaddress"."id" IN ( SELECT U0."id" FROM "ipam_ipaddress" U0 ) ORDER BY INET(HOST("ipam_ipaddress"."address")) ASC ``` The root cause lies here: https://github.com/netbox-community/netbox/blob/ee2aa35cbaeb6f519d9a2538e78e3c6384ee0bb7/netbox/utilities/querysets.py#L64-L68 The `allowed_objects` subquery is used to avoid duplicated objects where complex constraints are in use (see #8715). However, it is extraneous in cases where no constraints are in place, and can be discarded.
adam added the type: bugstatus: acceptednetboxseverity: low labels 2025-12-29 21:51:48 +01:00
adam closed this issue 2025-12-29 21:51:48 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11949