how to decrease the response time of a Web API?

Asked

Viewed 230 times

-1

I’m a beginner in C# and I’m developing a CRUD using Angular and Web Api (Entity Framework Core), but the response time to list my Serverscontroller data in my table is very high. What happens nowadays is that it takes a long time to list only 48 records (more than 5 minutes or sometimes not even loads), talking to a colleague I was proposed to do the consultation in the direct bank in my Get method and consume the query by right there, the big question is: how can I consume this query? because the connection/instance of the database I was able to do. Follow the current code below:

  [HttpGet]
  public async Task<ActionResult<IEnumerable<Server>>> GetServer()
  {
     using (var context = new ITControlContext())
     {
        using (var command = context.Database.GetDbConnection().CreateCommand())
        {
            command.CommandText = "SELECT e.Name [Environment], t.Name [ServerType], p.Name [Plant] FROM [ITControl].[dbo].[Server] s INNER JOIN [ITControl].[dbo].[Environment] e on s.IdEnvironment = e.IdEnvironment INNER JOIN [ITControl].[dbo].[ServerType] t on s.IdServerType = t.IdServerType INNER JOIN [ITControl].[dbo].[Plant] p on s.IdPlant = p.IdPlant";
            context.Database.OpenConnection();
            System.Data.DataTable dataTable = new System.Data.DataTable();
            dataTable.Load(command.ExecuteReader());
        }
     }

       return await _context.Server.Include(i => i.IdEnvironmentNavigation).Include(i => i.IdServerTypeNavigation).Include(i => i.IdPlantNavigation).ToListAsync();
  }

What I tried to do was create a "new instance" of my database and do the query there (I don’t know much what I did) and I would like to return the data of this query to be able to list them in my table without taking so long. Does anyone have any idea what I can do or give me any North of what to research/study?

Ps: I imagine I have to change something in mine return

  • 2

    pq has a select for a Datatable and then another in the Entity ? still you would have to see how is your bank, and exactly what point is there the delay

  • the Entity I left pq is the one I was using before the colleague told me to do the Get query even, follow link like this in my bank

  • 2

    comments block using any...leaves only Entity line... still there is delay ? there just seeing bank, indices, etc...

  • then, first with the Entity passing by .Include(i => i.IdEnvironmentNavigation).Include(i => i.IdServerTypeNavigation).Include(i => i.IdPlantNavigation).ToListAsync(); in the return He didn’t even present anything on the table and was that way,18 minutes charging and still nothing, now when I passed only 2 "Includes" EX: .Include(i => i.IdEnvironmentNavigation).Include(i => i.IdServerTypeNavigation).ToListAsync(); it took 0.58ms and presented only the 2 image here

  • I commented using all and left only Entity as recommended above photo but he continues to consult eternally print table

  • Rovann would have some idea of what I could do or study to do this?

  • 1

    tries to run a request manually with Postman... to see if it’s in the api too right... but would need to see what was done in the bank, how is your relationship etc...

  • I’ll do it now. I’ll have to talk to the team because I’m not the one who modeled the bank I’m an intern to suffering hahah

  • Rovann my noble, by Postman sent a request of type GET and he returned me: Status: 200 OK, Time: 506ms, Size: 847 B

  • 1

    500ms may even be time consuming depending on the scenario, but apparently the problem is not in the api...

  • Would it be on the front? also imagined q n would be on the pq API if n it would return me error on the browser console

  • I’m doing with the angle code here at my service in part prop I’m pulling the values that were in return await _context

  • Apparently I found the error, I’m making the query in the database by GET but I’m not calling anything from datatable in Return, but checking otherwise (which makes my api weigh), apparently this is the problem, now I need to look how to return this datatable

  • as I said above, comment the block using all... do not use one DataTable...

  • I figured it out, I’ll post the answer

Show 10 more comments

1 answer

0


Well, after a lot of research and tests (with a few hints of frustration) I managed to solve the problem :D I will post the solution below and explain how I got to this result.

Solution:

[HttpGet]
public async Task<ActionResult<IEnumerable<Server>>> GetServer()
{
    using (var context = new ITControlContext())
    {
        System.Data.DataTable dataTable = new System.Data.DataTable();

        using (var command = context.Database.GetDbConnection().CreateCommand())
        {
            command.CommandText = "SELECT s.IdServer, s.IP, s.Name, s.Description, e.Name [Environment], t.Name [ServerType], p.Name [Plant], s.CreatedAt, s.UserInserted, s.UpdatedAt, s.UserUpdated FROM [ITControl].[dbo].[Server] s INNER JOIN[ITControl].[dbo].[Environment] e on s.IdEnvironment = e.IdEnvironment INNER JOIN[ITControl].[dbo].[ServerType] t on s.IdServerType = t.IdServerType INNER JOIN[ITControl].[dbo].[Plant] p on s.IdPlant = p.IdPlant";
            context.Database.OpenConnection();
            dataTable.Load(command.ExecuteReader());
        }
        
        return Ok(dataTable);
    }          
}

Basically, what I did was recover my connection to the bank inside the [HttpGet] using the following block:

using (var command = context.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "SELECT s.IdServer, s.IP, s.Name, s.Description, e.Name [Environment], t.Name [ServerType], p.Name [Plant], s.CreatedAt, s.UserInserted, s.UpdatedAt, s.UserUpdated FROM [ITControl].[dbo].[Server] s INNER JOIN[ITControl].[dbo].[Environment] e on s.IdEnvironment = e.IdEnvironment INNER JOIN[ITControl].[dbo].[ServerType] t on s.IdServerType = t.IdServerType INNER JOIN[ITControl].[dbo].[Plant] p on s.IdPlant = p.IdPlant";
    context.Database.OpenConnection();
    dataTable.Load(command.ExecuteReader());
}

but instead of returning the datatable that was loading this query, I was still returning the old command (through Includes, thus making the implementation of this connection with the datatable "useless" since I was not consuming it). Another mistake is also that I was calling the System.Data.DataTable dataTable = new System.Data.DataTable(); inside the connection block itself, not being able to return it. So I needed to first leave this instance of datatable outside the connection block and then return inside mine return passing him on to the method Ok(), getting Ok(dataTable) and voila, it worked.

I even separated a comparative image with the response time with the before and after, see below:

inserir a descrição da imagem aqui

It was possible to get out of 34.4 seconds (that didn’t even list everything I needed, because if I put it all in it would not even carry see) and I was able to go to 583ms listing all the necessary data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.