Performance issue when exporting to CSV from reports with custom fields
User Story
As an end user, I want to run a report of my organization's alumni. This is about 16,000 contacts, and my report contains 6 custom fields. However, this report takes about 8.5 minutes to generate, far longer than the 300 second timeout on processes.
Profiling
I stepped through the code until I saw that CRM_Core_BAO_CustomField::displayValue()
was the culprit, which is called ~96,000 times (# of rows * custom fields). I used a profiler and it reported that 97% of the time was spent in the CRM_Core_BAO_CustomField::getOptions()
method.
Improvements
-
CRM_Core_BAO_CustomField::getOptions()
is called on every custom field - even those that don't use it (e.g. text or money fields). By moving it so it's only called when needed, I immediately got a speedup. -
getOptions()
should only be called 6 times in my example, not 96,000 times. I'm going to put a cache intogetOptions()
.