2

We try to gather total number of users of same Relationship Type but different number of Relationships (mean, user can be a Member of no / 1 / 2 3 .. etc. Organisations).

Here's a code we use:

$start = microtime(true);
$total_count = civicrm_api3('Contact', 'getcount', array(
 'return' => "id",
  'contact_type' => "Individual",
  'is_deleted' => 0,
      ));
  $result = civicrm_api3('Contact', 'get', array(
    'sequential' => 1,
    'return' => "id",
    'contact_type' => "Individual",
    'is_deleted' => 0,
    'options' => array(
       'limit' => $total_count, 
       ),
      'api.Relationship.getcount' => array(
        'is_active' => 1,
        'relationship_type_id' => 4,
)));

foreach ($result['values'] as $r) {
  $relscount[] = $r['api.Relationship.getcount']; 
}

echo '<pre>';
print_r(array_count_values($relscount));
echo '</pre>';
 $time_elapsed_secs = microtime(true) - $start;
  echo 'Time spent: ' . $time_elapsed_secs . '<br>';

# Records: Time #
# 1000: 4.2179801464081
# 5000: 23.655788183212
# 15000: 74.082838058472 

There is one tiny issue though: we've around 100K Contacts in a DB and we haven't been able to find out how much time it takes to count 'em all..

The most obvious solution seems to be one of array traversing techniques involving for/foreach/while and "limit" + "offset". Just wondering, what is a better/smarter move, if exists?

UPD

Code which uses "getcount" is even slower:

$start = microtime(true);
 $result = civicrm_api3('Contact', 'get', array(
  'debug' => 1,
  'sequential' => 1,
  'return' => "id",
  'contact_type' => "Individual",
  'is_deleted' => 0,
  'api.Relationship.getcount' => array(
    'contact_id_a' => "\$value.id", 
    'relationship_type_id' => 4
  ),
  'options' => array('limit' => 15000),
 ));

 foreach ($result['values'] as $r) {
   $relscount[] = $r['api.Relationship.getcount']; 
 }

 echo '<pre>';
   print_r(array_count_values($relscount));
 echo '</pre>';


$time_elapsed_secs = microtime(true) - $start;
  echo 'Time spent: ' . $time_elapsed_secs . '<br>'; 

  # Records: Time #
  # 15000: 101.00403809547

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.