This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSCFAIRMETA-1416: Optimize dataset listing performance
* Use prefetch_related when listing datasets to avoid querying related objects one-by-one * Don't make a separate query for dataset_version_set records if there is only 1 * Use pickle instead of copy.deepcopy in track_fields for faster copying of research_dataset * Add research_dataset_fields query param to allow returning only needed fields
- Loading branch information
Showing
7 changed files
with
137 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
# :author: CSC - IT Center for Science Ltd., Espoo Finland <[email protected]> | ||
# :license: MIT | ||
|
||
from copy import deepcopy | ||
import pickle | ||
|
||
from dateutil import parser | ||
from django.core.exceptions import FieldError | ||
|
@@ -132,6 +132,16 @@ def modified_since(self, timestamp): | |
|
||
return timestamp < self.date_modified | ||
|
||
def _deepcopy_field(self, field_value): | ||
""" | ||
Deep copy field value. | ||
Pickle can be an order of magnitude faster than copy.deepcopy for | ||
deeply nested fields like CatalogRecord.research_dataset. | ||
""" | ||
return pickle.loads(pickle.dumps(field_value)) | ||
|
||
|
||
def track_fields(self, *fields): | ||
""" | ||
Save initial values from object fields when object is created (= retrieved from db), | ||
|
@@ -141,17 +151,16 @@ def track_fields(self, *fields): | |
field_name is a dict (a JSON field). For now only one level of nesting is supported. | ||
If a need arises, can be made mega generic. | ||
""" | ||
for field_name in fields: | ||
|
||
self._tracked_fields.append(field_name) | ||
self._tracked_fields.extend(fields) | ||
|
||
for field_name in fields: | ||
if "." in field_name: | ||
self._track_json_field(field_name) | ||
else: | ||
if self._field_is_loaded(field_name): | ||
requested_field = getattr(self, field_name) | ||
if isinstance(requested_field, dict): | ||
self._initial_data[field_name] = deepcopy(requested_field) | ||
self._initial_data[field_name] = self._deepcopy_field(requested_field) | ||
else: | ||
self._initial_data[field_name] = requested_field | ||
|
||
|
@@ -173,7 +182,7 @@ def _track_json_field(self, field_name): | |
self._initial_data[field_name] = {} | ||
|
||
if isinstance(json_field_value, dict): | ||
self._initial_data[field_name][json_field_name] = deepcopy(json_field_value) | ||
self._initial_data[field_name][json_field_name] = self._deepcopy_field(json_field_value) | ||
else: | ||
self._initial_data[field_name][json_field_name] = json_field_value | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters