데이터베이스에 저장된 문자열 (예 : "(x + y) * 4")을 가져 와서 x 값을 가져 오는 간단한 계산 엔진이 있습니다. y를 데이터베이스에서 가져 와서 계산을 수행하고 그 결과를 데이터베이스에 저장합니다. 이것이 너무 오래 걸리는 것 같아요. 나는 Linq 함정에 들어갔을 까봐 두렵습니다. 이 문제를 개선 할 수있는 방법이 있으면 알려주십시오.Linq 쿼리를 최적화하고 속도 값을 업데이트하는 방법
public Nullable<decimal> CalculateFormulaByYearDistrict(int formulaId, int fundingYearId, int districtId)
{
string formulaText = "";
decimal? retValue = null;
using (STARSEntities context = new STARSEntities())
{
var formulaItems = from fi in context.STARS_FormulaItem
where fi.FormulaId == formulaId
select fi;
STARS_Formula formula = formulaItems.FirstOrDefault().STARS_Formula;
formulaText = formula.FormulaText;
foreach (STARS_FormulaItem formulaItem in formulaItems)
{
int accountingItemId = formulaItem.AccountingItemId;
var itemValue = (from iv in context.AccountingItemValues
join di in context.STARS_DistrictInputData
on iv.DomainSpecificId equals di.DistrictInputDataId
where (di.DistrictId == districtId || di.DistrictId == -1) //District -1 is the invalid and universal district for coefficients
&& di.DomainYearReportingPeriod.FundingYearId == fundingYearId
&& iv.AccountingItemId == accountingItemId
select iv).SingleOrDefault();
//If no value exists for the requested Assessment Item Value, then force an error message into the formula text
//to be thrown during calculate.
if (itemValue != null)
formulaText = Regex.Replace(formulaText, @"\b" + formulaItem.ItemCode + @"\b", itemValue.Amount.ToString());
else
formulaText = Regex.Replace(formulaText, @"\b" + formulaItem.ItemCode + @"\b", "No Value Exists for " + formulaItem.ItemCode);
}
switch (formula.FormulaTypeId)
{
case (int)FormulaType.CALC:
retValue = Calculate(formulaText);
break;
case (int)FormulaType.EXPONENT:
// pull the number directly after e and and calculate the Math.Exp(value) and then push that into the calculation.
retValue = Calculate(ReplaceExponent(formulaText));
break;
case (int)FormulaType.IFTHEN:
// evaluate the If statements and pass any math to the calculator.
retValue = Calculate(EvaluateIf(formulaText));
break;
default:
break;
}
}
return retValue;
}
public bool CalculateAndSaveResults(DistrictDataCategory category, List<int> districtIds, int fundingYearId, int userId)
{
//Optimization Logic
DateTime startTime = DateTime.Now;
Debug.WriteLine("Starting Calculate and Save at:" + startTime.ToString());
using (STARSEntities context = new STARSEntities())
{
var formulas = from f in context.STARS_FormulaCategory
where f.DistrictDataCategoryId == (int)category
select f.STARS_Formula;
foreach (var districtId in districtIds)
{
Debug.WriteLine("District: " + districtId.ToString());
DateTime districtStartTime = DateTime.Now;
foreach (var formula in formulas)
{
var itemValue = (from iv in context.AccountingItemValues
join di in context.STARS_DistrictInputData
on iv.DomainSpecificId equals di.DistrictInputDataId
where (di.DistrictId == districtId)
&& di.DomainYearReportingPeriod.FundingYearId == fundingYearId
&& iv.AccountingItemId == formula.ResultAccountingItemId
select new { iv, di }).SingleOrDefault();
itemValue.iv.Amount = CalculateFormulaByYearDistrict(formula.FormulaId, fundingYearId, districtId);
//Update Actual Amount Record
itemValue.iv.LastUpdated = DateTime.Now;
itemValue.iv.UpdatedBy = userId;
//Update District Data Import Record
itemValue.di.LastUpdated = DateTime.Now;
itemValue.di.UpdatedBy = userId;
}
Debug.WriteLine("District Calculation took: " + ((TimeSpan)(DateTime.Now - districtStartTime)).ToString() + "for " + districtId.ToString());
}
context.SaveChanges();
}
Debug.WriteLine("Finished Calculate and Save at:" + ((TimeSpan)(DateTime.Now - startTime)).ToString());
return true;
}
기본 데이터 구조에 대한 정보가 필요하면 알려주십시오. 중요하게 보이는 점은 주어진 지구의 특정 유형에 대한 모든 계산을 수행 할 수 있도록 수식 텍스트를 저장하는 수식 표 사이에 연관 개체가 있다는 것입니다. 저장된 실제 값은 AccountingItemValue 테이블에 있지만 회계 항목 값에 대한 위치 정보가있는 DistrictInputData라는 관련 테이블이 있습니다.
대단히 감사합니다.
http://en.wikipedia.org/wiki/Inner-platform_effect – asawyer