Let's start by stating:
The first run was too slow, processing more than 9,600 employee data, took 81 minutes!!
The code is also very simple, mainly to get data-"Comparative analysis-" Insert analysis results into the database. The EF operation mode is used.
Public voidAllocateallplans () {varEmployees = _efworker.employeerepository.findall (Filter:p = P.status.trim (). ToUpper () = ="ACTIVE"); varPlans = _efworker.benefitplanrepository.findall (includeproperties:"planeligibility", orderby:query = query. (p = p.priority), filter:p = P.status.trim (). ToUpper () = ="ACTIVE"); if(Employees = =NULL|| Plans = =NULL) return; Allocateplanforemployee (employees, plans); _efworker.savechanges (); } /// <summary> ///generate planentitlement and account initial data based on input employee and schedule/// </summary> /// <param name= "Employees" ></param> /// <param name= "plans" ></param> Public voidAllocateplanforemployee (ienumerable<employee> employees, ienumerable<benefitplan>plans) { //Initialize the employee points account with a default score of 0Accountinghelper Accountinghelper =NewAccountinghelper (_efworker); foreach(varEmployeeinchemployees) { //Verify basic information about business logic firstlist<string>errormsg; if(!_insuredinfovalidator.validateemployeeinfo (Employee, outerrormsg)) Throw NewException (string. Join (@"\ n", Errormsg.toarray ())); BOOLJoinedplan =false; foreach(varPlaninchplans) { varEligibilitys =plan. planeligibility; if(_eligibilityevaluator.matchplan (employee, Eligibilitys)) {//Add the employee's corresponding data to the schedule to the table Planentitlement varPlanentitlement =Newplanentitlement {Id=Guid.NewGuid (), PlanId=plan. Id, EmployeeId=employee. Id, Joinplandate=DateTime.Now,//selectionopendate = plan. Selectionopendate,//selectionclosedate = plan. Selectionclosedate,Status ="ACTIVE" }; _efworker.planentitlementrepository.insert (planentitlement); Accountinghelper.initaccount (planentitlement.id, employee. Id, 0m,"EE"); Joinedplan=true; Break;//by priority, employees are assigned to a plan to jump out of the plan matching cycle } } if(Joinedplan = =false) { Throw NewException ("Employee not match any plan,employeeid="+employee. Id.tostring ()); } } }
The code is ugly, everyone will see, there is any improvement suggestions please. Thank you for your advice!
The first optimization is to remove the original data trace from the query:
PublicIenumerable<tentity> FindAll (expression<func<tentity,BOOL>> filter =NULL, func<iqueryable<tentity>, iorderedqueryable<tentity>> =NULL, stringIncludeproperties ="") {IQueryable<TEntity> query =DbSet; if(filter!=NULL) {Query=query. Where (filter); } foreach(varIncludepropertyinchIncludeproperties.split (New Char[]{','},stringsplitoptions.removeemptyentries)) {query=query. Include (Includeproperty); } if(orderby!=NULL) { return(query). AsQueryable (). Asnotracking (). ToList ();//The asnotracking () method was not previously added here } Else { returnQuery. AsQueryable (). Asnotracking (). ToList ();//The asnotracking () method was not previously added here } }
Look at the comments, the change of the next method to get the data set, the same number of data, the execution took only 12 minutes to complete.
81 minutes of the picture is not posted out, run 81 minutes is too pit.
12 minutes or too slow, see everyone big blog, so ready to insert data that segment optimization.
Http://www.cnblogs.com/linfei721/archive/2013/06/07/3123579.html
Public voidAllocateallplans () {varEmployees = _efworker.employeerepository.findall (Filter:p = P.status.trim (). ToUpper () = ="ACTIVE"); varPlans = _efworker.benefitplanrepository.findall (includeproperties:"planeligibility", orderby:query = query. (p = p.priority), filter:p = P.status.trim (). ToUpper () = ="ACTIVE"); if(Employees = =NULL|| Plans = =NULL) return; _efworker.context.configuration.autodetectchangesenabled=false;//set no trace flagAllocateplanforemployee (employees, plans); _efworker.savechanges (); }
According to gourd painting scoop, first change to try.
!!! Sure enough, the tractor turned into a grey machine.
It took only 8 seconds and there was a picture of the truth. Wait, check the data right away first.
There seems to be no problem with data.
Summarize:
. Asnotracking () and context. Configuration.autodetectchangesenabled = False Both of these settings let EF do not track changes to the current data set, so if the data set needs to be updated or deleted, these two methods are not available, of course, depending on the business requirements, Use them flexibly.
EF Performance Tuning