Performance tuning of a visualforce page
It's my strong belief that a developer should always keep the performance in the top of the priority list. As I was working on a big Visualforce page with lot of aggregations, I wondered how to get best performance out of APEX and VF.
I went through Apex Code Best Practices and salesforce_visualforce_best_practices.pdf.
Both were really helpful, until I ran into following statements.
Apex Best Practice # 4 states "Use the power of the SOQL
where
clause to query all data needed in a single query."
Visualforce guide states
"When writing Apex or SOQL for use with a Visualforce page:
• Perform calculations in SOQL instead of in Apex, whenever possible.
• Never perform DML inside a loop.
• Filter in SOQL first, then in Apex, and finally in Visualforce"
Now, I am confused on writing a single SOQL and do the aggregations such as (max,min, sum, avg) in APEX or writing multiple aggregate SOQL's relay the data to VF through APEX.
I have done a few tests to understand the behaviour, using application performance profiling.
Note: I used this link to get more insight. But, it's always important to capture right amount of log to troubleshoot the issue. If the log size is bigger than 1MB, some parts may be truncated. This results in incorrect graphs in developer console. Adjust, log levels accordingly to get complete and appropriate log. Also, it's important to leave out first 2-3 logs before you analyze the log as caching plays an important role in reducing turnout times.
After thorough analysis, VF guide proved right. Ouf of writing multiple aggregate SOQL's and using Apex collections for aggregation, it's better to choose former approach. The first approach served the request within 50% time of latter. Once cached SOQL's are very quick and Apex processing lags behind (Also don't forget that we have CPU time governor limit to worry about) . Also, It's quick to get the aggregate information from DB to Apex than bring the whole record set for processing.
Below are few graphs.
Below are few graphs.
CASE I - Multiple Aggregate SOQL (7ms) |
CASE I - One SOQL - Aggregate using Apex Collections (15ms) |
CASE II - Multiple Aggregate SOQL(3ms) |
CASE II - One SOQL - Aggregate using Apex Collections (6+ms) |
Comments
Post a Comment
Feedback - positive or negative is welcome.