问题定义: 我有一个包含数百万个条目的数据库表。该表由以下几列定义:Date,Value和NumberOfEvents。我需要获取几个时间间隔之间的数据,并对每个间隔的所有返回记录应用一个函数(求和,平均值,最大值,最小值)。
当前解决方案:
List<SingleSeries> series = new List<SingleSeries>();
//Iterate intervals
for (int i = 0; i < intervals.Count(); i++)
{
if (i == 0)
{
}
if (i + 1 < intervals.Count())
{
//Get database entries between interval
var validKpis = kpis.Where(x => x.Date >= intervals.ElementAt(i) && x.Date < intervals.ElementAt(i + 1));
//Apply calc operation (SUM; AVG; MAX;MIN)
var result= CalcOperations.Calculate(calcOperation, validKpis);
//Add result to results array
series.Add(new SingleSeries()
{
Value = result,
Name = DateOperations.ConvertToMilliseconds(intervals.ElementAt(i + 1)).ToString()
});
};
}
return series;
当前问题: 当前,此函数对数据库执行N个查询,其中N = intervals.Count()/ 2。我希望它仅执行一个查询,但我无法弄清楚如何使用间隔数组来汇总每个间隔的值 并在其上应用calc操作它。 怀孕吗?
答案 0 :(得分:0)
您需要构建一个查询,该查询将获取所有间隔的结果,然后循环遍历间隔以将结果集拆分为每个特定间隔的结果。
更多代码,但更有效地使用数据库请求。
下面是一个粗略的代码段,用于演示以上内容-请注意,我尚未对此进行测试-只是为了让您具体了解如何通过单个查询执行此操作。
List<SingleSeries> series = new List<SingleSeries>();
var kpiQuery = kpis;
//Iterate intervals and build a single query to retrieve all of them
for (int i = 0; i < intervals.Count(); i++)
{
if (i + 1 < intervals.Count())
{
// add each target interval sequentially - without running the query yet
kpiQuery = kpiQuery.Where(x => x.Date >= intervals.ElementAt(i) && x.Date < intervals.ElementAt(i + 1));
}
}
var validKpis = kpiQuery.ToList(); // now get all database results in a single transaction
//Iterate the intervals again, this time to split the combined results that are now in memory
for (int i = 0; i < intervals.Count(); i++)
{
if (i + 1 < intervals.Count())
{
//select only the results applicable to the current interval
var kpisInThisInterval = validKpis.Where(x =>
x.Date >= intervals.ElementAt(i) && x.Date < intervals.ElementAt(i + 1));
//Apply calc operation (SUM; AVG; MAX;MIN)
var result = CalcOperations.Calculate(calcOperation, kpisInThisInterval);
//Add result to results array
series.Add(new SingleSeries()
{
Value = result,
Name = DateOperations.ConvertToMilliseconds(intervals.ElementAt(i + 1)).ToString()
});
}
}
return series;