I am using stored procedures to fetch information from the database. First I fetch all the parent elements and hold them in the array and then using the parent Id I fetch all the related children. Each parent can have 150 children. There are about 100 parent elements. What is the best way to increase the performance of the fetch operation. Currently it takes 13 seconds to retrieve.
Here is the basic algorithm:
while(reader.read())
{
Parent p = new Parent();
// assign properties to the parent
p.Children = GetChildrenByParentId(parent.Id);
}
You should get all that data in one SQL select / stored proc (do some sort of join on child data) and then populate parent and children objects. Now you have 100*150 = 15000 requests on DB and I if you can do this with one request I would expect dramatic performance effect.
As Brian mentioned it in comment, that is known as RBAR, RowByAgonizingRow 🙂
Like a achronime a lot, here is more :
https://www.simple-talk.com/sql/t-sql-programming/rbar–row-by-agonizing-row/