Entity Framework Queryable async

后端 未结 3 1807
悲&欢浪女
悲&欢浪女 2020-11-30 17:43

I\'m working on some some Web API stuff using Entity Framework 6 and one of my controller methods is a \"Get All\" that expects to receive the contents of a table from my da

3条回答
  •  忘掉有多难
    2020-11-30 18:19

    There is a massive difference in the example you have posted, the first version:

    var urls = await context.Urls.ToListAsync();
    

    This is bad, it basically does select * from table, returns all results into memory and then applies the where against that in memory collection rather than doing select * from table where... against the database.

    The second method will not actually hit the database until a query is applied to the IQueryable (probably via a linq .Where().Select() style operation which will only return the db values which match the query.

    If your examples were comparable, the async version will usually be slightly slower per request as there is more overhead in the state machine which the compiler generates to allow the async functionality.

    However the major difference (and benefit) is that the async version allows more concurrent requests as it doesn't block the processing thread whilst it is waiting for IO to complete (db query, file access, web request etc).

提交回复
热议问题