简体   繁体   中英

Is my Linq Query Optimal and High performance in EF Core 3.1?

can i remove vg.First().Voucher? and replace the beter code? what is the optimal and best practice? is convertable this code to another method? like chain method?

        var query = from v in _journalLineRepository.TableNoTracking
                .Include(j => j.Voucher).AsEnumerable()
                    group v by v.AccountId into vg
                    select new // <-- temporary projection with group by fields needed
                    {
                        AccountId = vg.Key,
                        Credit = vg.Sum(v => v.Credit),
                        Debit = vg.Sum(v => v.Debit),
                        Voucher = vg.First().Voucher

                    } into vg
                    join p in _partyRepository.TableNoTracking.Include(p => p.PartyPhones).AsEnumerable() on vg.AccountId equals p.AccountId // <-- additional join(s)
                    select new PartyDeptorAndCreditorViewModel
                    {



                        PartyId = p.Id,
                        FullName = p.FullName,
                        PhoneNo = p.PartyPhones.FirstOrDefault(p => p.IsActive)?.Phone,
                        ProjectId = vg.Voucher.ProjectId,
                        AccountId = vg.AccountId.Value,
                        Creditor = vg.Credit,
                        Deptor = vg.Debit,
                        Balance = vg.Credit - vg.Debit,
                        VoucherDate = vg.Voucher.VoucherDate,
                        VoucherRegisterDate = vg.Voucher.VoucherDate,
                        BalanceType =
                            vg.Debit > vg.Credit ? AccountingComplexEnum.ShowPartyBalanceParamSearch.Deptor.ToDisplay(DisplayProperty.Name) :
                            vg.Debit < vg.Credit ? AccountingComplexEnum.ShowPartyBalanceParamSearch.Creditor.ToDisplay(DisplayProperty.Name) :
                             AccountingComplexEnum.ShowPartyBalanceParamSearch.ZeroBalance.ToDisplay(DisplayProperty.Name),

                    };

I'd certainly be looking at the SQL query generated. At face value I see a few warning flags that it may not be composing a query but possibly pre-executing to in-memory processing which would be inefficient. It would firstly depend on what these .TableNoTracking methods/properties return, and the use of .AsEnumerable on the eager load joins.

Firstly, when projecting with Select , eager load joins ( .Include ) are not necessary. The projections will take care of the joins for you, provided it is projecting down to SQL. If you take out the .Include().AsEnumerable() calls and your query still works then it is likely projecting down to SQL. If it is no longer working then it's processing in memory and not efficiently.

Edit: Nope, the inner projection won't resolve: Regarding the .Voucher , your final projection is using 2 fields from this entity, so it stands you could replace this in the initial projection:

 select new // <-- temporary projection with group by fields needed { AccountId = vg.Key, Credit = vg.Sum(v => v.Credit), Debit = vg.Sum(v => v.Debit), Voucher = vg.Select(v => new { v.ProjectId, v.VoucherDate }).First() } into vg

When it comes to transformations like this:

BalanceType = vg.Debit > vg.Credit 
    ?        AccountingComplexEnum.ShowPartyBalanceParamSearch.Deptor.ToDisplay(DisplayProperty.Name) 
    : vg.Debit < vg.Credit 
        ? AccountingComplexEnum.ShowPartyBalanceParamSearch.Creditor.ToDisplay(DisplayProperty.Name) 
        : AccountingComplexEnum.ShowPartyBalanceParamSearch.ZeroBalance.ToDisplay(DisplayProperty.Name),

... inside a projection, this sends off warning flags as Linq2EF needs to compose projections down to SQL so methods/extensions like ToDisplay won't be understood. Instead, since this is based solely on the Credit/Debit amounts, I'd move this to be computed by the property in the view model:

select new PartyDeptorAndCreditorViewModel
{
    PartyId = p.Id,
    FullName = p.FullName,
    PhoneNo = p.PartyPhones
        .Where(p => p.IsActive)
        .Select(p => p.Phone)
        .FirstOrDefault(),
    ProjectId = vg.Voucher.ProjectId,
    AccountId = vg.AccountId.Value,
    Creditor = vg.Credit,
    Deptor = vg.Debit,
    Balance = vg.Credit - vg.Debit,
    VoucherDate = vg.Voucher.VoucherDate,
    VoucherRegisterDate = vg.Voucher.VoucherDate
};

Then in the view model:

[Serializable]
public class PartyDebtorAndCreditorViewModel
{
    // ... 
    public decimal Balance { get; set; }

    public string BalanceType 
    {
        get 
        { 
             return Balance < 0 
                ? AccountingComplexEnum.ShowPartyBalanceParamSearch.Deptor.ToDisplay(DisplayProperty.Name) 
                : Balance > 0
                    ? AccountingComplexEnum.ShowPartyBalanceParamSearch.Creditor.ToDisplay(DisplayProperty.Name) 
                    : AccountingComplexEnum.ShowPartyBalanceParamSearch.ZeroBalance.ToDisplay(DisplayProperty.Name);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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