From 2db4b675051f768cba8c3a6f9dd218973160bd83 Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Fri, 7 Oct 2016 00:00:41 +0300 Subject: [PATCH] Fix: queries API was doing N+1 queries in most cases --- redash/handlers/queries.py | 6 +++--- redash/models.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/redash/handlers/queries.py b/redash/handlers/queries.py index d4d9c18b..662ff6f2 100644 --- a/redash/handlers/queries.py +++ b/redash/handlers/queries.py @@ -28,7 +28,7 @@ class QuerySearchResource(BaseResource): def get(self): term = request.args.get('q', '') - return [q.to_dict() for q in models.Query.search(term, self.current_user.groups)] + return [q.to_dict(with_last_modified_by=False) for q in models.Query.search(term, self.current_user.groups)] class QueryRecentResource(BaseResource): @@ -76,7 +76,7 @@ class QueryListResource(BaseResource): results = models.Query.all_queries(self.current_user.groups) page = request.args.get('page', 1, type=int) page_size = request.args.get('page_size', 25, type=int) - return paginate(results, page, page_size, lambda q: q.to_dict(with_stats=True)) + return paginate(results, page, page_size, lambda q: q.to_dict(with_stats=True, with_last_modified_by=False)) class MyQueriesResource(BaseResource): @@ -86,7 +86,7 @@ class MyQueriesResource(BaseResource): results = models.Query.by_user(self.current_user, drafts) page = request.args.get('page', 1, type=int) page_size = request.args.get('page_size', 25, type=int) - return paginate(results, page, page_size, lambda q: q.to_dict(with_stats=True)) + return paginate(results, page, page_size, lambda q: q.to_dict(with_stats=True, with_last_modified_by=False)) class QueryResource(BaseResource): diff --git a/redash/models.py b/redash/models.py index f794e501..91ad4d0a 100644 --- a/redash/models.py +++ b/redash/models.py @@ -700,7 +700,7 @@ class Query(ModelTimestampsMixin, BaseModel, BelongsToOrgMixin): .where(where) \ .where(DataSourceGroup.group << groups) - return cls.select().where(cls.id << query_ids) + return cls.select(Query, User).join(User).where(cls.id << query_ids) @classmethod