none
Linq to SQL usage - is it bad for performance to keep a link on beginning of a List? RRS feed

  • Question

  • Hi. Consider such situation. I have a set of classes autmatically generated by Visual studio to support Linq to Sql. I have a table caled PricesSource. Using dbContext I iterate through its rows:

    FactEventAnalysisDBDataContext dbContext = new FactEventAnalysisDBDataContext();
    
    var startPrice = new FactEventAnalyzer.PricesSource();
    
    foreach (FactEventAnalyzer.PricesSource curPrice in PriceQuery)
    {
    //do smth
    }

    I want to keep a reference to one of 'interesting' records in database to do this I add a reference to interesting record:

    startPrice = curPrice;

    While iterating I check if curPrice meets some conditions and once I find a record which meets this conditions I want to do some work against startPrice and curPrice:

    curChange = determineChangeType(prevPrice: startPrice.Price, curPrice: curPrice.Price, changeThreshold: changeThreshold);

    The question is that, is it bad to keep a reference to startPrice directly? How it actually works behind the scene? What the system really does when I tell it to retreive startPrice.Price value? AFAR when you iterate through result set it doesn't try to actually store whole resultset in RAM, so I am afraid that if I will keep a reference to startPrice it will somehow try to store the piece of resultset between startPrice record and curPrice record. Currently I am testing my code against limited amount of data, so resultset is small and code is completed within a second, but what I am a little bit nervous about the moment when I will need to run it against billion of records - it is possible that I will have millions of rows between startPrice and curPrice.

    Tuesday, April 29, 2014 6:23 PM

Answers

  • I suggest you investigating the PricesSource class, generated by the system. (Right-click and select Go To Definition). If it is a class that just contains a copy of row’s data, then I do not think it will affect the performance. Check the Price getter too. If you need just the Price value, then keep it in a simpler variable, instead of keeping the whole row.

    Wednesday, April 30, 2014 7:48 AM

All replies

  • Hello,

    >>AFAR when you iterate through result set it doesn't try to actually store whole resultset in RAM, so I am afraid that if I will keep a reference to startPrice it will somehow try to store the piece of resultset between startPrice record and curPrice record

    From my opinion, I do not think it will try to store the piece of resultset between startPrice record and curPrice record. As far as I know, the froeach syntax as in your case it will allocate memory for the PriceQuery to store the data, the curPrice is just a pointer to reference the current Price object in the list. If we set startPrice = curPrice, we just only give the pointer which references one object of the list.

    If I misunderstand, please correct me and let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 30, 2014 7:24 AM
    Moderator
  • I suggest you investigating the PricesSource class, generated by the system. (Right-click and select Go To Definition). If it is a class that just contains a copy of row’s data, then I do not think it will affect the performance. Check the Price getter too. If you need just the Price value, then keep it in a simpler variable, instead of keeping the whole row.

    Wednesday, April 30, 2014 7:48 AM