From 6f9b75e2c3bbbd7dfc5e2d414d7271bb4f8bcf71 Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Mon, 19 Aug 2013 15:00:03 +1000 Subject: [PATCH] Use joins instead of multiple lookups in groups sql Currently to determine a user's groups (or vice versa) we do - 1 lookup for the user. - 1 lookup for the association table. - n lookups for the n groups the user has. The association and groups lookup are replaced with a join query. Fixes: bug 1218675 Change-Id: I560659eda1a30635399829e1c86f06734c90f9e2 --- keystone/identity/backends/sql.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/keystone/identity/backends/sql.py b/keystone/identity/backends/sql.py index 84026a58ac..f647e26a53 100644 --- a/keystone/identity/backends/sql.py +++ b/keystone/identity/backends/sql.py @@ -201,18 +201,17 @@ class Identity(sql.Base, identity.Driver): def list_groups_for_user(self, user_id): session = self.get_session() self.get_user(user_id) - query = session.query(UserGroupMembership) - query = query.filter_by(user_id=user_id) - membership_refs = query.all() - return [self.get_group(x.group_id) for x in membership_refs] + query = session.query(Group).join(UserGroupMembership) + query = query.filter(UserGroupMembership.user_id == user_id) + return [g.to_dict() for g in query] def list_users_in_group(self, group_id): session = self.get_session() self.get_group(group_id) - query = session.query(UserGroupMembership) - query = query.filter_by(group_id=group_id) - membership_refs = query.all() - return [self.get_user(x.user_id) for x in membership_refs] + query = session.query(User).join(UserGroupMembership) + query = query.filter(UserGroupMembership.group_id == group_id) + + return [identity.filter_user(u.to_dict()) for u in query] def delete_user(self, user_id): session = self.get_session()