title: ASP.Net Core API response takes too much time
body: <p>I have a SQL database table with 9000 rows and 97 columns. Its primary key has 2 columns: <code>Color</code> and <code>Name</code>. You can see the simplified table to image better: </p>
  
  <p><img src="https://i.stack.imgur.com/oVf0a.png" alt="enter image description here"></p>
  
  <p>I have an ASP.NET Core API listening at URL <code>api/color/{colorName}</code>, it reads the table to get color information. Currently I have 3 colors and about 3000 rows each.</p>
  
  <p>It takes too much time. It reads table in 2383ms and maps to DTO in 14ms. And after that I immediately return the DTO to consumer but somehow the API takes 4135.422ms. I don't understand why. I guess I should take 2407.863ms but it not. It takes almost 2 times more.</p>
  
  <p>You can see my code and logs below. Do you have an idea how can I improve the response time?</p>
  
  <p>I use Entity Framework Core 3.1, AutoMapper and ASP.NET Core 3.1.</p>
  
  <p>Service:</p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;ColorDTO&gt;&gt; GetColors(string requestedColor)
  {
      var watch = System.Diagnostics.Stopwatch.StartNew();
      var colors = await _dbContext.Colors.Where(color =&gt; color.color == requestedColor).ToListAsync();
      watch.Stop();
      _logger.LogError("Color of:{requestedColor} Reading takes:{elapsedMs}", requestedColor, watch.ElapsedMilliseconds);
  
  
      var watch2 = System.Diagnostics.Stopwatch.StartNew();
      var colorDtos = _mapper.Map&lt;IEnumerable&lt;ColorDTO&gt;&gt;(colors);
      watch2.Stop();
      _logger.LogError("Color of:{requestedColor} Mapping takes:{elapsedMs}", requestedColor, watch2.ElapsedMilliseconds);
  
      return colorDtos;
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public async Task&lt;ActionResult&lt;IEnumerable&lt;ColorDTO&gt;&gt;&gt; GetBlocksOfPanel(string requestedColor)
  {
      return Ok(await _colorService.GetColors(requestedColor));
  }
  </code></pre>
  
  <p>And the logs:</p>
  
  <pre><code>2020-04-27 15:21:54.8793||0HLVAKLTJO59T:00000003|MyProject.Api.Services.IColorService|INF|Color of Purple Reading takes:2383ms
  2020-04-27 15:21:54.8994||0HLVAKLTJO59T:00000003|MyProject.Api.Services.IColorService|INF|Color of Purple Mapping takes:14ms
  2020-04-27 15:21:54.9032||0HLVAKLTJO59T:00000003|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|INF|Executed action method MyProject.Api.Web.Controllers.ColorsController.GetColors (MyProject.Api.Web), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 2407.863ms.
  2020-04-27 15:21:54.9081||0HLVAKLTJO59T:00000003|Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor|INF|Executing ObjectResult, writing value of type 'System.Collections.Generic.List`1[[MyProject.Api.Contracts.Dtos.ColorDTO, MyProject.Api.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
  2020-04-27 15:21:56.4895||0HLVAKLTJO59T:00000003|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|INF|Executed action MyProject.Api.Web.Controllers.ColorsController.GetColors (MyProject.Api.Web) in 4003.8022ms
  2020-04-27 15:21:56.4927||0HLVAKLTJO59T:00000003|Microsoft.AspNetCore.Routing.EndpointMiddleware|INF|Executed endpoint 'MyProject.Api.Web.Controllers.ColorsController.GetColors (MyProject.Api.Web)'
  2020-04-27 15:21:56.4972||0HLVAKLTJO59T:00000003|Microsoft.AspNetCore.Hosting.Diagnostics|INF|Request finished in 4135.422ms 200 application/json; charset=utf-8
  </code></pre>