Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-10598

improve query performance for recent case activities

    Details

    • Type: Improvement
    • Status: Done/Fixed
    • Priority: Major
    • Resolution: Fixed/Completed
    • Affects Version/s: 4.1.2
    • Fix Version/s: 4.2.0
    • Component/s: CiviCase
    • Labels:
      None

      Description

      the current recent activity query does the following:

      select from civicrm_view_case_activity_recent
      [ view retrieves all case activities within the last few weeks
      where not status scheduled, not deleted, and current revision ]

      join with subquery:
      select MIN(id) from _view_case_activity_recent group by case_id
      join with subquery:
      select MAX(date) from _view_case_activity_recent group by case_id

      join with _option_group (get activity_type option group)
      join with _option_value (get activity type name/label)


      the performance hit appears to mostly be on the subqueries. the entire query takes 3.5 min with 12k recent activities and 70k cases. the subquery (with it's internal subquery) takes pretty much that whole time.

      obeservations:

      • we shouldn't need the initial FROM with the view. all we're doing is selecting it and then limiting it by it's own data.
      • it seems unnecessary to find the lowest id/max date. that would only actually happen if there are two activities with the exact same timestamp. is the concern to deal with activities constructed during initial case creation?

      it seems our goal is to sort through the most recent activities and for each case, find the one with the greatest timestamp. if 2+ have the same timestamp for the same case, then select the one with the lowest id (i think that makes some sense, but should not be a strict requirement).

      we can do this with a single subquery where we order by date and id, then in the outer query we group by case_id. in my testing, the query drops from 3.5min to .3sec.

      i'm attaching a patch, but folks with more knowledge of the expected behavior should review and test.

        Attachments

          Activity

            People

            • Assignee:
              dgg David Greenberg
              Reporter:
              lcdweb Brian Shaughnessy
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: