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
@@ -43,6 +43,7 @@ class Meta:
             "pulp_id",
             "pulp_created",
             "pulp_last_updated",
+            "timestamp_of_interest",
         )
         import_id_fields = ("sha256",)
 
@@ -99,15 +100,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
@@ -106,7 +106,9 @@ def create(self, request, importer_pk):
 
         path = serializer.validated_data.get("path")
         toc = serializer.validated_data.get("toc")
-        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
@@ -22,6 +22,9 @@ class QueryModelResource(resources.ModelResource):
     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:
@@ -47,8 +50,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: