Repository: pulp/pulpcore

Issue:
Sub-optimal performance during exports due to N+1 queries and complex quries
**Version**
Please provide the versions of the pulpcore and plugin packages in use, and how they are installed.  If you are using Pulp via Katello, please provide the Katello version.

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:

**Expected behavior**
A clear and concise description of what you expected to happen.

**Additional context**
Add any other context about the problem here.  Please provide links to any previous discussions via Discourse or Bugzilla.



Discussion:

Resolving patch:
diff --git a/pulpcore/app/modelresource.py b/pulpcore/app/modelresource.py
--- a/pulpcore/app/modelresource.py
+++ b/pulpcore/app/modelresource.py
@@ -45,6 +45,7 @@ class Meta:
             "pulp_id",
             "pulp_created",
             "pulp_last_updated",
+            "timestamp_of_interest",
         )
         import_id_fields = (
             "pulp_domain",
@@ -112,15 +113,20 @@ def before_import_row(self, row, **kwargs):
         row["content"] = str(linked_content.pulp_id)
 
     def set_up_queryset(self):
-        vers_content = ContentArtifact.objects.filter(content__in=self.repo_version.content)
+        content_pks = set(self.repo_version.content.values_list("pk", flat=True))
+
         if self.content_mapping:
-            all_content = []
             for content_ids in self.content_mapping.values():
-                all_content.extend(content_ids)
-            vers_content = vers_content.union(
-                ContentArtifact.objects.filter(content__in=all_content)
-            )
-        return vers_content.order_by("content", "relative_path")
+                content_pks |= set(content_ids)
+
+        return (
+            ContentArtifact.objects.filter(content__in=content_pks)
+            .order_by("content", "relative_path")
+            .select_related("artifact")
+        )
+
+    def dehydrate_content(self, content_artifact):
+        return str(content_artifact.content_id)
 
     class Meta:
         model = ContentArtifact
diff --git a/pulpcore/app/viewsets/importer.py b/pulpcore/app/viewsets/importer.py
--- a/pulpcore/app/viewsets/importer.py
+++ b/pulpcore/app/viewsets/importer.py
@@ -102,7 +102,9 @@ def create(self, request, importer_pk):
         path = serializer.validated_data.get("path")
         toc = serializer.validated_data.get("toc")
         create_repositories = serializer.validated_data.get("create_repositories")
-        task_group = TaskGroup.objects.create(description=f"Import of {path}")
+        task_group = TaskGroup.objects.create(
+            description="Import of {path}".format(path=path or toc)
+        )
 
         dispatch(
             pulp_import,
diff --git a/pulpcore/plugin/importexport.py b/pulpcore/plugin/importexport.py
--- a/pulpcore/plugin/importexport.py
+++ b/pulpcore/plugin/importexport.py
@@ -38,6 +38,9 @@ def before_import_row(self, row, **kwargs):
     def set_up_queryset(self):
         return None
 
+    def dehydrate_pulp_domain(self, content):
+        return str(content.pulp_domain_id)
+
     def __init__(self, repo_version=None):
         self.repo_version = repo_version
         if repo_version:
@@ -63,8 +66,13 @@ class BaseContentResource(QueryModelResource):
     #
     content_mapping = None
 
-    class Meta:
-        exclude = QueryModelResource.Meta.exclude + ("_artifacts", "content", "content_ptr")
-
     def dehydrate_upstream_id(self, content):
         return str(content.pulp_id)
+
+    class Meta:
+        exclude = QueryModelResource.Meta.exclude + (
+            "_artifacts",
+            "content",
+            "content_ptr",
+            "timestamp_of_interest",
+        )


Test patch: