1

Let me change the question like this:

Which one of the below modes would increase the performance of an application?

  1. A method in C# which performs about 6 JOIN statements and returns a value.
  2. An stored procedure which performs 6 JOIN statements

Which of the above would be more efficient in terms of application performance?

Here is the Code:

public bool UserHasAccess(string userName, string actionName)
    {
        var decodedUserName = UtilityHands.GeneralTools.DecodeString(userName);
        using (MAHAL_E_MA_Repository.UnitOfWork unit = new UnitOfWork())
        {
            var theUserID = unit.UserRepository.Get(filter: s => s.UserName.Equals(decodedUserName)).First().ID;
            var userInRoleGroup = unit.RoleGroupUserRepository.Get(filter: r => r.UserInRole == theUserID).ToList();

            var roleInRoleGroups = unit.RoleOfRoleGroupRepository.Get().ToList();
            var serviceOfRoles = unit.ServiceOfRoleRepository.Get().ToList();
            var serviceOfAction = unit.ServiceOfActionRepository.Get().ToList();
            var roles = unit.RoleRepository.Get().ToList();
            var service = unit.ServiceRepository.Get().ToList();
            var actions = unit.ActionProviderRepository.Get().ToList();

            var rolesOfRoleGroupJoin = userInRoleGroup.Join(roleInRoleGroups,
                                            (u => u.RoleGroupID),
                                            (r => r.RoleGroupID),
                                            ((u, r) => new { userInRoleGroup = u, roleInRoleGroups = r }));

            var rrgOfRolesJoin = rolesOfRoleGroupJoin.Join(roles,
                                                      (r => r.roleInRoleGroups.RoleID),
                                                      (rs => rs.RoleID),
                                                      ((r, rs) => new { rolesOfRoleGroupJoin = r, roles = rs }));

            var roleServiceJoin = rrgOfRolesJoin.Join(serviceOfRoles,
                                                    (rrg => rrg.roles.RoleID),
                                                    (sor => sor.RoleID),
                                                    ((rrg, sor) => new { rrgOfRolesJoin = rrg, serviceOfRoles = sor }));

            var serviceOfRolesServiceJoin = roleServiceJoin.Join(service,
                                                                (rs => rs.serviceOfRoles.ServiceID),
                                                                (s => s.ServiceID),
                                                                ((rs, s) => new { roleServiceJoin = rs, service = s }));

            var serviceActionJoin = serviceOfRolesServiceJoin.Join(serviceOfAction,
                                                                  (sors => sors.service.ServiceID),
                                                                  (soa => soa.ServiceID),
                                                                  ((sors, soa) => new { serviceOfRolesServiceJoin = sors, serviceOfAction = soa }));

            var serviceActionWithActionJoin = serviceActionJoin.Join(actions,
                                                                    (sa => sa.serviceOfAction.ActionProviderID),
                                                                    (a => a.ActionProviderID),
                                                                    ((sa, a) => new { serviceActionJoin = sa, actions = a }));

            var actionNames = serviceActionWithActionJoin.Where(s => s.actions.Description.Equals(actionName));

            if (actionNames != null && actionNames.Count() > 0)
            {
                return true;
            }
            return false;
        }
    }

Thank you

4

1 回答 1

3

我假设使用“`C# 中的一种方法,它执行大约 6 个 JOIN 语句并返回一个值。” 您在 Linq-to-SQL 或 Linq-to-Entities 中执行此操作,将所有连接转换为访问数据库的一个查询。

在这种情况下,不会有明显的差异。对于 SP 和动态生成的查询,查询执行计划以相同的方式缓存(SP 曾经具有更好的性能,但这种差异在几年后就被消除了)。

如果您通过从数据库中检索单独的结果来“在内存中”进行连接,您将获得非常非常糟糕的性能。

于 2013-06-22T11:48:54.007 回答