提高EF查询性能

时间:2012-06-21 15:18:54

标签: entity-framework

我目前在我的项目中有以下代码

   public List<PermissionValue> GetUnderlyingPermissionsForUser(string userName, int guiPermissionTypeId, int productTypeId)
    {
        using (CliVeEntities db = new CliVeEntities())
        {             
            var listUnderlyings = (from gui in db.GuiPermissionUnderlying
                                join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID     
                                join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
                                where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
                                && gui.UnderlyingId.HasValue
                                && !gui.SectorId.HasValue
                                && gui.ProductTypeId == null
                                && gui.ProductGroupId == null
                                select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value}).ToList<PermissionValue>();


            var listUnderlyingsProductGroup = (from gui in db.GuiPermissionUnderlying
                                                join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
                                               join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
                                               join pg in db.ProductGroup on gui.ProductGroupId equals pg.ProductGroupId
                                               join pt in db.ProductType on pg.ProductGroupId equals pt.ProductGroupId
                                               where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
                                               && gui.UnderlyingId.HasValue
                                               && !gui.SectorId.HasValue
                                               && pt.ProductTypeId == productTypeId
                                               select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value }).ToList<PermissionValue>();




            var listUnderlyingsProductType = (from gui in db.GuiPermissionUnderlying
                                                join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
                                              join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
                                              where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
                                              && gui.UnderlyingId.HasValue
                                              && !gui.SectorId.HasValue
                                              && gui.ProductTypeId == productTypeId
                                              select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value }).ToList<PermissionValue>();


            var listUnderlyingsForSectors = (from gui in db.GuiPermissionUnderlying
                                             join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
                                             join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
                                             join u in db.Underlying on gui.SectorId equals u.SectorId
                                             where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
                                             && !gui.UnderlyingId.HasValue
                                             && gui.ProductTypeId == null
                                             && gui.ProductGroupId == null
                                             select new PermissionValue { Id = u.UnderlyingId, Visible = gui.value }).ToList<PermissionValue>();


            var listUnderlyingsForSectorsProductGroup = (from gui in db.GuiPermissionUnderlying
                                                        join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
                                                         join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
                                                         join u in db.Underlying on gui.SectorId equals u.SectorId
                                                         join pg in db.ProductGroup on gui.ProductGroupId equals pg.ProductGroupId
                                                         join pt in db.ProductType on pg.ProductGroupId equals pt.ProductGroupId
                                                         where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
                                                         && !gui.UnderlyingId.HasValue
                                                         && pt.ProductTypeId == productTypeId
                                                         select new PermissionValue { Id = u.UnderlyingId, Visible = gui.value }).ToList<PermissionValue>();

            var listUnderlyingsForSectorsProductType = (
                                                        from gui in db.GuiPermissionUnderlying
                                                        join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
                                                        join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
                                                        where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
                                                        && !gui.UnderlyingId.HasValue
                                                        && gui.SectorId.HasValue
                                                        && gui.ProductTypeId == productTypeId
                                                        select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value }).ToList<PermissionValue>();

            var mergeList1 = MergeAndDistinctList(listUnderlyings, listUnderlyingsProductType);
            var mergeList2 = MergeAndDistinctList(mergeList1, listUnderlyingsProductGroup);
            var mergeList3 = MergeAndDistinctList(mergeList2, listUnderlyingsForSectors);
            var mergeList4 = MergeAndDistinctList(mergeList3, listUnderlyingsForSectorsProductType);
            var resultMergeList = MergeAndDistinctList(mergeList4, listUnderlyingsForSectorsProductGroup);

            return resultMergeList;

这是我的MergeAndDistinctList函数的代码

 protected List<PermissionValue> MergeAndDistinctList(List<PermissionValue> listPrimary, List<PermissionValue> listSecondry)
    {
        List<PermissionValue> listMergedAndDistinct = new List<PermissionValue>();

        listMergedAndDistinct.AddRange(listPrimary);

        var filter = listPrimary.Select<PermissionValue, int>(p => p.Id);
        listMergedAndDistinct.AddRange(listSecondry.Where<PermissionValue>(p => !filter.Contains(p.Id)).Select(p => p));
        return listMergedAndDistinct;
    }

我的GetUnderlyingsForClient()代码的问题是它多次访问数据库。如果这种方法受到很大影响,那么这可能会转化为对数据库的多次调用。

是否有人知道如何提高此代码的效率并减少我必须进行的数据库调用。

2 个答案:

答案 0 :(得分:0)

你似乎在同一张桌子上做了很多查询(在你的情况下是一个)

要限制数据库查询,您可以获取本地列表中所需的所有数据,并将此列表用于多个查询,如下所示:

var MyBaseList = db.A.ToList();

var listUnderlyings = (from a MyBaseList [...]
var listUnderlyingsProductGroup = (from a MyBaseList [...]

为限制数据检索,过滤MyBaseList以仅获取后续查询使用的行。

答案 1 :(得分:0)

您可以按照以下简化模式Union查询:

(from a in As where a.Id > 0 select a)
.Union(
from a in As where a.Name == "a" select a)
.Union(
...
)
.Select(a => new { a.x, a.y })
.Distinct()

Union是MergeAndDistinctList和Distinct的合并部分,呃,是不同的部分。好处是:Distinct是在投影上进行的(在你的情况下是Id和Visible)。

现在一切都应该在一个(怪物)查询中执行,而不会由MergeAndDistinctList中的AddRange引起中间强制执行。

查看你的查询我觉得你可以通过结合一些条件来减少它们的数量,但这取决于你。