0

I'm testing database calls with Include and noticed that my async code was running really slow compared to the synchronous code. Given that the synchronous code is almost five times faster I'm assuming I have done something terribly wrong but I can't spot it. When looking at the code it seems I'm awaiting the ToListAsync() which should be correct. What am I doing wrong or is this normal behavior?

Note: Scaled down examples to only focus on one database call, async is unnecessary in the example but the time difference is present anyway.

Async:

[HttpGet]
[Route("{bsid}/{caseId}/async")]
public async Task<IHttpActionResult> GetAsync(string bsid, int caseId)
{
    var db = new DbContext();

    var deviations = await db.RenewalCycles.Where(x => x.Deviations.Any())
        .Include(cycle => cycle.TPCase.CaseNames.Select(caseName => caseName.TPName))
        .Include(cycle => cycle.TPCase.CaseNames.Select(caseName => caseName.TPNameType))
        .Include(cycle => cycle.TPCase.GoodsAndServicesDescriptions)
        .Include(cycle => cycle.TPCase.RelatedCases.Select(relatedCase => relatedCase.TPCaseRelation))
        .Include(cycle => cycle.TPCase.RelatedCases.Select(relatedCase => relatedCase.TPCountry))
        .ToListAsync();

    return Ok();
}

enter image description here

Synchronous:

[HttpGet]
[Route("{bsid}/{caseId}")]
public IHttpActionResult Get(string bsid, int caseId)
{
    var db = new DbContext();

   var deviations = db.RenewalCycles.Where(x => x.Deviations.Any())
        .Include(cycle => cycle.TPCase.CaseNames.Select(caseName => caseName.TPName))
        .Include(cycle => cycle.TPCase.CaseNames.Select(caseName => caseName.TPNameType))
        .Include(cycle => cycle.TPCase.GoodsAndServicesDescriptions)
        .Include(cycle => cycle.TPCase.RelatedCases.Select(relatedCase => relatedCase.TPCaseRelation))
        .Include(cycle => cycle.TPCase.RelatedCases.Select(relatedCase => relatedCase.TPCountry))
        .ToList();

    return Ok();
}

enter image description here

Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • Possible duplicate of [Entity Framework async operation takes ten times as long to complete](https://stackoverflow.com/questions/28543293/entity-framework-async-operation-takes-ten-times-as-long-to-complete) – Peter B Aug 28 '18 at 14:29
  • One question - what's the point of using async if you're just going to await it anyway? You could just use the synchronous method and the code flow will be the same. – ADyson Aug 28 '18 at 14:41
  • @PeterB Thank you, I did not see that question. – Ogglas Aug 28 '18 at 14:42
  • @ADyson The code is just for demonstration. Scaled it down to be able to test the methods isolated and thought the time difference was huge. – Ogglas Aug 28 '18 at 14:44
  • you're still awaiting a single asynchronous method in an otherwise synchronous operation. Are you saying in reality you're planning to run multiple queries asynchronously without awaiting each one individually? It might make more sense then. – ADyson Aug 28 '18 at 14:45
  • @ADyson That is true but it is not the point of the question. My point is that the method `.ToListAsync()` takes five times longer than `.ToList`. – Ogglas Aug 28 '18 at 14:50
  • I know it's not precisely the point of the question. But the reason I mentioned it was that if it turned out to be meaningless to use async (which, per the example given - which is all I've got to go on - it is) then you could simply avoid the issue by using the synchronous method. – ADyson Aug 28 '18 at 14:52
  • @ADyson Yes, however I deemed it unnecessary to write a correct `async` example given that the code presented is enough to replicate the result. Running two or more queries would be a worse example in my meaning since the question is about speed and therefore you would like to know how long every bit of the method took. – Ogglas Aug 28 '18 at 14:55
  • Ok well I appreciate that you simplified things for the purpose of the question, clearly that's a sensible idea, but then you have to anticipate that people might misunderstand your overall intentions. We can only deal with what we can see in the question. Now you've explained it in more detail, I'm happy to put my comment to one side. – ADyson Aug 28 '18 at 14:58
  • @ADyson Thanks and I understand your point of view as well. I have now updated the question and will think about it in the future. :) – Ogglas Aug 28 '18 at 16:56
  • What makes you think one individual request should be faster just by using `async-await`? – Paulo Morgado Aug 28 '18 at 23:53

0 Answers0