Skip to content

Instantly share code, notes, and snippets.

@MegaphoneJon
Created September 3, 2019 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MegaphoneJon/59089c1e155645a438af41051bca7ce7 to your computer and use it in GitHub Desktop.
Save MegaphoneJon/59089c1e155645a438af41051bca7ce7 to your computer and use it in GitHub Desktop.
CiviCRM Smart group profiling
#!/usr/bin/php
<?php
// Use this code to see how long it takes to generate all of your smart groups to find slow ones.
eval(`cv php:boot`);
//Force a refresh of the cache
$sql = 'UPDATE civicrm_group SET cache_date = NULL, refresh_date = NULL';
CRM_Core_DAO::executeQuery($sql);
// Get a list of smart group IDs.
$smartGroups = civicrm_api3('Group', 'get', [
'return' => ["id", "title"],
'options' => ['limit' => 0],
'saved_search_id.id' => ['IS NOT NULL' => 1],
'is_active' => 1,
'is_hidden' => 0,
])['values'];
//place this before any script you want to calculate time
foreach ($smartGroups as $group) {
$time_start = microtime(true);
echo "{$group['title']} ({$group['id']}): ";
CRM_Contact_BAO_GroupContactCache::loadAll($group['id']);
$time_end = microtime(true);
$executionTime = ($time_end - $time_start);
echo "$executionTime seconds\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment