Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
var result= from VD in VisitDetails 
join PB in PatientBill on VD.VisitId equal PB.VisitId 
group new
{
PB
}
by new
{
VD.VisitCode
}
into data
select new
{
VisitCode= data.Key.VisitCode,
PatientPayable= data.sum(x=> x.PB.PatientPayable),
CompanyPayable= data.sum(x=> x.PB.CompanyPayable),
NeyPayable= data.Sum(item=> item.PB.PatientBillDetails.Sum(x=> x.NetPayable)
}


What I have tried:

I am getting correct output from this query but it's slow because of NetPayable Calculation. So please help me to optimize this query.
Posted
Updated 17-Dec-16 12:02pm
Comments
Jon McKee 15-Dec-16 4:05am    
I'm still not sure this is what you want from your previous post here but you've optimized this as much as you can expect. The calculation of NetPayable demands an iteration over PatientBillDetails and subsequently an iteration over the summed value.
Ehsan Sajjad 15-Dec-16 5:52am    
what you can do is have another calcs table where the pre calculated data is there, and just select it top use then
Philippe Mori 15-Dec-16 9:44am    
You might try to split the request in 2 or 3 parts and see if it is better to have a few optimized requests that a single one not optimal...

By the way, I assume that you already check that the generated request was not degenerating in a bunch of request (for your specific provider).

To expand on what Ehsan Sajjad said in his comment, sometimes to improve performance you have to do some precalculations instead of leaving it all to be calculated at the very last moment. This way you benefit from amortization of time it costs to do that calculation.

So here's a suggestion for optimizing only NetPayable at the PatientBill level, which can be done quite easily without adding a new table. Instead just add a new column to your existing PatientBill table and a few steps whenever you add/change the PatientBillDetails for a particular PatientBill record as described in more detail here:

- Add "RunningNetPayable" column, which will be the running total of the PatientBillDetails items' NetPayable.
- You must update this "RunningNetPayable" value whenever an insert or change is made to an entry in your PatientBill's PatientBillDetails records, such that it is the sum of the constituent PatientBillDetails's NetPayable. So at the code that inserts/changes the PatientBillDetails, also make it update the PatientBill's "RunningNetPayable" -or- if your database supports 'triggers' you could make use of it to do this for you too.
- Then you can use a slightly modified query:
var result= from VD in VisitDetails 
join PB in PatientBill on VD.VisitId equal PB.VisitId 
group new
{
PB
}
by new
{
VD.VisitCode
}
into data
select new
{
VisitCode= data.Key.VisitCode,
PatientPayable= data.sum(x=> x.PB.PatientPayable),
CompanyPayable= data.sum(x=> x.PB.CompanyPayable),
NetPayable= data.Sum(item=> item.PB.RunningNetPayable)
}


However, if you want to make it even faster. You could precalculate everything (PatientPayable, CompanyPaybable and NetPayable) anytime values are added/changed and stored in a separate table, like Ehsan Sajjad said.
 
Share this answer
 
v3
Please, see my previous answer: Detail table sum in linq query[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900