0

我有以下使用 EF5 和通用存储库的 LINQ 查询,工作单元模式到 SQL Server 2008 db

        var countriesArr = GetIdsFromDelimStr(countries);
        var competitionsArr = GetIdsFromDelimStr(competitions);
        var filterTeamName = string.Empty;

        if (teamName != null)
        {
            filterTeamName = teamName.ToUpper();
        }

        using (var unitOfWork = new FootballUnitOfWork(ConnFooty))
        {

            // give us our selection of teams
            var teams =
                (from team in
                     unitOfWork.TeamRepository.Find()
                 where ((string.IsNullOrEmpty(filterTeamName) || team.Name.ToUpper().Contains(filterTeamName)) &&
                        (countriesArr.Contains(team.Venue.Country.Id) || countriesArr.Count() == 0))
                 select new
                            {
                                tId = team.Id
                            }).Distinct();

            // give us our selection of contests
            var conts = (
                            from cont in
                                unitOfWork.ContestRepository.Find(
                                    c =>
                                    ((c.ContestType == ContestType.League && competitionsArr.Count() == 0) ||
                                     (competitionsArr.Contains(c.Competition.Id) && competitionsArr.Count() == 0)))
                            select new
                                       {
                                           contId = cont.Id
                                       }
                        ).Distinct();

            // get selection of home teams based on contest
            var homecomps = (from fixt in unitOfWork.FixtureDetailsRepository.Find()
                             where
                                 teams.Any(t => t.tId == fixt.HomeTeam.Id) &&
                                 conts.Any(c => c.contId == fixt.Contest.Id)
                             select new
                                        {
                                            teamId = fixt.HomeTeam.Id,
                                            teamName = fixt.HomeTeam.Name,
                                            countryId = fixt.HomeTeam.Venue.Country.Id != null ? fixt.HomeTeam.Venue.Country.Id : 0,
                                            countryName = fixt.HomeTeam.Venue.Country.Id != null ? fixt.HomeTeam.Venue.Country.Name : string.Empty,
                                            compId = fixt.Contest.Competition.Id,
                                            compDesc = fixt.Contest.Competition.Description
                                        }).Distinct();

            // get selection of away teams based on contest
            var awaycomps = (from fixt in unitOfWork.FixtureDetailsRepository.Find()
                             where
                                 teams.Any(t => t.tId == fixt.AwayTeam.Id) &&
                                 conts.Any(c => c.contId == fixt.Contest.Id)
                             select new
                             {
                                 teamId = fixt.AwayTeam.Id,
                                 teamName = fixt.AwayTeam.Name,
                                 countryId = fixt.AwayTeam.Venue.Country.Id != null ? fixt.AwayTeam.Venue.Country.Id : 0,
                                 countryName = fixt.AwayTeam.Venue.Country.Id != null ? fixt.AwayTeam.Venue.Country.Name : string.Empty,
                                 compId = fixt.Contest.Competition.Id,
                                 compDesc = fixt.Contest.Competition.Description
                             }).Distinct();

            // ensure that we return the max competition based on id for home teams
            var homemax = (from t in homecomps
                           group t by t.teamId
                               into grp
                               let maxcomp = grp.Max(g => g.compId)
                               from g in grp
                               where g.compId == maxcomp
                               select g).Distinct();

            // ensure that we return the max competition based on id for away teams
            var awaymax = (from t in awaycomps
                           group t by t.teamId
                               into grp
                               let maxcomp = grp.Max(g => g.compId)
                               from g in grp
                               where g.compId == maxcomp
                               select g).Distinct();

            var filteredteams = homemax.Union(awaymax).OrderBy(t => t.teamName).AsQueryable();

如您所见,我们希望返回传递给 WebAPI 的以下格式,因此我们将结果转换为我们可以在 UI 中关联的类型。

从本质上讲,我们要做的是让主队和客队从赛程中脱颖而出,这些赛程有一场与比赛相关的比赛。然后,我们从分组中获得最高的比赛 ID,然后将其与该团队一起返回。该国家/地区与基于场地ID的团队相关,当我最初这样做时,我在弄清楚如何做或加入linq时遇到了问题,这就是为什么我将其分解为获得主队和客队,然后根据他们进行分组在竞争中,然后将它们联合在一起。

当前表格大小的一个想法是夹具有 7840 行,团队有 8581 行,比赛有 337 行,比赛有 96 行。可能会迅速增加的桌子是夹具桌,因为这与足球有关。

我们想要最终得到的输出是

团队 ID、团队名称、国家 ID、国家名称、比赛 ID、比赛名称

不使用过滤此查询平均需要大约 5 秒,只是想知道是否有人对如何使其更快有任何想法/指示。

提前谢谢马克

4

1 回答 1

1

我无法判断它是否会加快速度,但您的homemax查询awaymax可能是

var homemax = from t in homecomps
              group t by t.teamId into grp
              select grp.OrderByDescending(x => x.compId).FirstOrDefault();

var awaymax = from t in awaycomps
              group t by t.teamId into grp
              select grp.OrderByDescending(x => x.compId).FirstOrDefault();

此外,当您编写一个非常大的查询时,当您将其分成几个较小的查询来获取中间结果时,它可能会执行得更好。有时,与数据库引擎找不到好的执行计划的一个非常大的查询相比,多几次到数据库的往返性能要好。

另一件事是所有这些Distinct()。你总是需要它们吗?我认为您可以不这样做,因为您总是从一个表中获取数据而不加入子集合。删除它们可能会节省一堆。

另一个优化可能是删除ToUpper. 比较是由 SQL 中的数据库引擎完成的,并且很可能数据库具有不区分大小写的排序规则。如果是这样,即使您希望比较也不区分大小写!诸如Name.ToUpper取消使用任何索引的构造Name(它不是sargable)。

于 2013-06-08T22:59:21.733 回答