简体   繁体   English

为什么在获得用户列表时获得角色如此缓慢?

[英]Why is getting roles when getting a list of users so slow?

I'm getting all users in the DB and their associated roles. 我正在数据库中获得所有用户及其相关角色。 It works, but man is it slow. 它有效,但是人慢。

        var ctx = new SiteUserContext();
        var ctxUserList = ctx.Users.ToList();


        //var userManager = new UserManager<SiteUser>(new UserStore<SiteUser>(new SiteUserContext()));

        var jsonModels = from user in ctxUserList
                         select new
                         {
                             userName = user.UserName,
                             Roles = (from userRole in user.Roles
                                      join role in ctx.Roles on userRole.RoleId
                                      equals role.Id
                                      select role.Name).ToList(),
                             id = user.Id
                         };

Getting just a list of users is fine, about 600ms for 100 users. 仅获取用户列表是可以的,对于100个用户而言大约为600ms。 But as soon as I try and add in the roles, I end up waiting 5-10 seconds. 但是,一旦我尝试添加角色,我最终将等待5-10秒。 Each user only has 1 or 2 roles. 每个用户只有1个或2个角色。 This isn't exactly a huge query. 这并不是一个巨大的查询。

I tried using the userManager GetRolesById(user.Id) but that was even slower. 我尝试使用userManager GetRolesById(user.Id)但这甚至更慢。 10+ seconds. 10+秒。

Any tips on making this run quickly would be greatly appreciated. 任何使此快速运行的提示将不胜感激。

What you have here is executing N+1 queries to get the information since you get the list of users, then enumerate the list of users to streamline. 因为您已获得用户列表,所以这里执行的是N + 1查询以获取信息,然后枚举要简化的用户列表。 Using LINQ we can do this a bit more efficiently. 使用LINQ,我们可以更有效地做到这一点。 I've used this before to do this in one process. 我以前曾使用过此功能,但现在只需要一个过程即可。

var usersWithRoles = (from user in ctx.Users  
                      select new  
                       {  
                        UserId = user.Id,                                        
                        Username = user.UserName,  
                        Email = user.Email,  
                        RoleNames = (from userRole in user.Roles  
                                     join role in context.Roles on userRole.RoleId   
                                     equals role.Id  
                                     select role.Name).ToList()  
                                  })
                    .ToList()
                    .Select(p => new   
                    {  
                       UserId = p.UserId,  
                       Username = p.Username,  
                       Email = p.Email,  
                       Role = string.Join(",", p.RoleNames)  
                    });  

This should be much faster and get it with 1 query! 这应该更快,并通过1个查询获得它!

Edit: Looking at your request, if you just want the roles as a list, you can skip the second projection from this example. 编辑:查看您的请求,如果只想将角色作为列表,则可以跳过此示例的第二个投影。 (I had a need to display this in a list, thus the string.Join in the example. (我需要将其显示在列表中,因此显示为字符串。在示例中加入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM