locked
I suppose that part of my program work to slow RRS feed

  • Question

  • User-865174439 posted

    Problem

    I suppose that my program work so slow. I wait about 2-5 seconds for the code to be executed. 

    PS. On the screen you can see GetRooms function, in problem description i use for simplicity GetAnimals

    Question

    It is posibble make this code more efficient/faster?

    Enviroment

    In my one from more view i want display data from dependent tables. To illustrate the problem more easily, let's say I have two tables: animals and animalsType

    animals Table

    Id name type number of paws
    1 dog mammal 4
    2 cat mammal 4
    3 ostrich bird 2

    animalsType Table

    Id Id_animals Name Population
    1 1 German Shepherd 12450
    2 1 Husky Dog 23910
    3 2 British Cat 94012
    4 2 Persian Cat 29381

    In my view i have two container (let's assume <div> element). In first container i have list of animal. When I click on the name of an animal in the first container (data from animals Table), all animals from the animalType table of this type are loaded into the second container (without page reload).

    My controller:

     public async Task<IActionResult> Index()
            {
                var animalsContext = _context.Animals.Include(p => p.AnimalsType);
                return View(await animalsContext .ToListAsync());
            }
     public IActionResult GetAnimalsType(int id)
       {
          var animalsTypeList = _context.AnimalList.Where(x => x.Id_animals== id).ToList();
          var jsonRoomList = JsonConvert.SerializeObject(animalsTypeList ,
             Formatting.None,
             new JsonSerializerSettings()
                {
                   ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                });
           return Content(jsonRoomList, "application/json");
    }

    My View:

      @foreach (var item in Model.Apartment)
     {
    <div onclick="loadData(item.Id)">item.AnimalName</div>
    
    }
    function loadData(id) { 
    var div = document.getElementById("placeToAnimalsType");
    div.innerHTML = "";
    $.ajax({ type: "POST",
    url: '../../Animals/GetAnimalsType',
    data: { id: id }, dataType: "json",
    success: function (data) {
    for (var i = 0; i < data.length; i++) {
    var html = `<div>data[i].Name</div>`;
    $("#placeToAnimalsType").append(html);
    }
    }
    });
    }
    Tuesday, November 10, 2020 8:50 PM

Answers

  • User-1330468790 posted

    Hi polunyt, 

     

    I cannot see any explicit codes that slows the performance of your project.

    There are many diagnostics tools that you could use to find out what decreases the performance of your application.

    Do you have chance to look into miniprofiler: ( https://miniprofiler.com/ ), which is recommended in Performance Diagnostic Tools.

      

    Debugging from the very beginning place, I would like to know the scale of the data which might be the main reason for the slow response. 

    If it is the case, you might need to improve the performance from database side, e.g. adding index or using store procedure.

    If you find that the process stops at another phase, then you could check if there is unexpected loop.

     

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2020 8:00 AM