简体   繁体   English

优化 ef-core 查询

[英]Optimize ef-core query

does anyone have any ideas how to improve or optimize this query in terms of performance?有没有人知道如何在性能方面改进或优化此查询? An Include cannot be used due to missing Foreign Keys / Navigation Properties because this is a scaffolded model.由于缺少外键/导航属性,无法使用包含,因为这是一个脚手架 model。

using (var session = new Typo3DBContext())
            {
                var countryList = session.TxNeustageodataDomainModelCountry
                            .Where(x => x.Deleted == 0)
                            .Join(session.TxNeustameinereiseDomainModelTripCountryMm,
                                    country => (uint)country.Uid,
                                    tripMM => tripMM.UidForeign,
                                    (country, tripMM) =>
                                        new
                                        {
                                            country = country,
                                            tripMM = tripMM
                                        })
                            .Join(session.TxNeustameinereiseDomainModelTrip,
                                    combinedEntry => combinedEntry.tripMM.UidLocal,
                                    trip => trip.Uid,
                                    (combinedEntry, trip) =>
                                        new
                                        {
                                            combinedEntry = combinedEntry,
                                            trip = trip
                                        })
                            .GroupBy(
                                temp =>
                                    new
                                    {
                                        Name = temp.combinedEntry.country.Name,
                                        Iso = temp.combinedEntry.country.Iso,
                                        Id = temp.combinedEntry.tripMM.UidForeign,
                                        Status = temp.trip.Status,
                                        Deleted = temp.trip.Deleted
                                    },
                                temp => temp.combinedEntry.tripMM
                            )
                            .Where(x => x.Key.Status == 2 && x.Key.Deleted == 0)
                            .Select(
                                group =>
                                    new CountryHelperClass
                                    {
                                        Count = group.Count(),
                                        Iso = group.Key.Iso,
                                        Name = group.Key.Name,
                                        Id = group.Key.Id

                                    })
                            .ToList();

                return countryList;
            }

You may analyze the generated SQL first and see if optimal sql is being generated.您可以先分析生成的SQL,看看是否生成了最优的sql。 you may follow the this link to start.您可以点击链接开始。 Another good tool to work with linq queries is to use LINQPad .使用 linq 查询的另一个好工具是使用LINQPad Some of the common issue with Linq queries are Linq 查询的一些常见问题是

  • The 'N+1 Select' problem (If you are using ef core 3 This and other sql related issue re being optimized): 'N+1 Select' 问题(如果您使用的是 ef core 3 This 和其他 sql 相关问题正在优化):
  • To greedy with row and columns贪婪的行和列
  • Change Tracking related issues变更跟踪相关问题
  • Missing indexes缺少索引

Details of these issue can be found in above link an on internet also这些问题的详细信息可以在上面的链接中找到,也可以在互联网上找到

Normally i go for stored procedure approach for complex queries as it saves lot of time of optimization of queries通常我 go 用于复杂查询的存储过程方法,因为它节省了大量优化查询的时间

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

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