Skip to content

Repeated ContentType SQL queries in enqueue_webhooks slow down bulk-delete of objects via UI #5309

@glennmatthews

Description

@glennmatthews

Environment

  • Python version: 3.7
  • NetBox version: 2.8.9

Steps to Reproduce

  1. Via the UI, bulk-delete several thousand records (any object type; I observed this with custom models in a plugin but I do not believe that this is a requirement) in a single request
  2. Observe how long the request takes to complete and return a new page.

Expected Behavior

Request should complete within a couple of seconds.

Observed Behavior

Deleting 2000 records takes approximately 28 seconds in my testing.

Analysis

It appears that enqueue_webhooks() is called for each record being deleted, and each time enqueue_webhooks() is called, it makes an SQL query for ContentType objects:

SELECT "django_content_type"."id", "django_content_type"."app_label", "django_content_type"."model"
FROM "django_content_type"
WHERE (
("django_content_type"."app_label" = 'dcim' AND "django_content_type"."model" IN ('consoleport', 'consoleserverport', 'powerport', 'poweroutlet', 'interface', 'frontport', 'rearport', 'devicebay', 'inventoryitem', 'region', 'site', 'rack', 'rackreservation', 'manufacturer', 'devicetype', 'device', 'virtualchassis', 'powerpanel', 'powerfeed', 'cable'))
OR ("django_content_type"."app_label" = 'circuits' AND "django_content_type"."model" IN ('provider', 'circuit'))
OR ("django_content_type"."app_label" = 'virtualization' AND "django_content_type"."model" IN ('cluster', 'virtualmachine')) 
OR ("django_content_type"."app_label" = 'ipam' AND "django_content_type"."model" IN ('vrf', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'service'))
OR ("django_content_type"."app_label" = 'secrets' AND "django_content_type"."model" IN ('secret'))
OR ("django_content_type"."app_label" = 'tenancy' AND "django_content_type"."model" IN ('tenant'))
); args=('dcim', 'consoleport', 'consoleserverport', 'powerport', 'poweroutlet', 'interface', 'frontport', 'rearport', 'devicebay', 'inventoryitem', 'region', 'site', 'rack', 'rackreservation', 'manufacturer', 'devicetype', 'device', 'virtualchassis', 'powerpanel', 'powerfeed', 'cable', 'circuits', 'provider', 'circuit', 'virtualization', 'cluster', 'virtualmachine', 'ipam', 'vrf', 'aggregate', 'prefix', 'ipaddress', 'vlan', 'service', 'secrets', 'secret', 'tenancy', 'tenant')

As a result, deleting 2000 records results in 2000 SQL queries, adding substantially to the request runtime. This appears to be due to the code changes made in #3932 (specifically here). It should be possible to cache the results of the ContentType lookup in some fashion rather than re-executing the same query over and over again, which should resolve this performance issue.

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions