简体   繁体   English

加快linq /实体结果的返回

[英]Speed up return of linq/entity result

Unfortunaltly I am maintaining a system that didnt take consideration into what will happen once the system has millions of records. 不幸的是,我维护的系统没有考虑到一旦系统拥有数百万条记录就会发生什么。

The issue I am having is the way the code is currently written mainly the GetList method actually returns every record from the database and the filtering is done at the getfull method. 我遇到的问题是当前编写代码的方式主要是GetList方法实际上返回了数据库中的每条记录,并且过滤是在getfull方法中完成的。

I tryed changing this by passing the id of the adjustment to the getlist method however when i try to do .Where(a=>a.Id == journalid) i get this error 我试图通过将调整的id传递给getlist方法来更改此设置,但是当我尝试执行时。其中(a => a.Id == journalid)我收到此错误

Cannot implicitly convert type 'System.Linq.IQueryable to 'System.Data.Objects.ObjectQuery 无法将类型'System.Linq.IQueryable隐式转换为'System.Data.Objects.ObjectQuery

public Adjustment GetFull(int id) {
            var result = GetList(id).FirstOrDefault(ja => ja.Id == id);
            if(result == null) {
                throw new InvalidOperationException(
                    String.Format(" Adjustment with Id={0} not found.", id)
                );
            }
            return LoadFullActivities(result);
        }


        private IQueryable<Adjustment> GetList(int journalid) {
            return GetList(journalid,"Original", "Activities",
                "Original.Employee", "Original.Employee.EmployeeType", 
                "Original.Employee.WorkType", "Original.Employee.Department", 
                "Original.Activities", "Original.Status", 
                "Original.Approver", "Status");
        }

        private IQueryable<Adjustment> GetList(int journalid, params string[] includes) {
            var tempResult = dataContext.AdjustmentSet;
            foreach (var property in includes) {
                tempResult = tempResult.Include(property).Where(a=>a.Id == journalid) ;
            }
            return tempResult;
        }

This would fix the error 这将修复错误

private IQueryable<Adjustment> GetList(int journalid, params string[] includes) {
         var tempResult = dataContext.AdjustmentSet.AsQueryable();
         foreach (var property in includes) {
              tempResult = ((ObjectQuery<Adjustment>)tempResult).Include(property);
         }            
         return tempResult.Where(a=>a.Id == journalid);
      }

Those Includes slow down the query. 这些包括使查询变慢。 Since the query is going to get only one row (and do other joins because of the includes) I don't think the performance is so slow. 由于查询将仅获得一行(并且由于包含而进行其他联接),我认为性能不会这么慢。 To increase the performance add indexes for each foreign key. 为了提高性能,请为每个外键添加索引。 I guess a.Id has already a clustered index. 我猜a.Id已经有一个聚集索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM