dev.bartels
dev.bartels

Reputation: 60

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.

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;
            }

Upvotes: 0

Views: 860

Answers (1)

Tassadaque
Tassadaque

Reputation: 8199

You may analyze the generated SQL first and see if optimal sql is being generated. you may follow the this link to start. Another good tool to work with linq queries is to use LINQPad. Some of the common issue with Linq queries are

  • The ‘N+1 Select’ problem (If you are using ef core 3 This and other sql related issue re being optimized):
  • 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

Upvotes: 1

Related Questions