[Patchew-devel] [PATCH] models: force execution of project/parent-project subquery

Paolo Bonzini posted 1 patch 1 year, 7 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew-ci tags/patchew/20220920130023.39630-1-pbonzini@redhat.com
api/models.py | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
[Patchew-devel] [PATCH] models: force execution of project/parent-project subquery
Posted by Paolo Bonzini 1 year, 7 months ago
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/models.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/api/models.py b/api/models.py
index 721366d..a3fd3e7 100644
--- a/api/models.py
+++ b/api/models.py
@@ -303,11 +303,18 @@ class Project(models.Model):
 
     @classmethod
     def get_project_ids_by_id(cls, id):
-        return cls.objects.filter(Q(id=id) | Q(parent_project__id=id)).values_list('id')
+        q = cls.objects.filter(Q(id=id) | Q(parent_project__id=id)).values_list('id')
+        # Force execution of the query.  PostgreSQL sometimes (but not always...)
+        # sees the subquery and does not use the (project, topic, date) index;
+        # instead it uses the (topic, date) index and filters on the project later,
+        # which is horrible for projects that have very few messages.  Since our
+        # intended plan is to first walk the small project table, do that explicitly.
+        return [x[0] for x in list(q)]
 
     @classmethod
     def get_project_ids_by_name(cls, name):
-        return cls.objects.filter(Q(name=name) | Q(parent_project__name=name)).values_list('id')
+        q = cls.objects.filter(Q(name=name) | Q(parent_project__name=name)).values_list('id')
+        return [x[0] for x in list(q)]
 
     def get_project_head(self):
         return self.get_property("git.head")
-- 
2.37.2

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/patchew-devel