From 210ddde630b7df64fdebd44ee621f1c9733e2305 Mon Sep 17 00:00:00 2001 From: Anthony Ruhier Date: Thu, 27 Dec 2018 23:20:19 +0100 Subject: [PATCH] Add NATURAL_ORDERING opt to disable natsort With a lot of objects, the sort query took more than 2s to complete, each time they were listed. Making the concession to disable natural ordering sort reduces this time to 0.1s, making the listing views and homepage snappier in our case. --- docs/configuration/optional-settings.md | 9 +++++++++ netbox/dcim/models.py | 9 ++++++--- netbox/netbox/configuration.example.py | 5 +++++ netbox/netbox/settings.py | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/configuration/optional-settings.md b/docs/configuration/optional-settings.md index 82412cdf7..ae9948854 100644 --- a/docs/configuration/optional-settings.md +++ b/docs/configuration/optional-settings.md @@ -207,6 +207,15 @@ The amount of time (in seconds) to wait for NAPALM to connect to a device. --- +## NATURAL_ORDERING + +Default: True + +Enable or disable the natural sort order when listing objects (see the Wikipedia page for [Natural sort order](https://en.wikipedia.org/wiki/Natural_sort_order)). +However, it is less efficient than the default sort, leading up to a high CPU consumption in database when sorting a lot of objects (a few thousands). + +--- + ## PAGINATE_COUNT Default: 50 diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 347f0c8b8..31e8885d9 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -296,7 +296,8 @@ class Site(ChangeLoggedModel, CustomFieldModel): to='extras.ImageAttachment' ) - objects = NaturalOrderingManager() + if settings.NATURAL_ORDERING: + objects = NaturalOrderingManager() tags = TaggableManager() csv_headers = [ @@ -543,7 +544,8 @@ class Rack(ChangeLoggedModel, CustomFieldModel): to='extras.ImageAttachment' ) - objects = NaturalOrderingManager() + if settings.NATURAL_ORDERING: + objects = NaturalOrderingManager() tags = TaggableManager() csv_headers = [ @@ -1432,7 +1434,8 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel): to='extras.ImageAttachment' ) - objects = NaturalOrderingManager() + if settings.NATURAL_ORDERING: + objects = NaturalOrderingManager() tags = TaggableManager() csv_headers = [ diff --git a/netbox/netbox/configuration.example.py b/netbox/netbox/configuration.example.py index d7a9cf2ed..6dd6acee9 100644 --- a/netbox/netbox/configuration.example.py +++ b/netbox/netbox/configuration.example.py @@ -118,6 +118,11 @@ NAPALM_TIMEOUT = 30 # be provided as a dictionary. NAPALM_ARGS = {} +# Enable or disable the natural sort order when listing objects (see https://en.wikipedia.org/wiki/Natural_sort_order +# for details). However, it is less efficient than the default sort, leading up to a high CPU consumption in database +# when sorting a lot of objects (a few thousands). +NATURAL_ORDERING = True + # Determine how many objects to display per page within a list. (Default: 50) PAGINATE_COUNT = 50 diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 8651c3d26..e9b9deeef 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -63,6 +63,7 @@ NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '') NAPALM_PASSWORD = getattr(configuration, 'NAPALM_PASSWORD', '') NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30) NAPALM_ARGS = getattr(configuration, 'NAPALM_ARGS', {}) +NATURAL_ORDERING = getattr(configuration, 'NATURAL_ORDERING', True) PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50) PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False) REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')