Fix Export when full group by mode is used
authoreileen <emcnaughton@wikimedia.org>
Mon, 19 Nov 2018 00:45:54 +0000 (13:45 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 19 Nov 2018 00:50:29 +0000 (13:50 +1300)
After upgrading locally to mysql 5.7 I found that the exportIM test
was taking so long locally that I was killing it rather than finding
out how long it would take.

Digging into it I found that we were changing the query so that
attempts to group by contact ID were being nullified when full group by
is enabled. This meant that we were winding up with
7776 rows being retrieved to reflect a grid of permutations, which was
being iterated down to 1 in php.

In general the practice of altering the groupby to meet this new
standard is one that we determined to be
causing problems and we discontinued / rolled back

CRM/Export/BAO/Export.php

index c530add01eb89b3410ad4324bfa3e43701b4f03f..c56ca09031a3e90cd3b6ac6af59c833085a04e2b 100644 (file)
@@ -166,14 +166,7 @@ class CRM_Export_BAO_Export {
       $groupBy = "civicrm_activity.id ";
     }
 
-    if (!empty($groupBy)) {
-      if (!Civi::settings()->get('searchPrimaryDetailsOnly')) {
-        CRM_Core_DAO::disableFullGroupByMode();
-      }
-      $groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($query->_select, $groupBy);
-    }
-
-    return $groupBy;
+    return $groupBy ? ' GROUP BY ' . $groupBy : '';
   }
 
   /**
@@ -463,7 +456,9 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
 
     while (!$limitReached) {
       $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}";
+      CRM_Core_DAO::disableFullGroupByMode();
       $iterationDAO = CRM_Core_DAO::executeQuery($limitQuery);
+      CRM_Core_DAO::reenableFullGroupByMode();
       // If this is less than our limit by the end of the iteration we do not need to run the query again to
       // check if some remain.
       $rowsThisIteration = 0;
@@ -1585,11 +1580,13 @@ WHERE  {$whereClause}";
       $today = date('Ymd');
       $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
       $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
-      $relationGroupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($relationQuery->_select, "crel.{$contactA}");
+      CRM_Core_DAO::disableFullGroupByMode();
       $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
-      $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving $relationGroupBy";
+      $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationHaving GROUP BY crel.{$contactA}";
 
       $allRelContactDAO = CRM_Core_DAO::executeQuery($relationQueryString);
+      CRM_Core_DAO::reenableFullGroupByMode();
+
       while ($allRelContactDAO->fetch()) {
         $relationQuery->convertToPseudoNames($allRelContactDAO);
         $row = [];