请建议我哪个选项最好。
我有一个表Student(id,name,create_date)。它有太多的记录(假设数百万)。 我有另一个名为student_stats(month,year,student_count)的表,现在是空的。我将查看我的学生表并填写student_stats表。
student_stats会有这样的数据
month year count
jan 2000 5
feb 2000 4
...
jan 2015 20
选项1:
List<MonthYearCount> monthYearCounts = select month(create_date), year(create_date), count(*) from Student where create_date between '2000-01-01' and '2015-02-01' group by month, year;
for(singleMonthYear : all month between '2000-01-01' and '2015-02-01'){
MonthYearCount monthYearCount = monthYearCounts.get(singleMonthYear);
studentStats = new StudentStats();
studentStats.setMonth(monthYearCount.getMonth())
studentStats.setYear(monthYearCount.getYear());
studentStats.setCount(monthYearCount.getCount());
studentStats.insertToDB();
}
class MonthYearCount{
int month;
int year;
int count;
}
选项2:
List<Student> students = select * from Student where create_date between '2000-01-01' and '2015-02-01;
for(singleMonthYear : all month between '2000-01-01' and '2015-02-01'){
List<Student> filtered = new ArrayList<>();
for(student : students){
if(student.getCreateDate() is between singleMonthYear i.e. 2012-05-01 to 2012-05-31){
filtered.add(student);
}
}
studentStats = new StudentStats();
studentStats.setMonth(singleMonthYear.getMonth())
studentStats.setYear(singleMonthYear.getYear());
studentStats.setCount(filtered.size());
studentStats.insertToDB();
}
提前致谢!