locked
Improving Performance of Calculated Properties RRS feed

  • Question

  • I'm using calculated properties to roll up information about related entities.  Things were working great with only a few items, but after loading a large amount of data my performance is terrible.

    I have a Group, which has many GroupAssignments (each containing a Volunteer), and each Volunteer has many Assignments. 

    On the Volunteer Entity I have a calculated property for "AttendanceRate" which is:

    this.Assignments.Where(x => x.Show.ShowCallTime < DateTime.Now && x.IsPresent == false).Count() /
    this.Assignments.Where(x => x.Show.ShowCallTime < DateTime.Now).Count()
    

    On the Group Entity I have a calculated property for "GroupAttendanceRate" which is:

    this.GroupAssignments.Select(x => x.Volunteer).Where(x => x.IsActive == true).Average(x => x.AttendanceRate);

    What I would like to do is display a screen that that shows the a list of groups and their GroupAttendanceRate.  I'm watching the activity through fiddler, and there is one request for Groups, and then a seperate request to get the GroupAssignments for each item in Groups.  Then there is a seperate request for each Volunteer related to the Group.

    So basically, in order to calculate the GroupAttendanceRate, it requests EVERY volunteer in that group.  Loading a list with 10 groups each with 100 Volunteers generates 1000 seperate http requests.

    What are my options for optomizing this? 

    I have considered changing them from calculated properties to properties that are actually calculated and saved in the database in the _Updating and _Inserting functions, but that seems counter-intuitive.  Also, because they involve comparisons to DateTime.Now they do need to be recalculated when they are requested. 

    Is there a way to make the calculated properties get calculated on the server side on the intial request to Groups()? 

    Wednesday, January 9, 2013 6:01 PM

Answers

  • There are 4 ways to display records with aggregated information (without computed properties):

    1. Use SQL calculated columns (only useful if your data is an attached database)
    2. Use a SQL View (again for an attached database)
    3. Use "real" properties (using the Inserting/Updating methods that you described above)
    4. Use a custom RIA service

    The best way, in my opinion, is to create a custom RIA service.

    This article link below will show you all the techniques that you'll need. It's VERY easy. And the performance is greatly improved over computed properties.

    How Do I: Display a Chart Built On Aggregated Data


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 10, 2013 4:43 AM
    Moderator

All replies

  • There are 4 ways to display records with aggregated information (without computed properties):

    1. Use SQL calculated columns (only useful if your data is an attached database)
    2. Use a SQL View (again for an attached database)
    3. Use "real" properties (using the Inserting/Updating methods that you described above)
    4. Use a custom RIA service

    The best way, in my opinion, is to create a custom RIA service.

    This article link below will show you all the techniques that you'll need. It's VERY easy. And the performance is greatly improved over computed properties.

    How Do I: Display a Chart Built On Aggregated Data


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 10, 2013 4:43 AM
    Moderator
  • @Angie - please propose an answer before marking it, then give the OP a reasonable amount of time to confirm it.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Friday, January 25, 2013 5:27 AM
    Moderator
  • For the "real" properties solution you can also use the _Changed method to calculate the property.  This will give you the client tier response similar to a computed property and remove some of the processing involved with the inserting/updating methods.
    Saturday, January 26, 2013 3:09 AM
  • Quite true! Thanks for pointing that out. :-)

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Saturday, January 26, 2013 12:55 PM
    Moderator
  • Just a followup, I wrote a WCF RIA service, copied my formulas for the computed methods into the WCF RIA service, and it is working well.

    I only put the computed fields in the new class, as I don't want to duplicate defining all of the other fields.

    However, in my screen, I want to use a grid view to display the all of the fields from the original entity as well as all of the computed fields on one row.

    Is there a good way to do this?  Or do I need to "copy over" the values from the original data source to my WCF RIA service?

    Wednesday, January 30, 2013 8:04 PM