Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Case.php #31499

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Update Case.php #31499

wants to merge 3 commits into from

Conversation

hoegrammer
Copy link
Contributor

Overview

Query running very slow, doing unnecessary work - see https://civicrm.stackexchange.com/questions/48892/is-my-reasoning-correct-in-regard-to-speeding-up-this-core-query-and-if-so-how

Before

Line 625 calls getCaseActivityCountQuery which in turn calls getCaseActivityQuery

After

Line 625 runs much simpler but equivalent SQL query directly (Coleman suggested using API4, but when I try to do a count in the API4 explorer it comes back as 0, so I was not sure of the right syntax)

Technical Details

I believe the function getCaseActivityCountQuery is not called anywhere else so it seems appropriate to inline it.

See https://civicrm.stackexchange.com/questions/48892/is-my-reasoning-correct-in-regard-to-speeding-up-this-core-query-and-if-so-how

Coleman suggested using API4, but when I try to do a count in the API4 explorer it comes back as 0, so I was not sure of the right syntax
Copy link

civibot bot commented Nov 19, 2024

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the master label Nov 19, 2024
@demeritcowboy
Copy link
Contributor

I like your reasoning but there are some test fails because it's not taking the parameters into account for different situations. See also #30178 which is related but also needs a little work because it's not taking all the parameters into account.

CRM_Case_BAO_CaseTest.testActiveCaseRole

CRM_Case_BAO_CaseTest.testInactiveCaseRole_

CRM_Case_BAO_CaseTest.testOnlyRecent

So as not to affect situations where a more fine-grained query is needed
NB. I renamed the function from getCaseActivityCountQuery to getCaseCountQuery because even when it take activities into account, it is still returning a count of cases, not a count of activities.
@hoegrammer
Copy link
Contributor Author

Now only using the simpler query when the request is for all and any cases (as opposed to cases related to a specific user or cases with recent or upcoming activities)

@demeritcowboy
Copy link
Contributor

demeritcowboy commented Nov 19, 2024

Hi,
While it looks like we don't have tests for it, it's not taking $condition into account - one place you can see this is on the all-cases dashlet if you add it to the civi home dashboard. The pager at the bottom has the wrong count if you filter the casetype or status. Or just running e.g. cv --user=admin ev 'echo CRM_Case_BAO_Case::getCases(true, ["type" => "any", "status_id" => 3], "dashboard", true);', where "admin" is an admin user. It will give you a count of all cases not just ones with status = 3.

There's a secondary bug that's pre-existing and I think has come up before but doesn't seem to happen anywhere in core. The caching of the count is only on $type, and so if you make two consecutive calls to CRM_Case_BAO_Case::getCases with allCases = true and allCases = false, you get an incorrect answer on the second one. That doesn't need to be fixed here I'm just mentioning if it comes up in testing.

@eileenmcnaughton
Copy link
Contributor

Note that the reason the underlying query is slow is that these joins are nasty - joining a varchar on an INT - in other places we have fixed such that we remove these joins & just lookup the labels using cached values on the php layer (e.g CRM_Core_Pseudoconstant::getLabel('CRM_Case_BAO_Case', 'case_type_id', $typeID)

    LEFT JOIN civicrm_option_group option_group_case_status ON ( option_group_case_status.name = 'case_status' )
    LEFT JOIN civicrm_option_value case_status ON ( civicrm_case.status_id = case_status.value
      AND option_group_case_status.id = case_status.option_group_id )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants