Skip to content

Commit

Permalink
avoid using group by and select min for tracker actions query to avoi…
Browse files Browse the repository at this point in the history
…d tmp tables refs #14535 (#14584)
  • Loading branch information
tsteur authored Jul 5, 2019
1 parent 7147fc3 commit aed8c10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
34 changes: 28 additions & 6 deletions core/Tracker/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public function getIdActionMatchingNameAndType($name, $type)
*/
public function getIdsAction($actionsNameAndType)
{
$sql = "SELECT MIN(idaction) as idaction, type, name FROM " . Common::prefixTable('log_action')
. " WHERE";
$sql = "SELECT `idaction`, `type`, `name` FROM " . Common::prefixTable('log_action') . " WHERE";
$bind = array();

$i = 0;
Expand All @@ -239,16 +238,39 @@ public function getIdsAction($actionsNameAndType)
$i++;
}

$sql .= " GROUP BY type, hash, name";

// Case URL & Title are empty
if (empty($bind)) {
return false;
}

$actionIds = $this->getDb()->fetchAll($sql, $bind);
$rows = $this->getDb()->fetchAll($sql, $bind);

$actionsPerType = array();

foreach ($rows as $row) {
$name = $row['name'];
$type = $row['type'];

if (!isset($actionsPerType[$type])) {
$actionsPerType[$type] = array();
}

if (!isset($actionsPerType[$type][$name])) {
$actionsPerType[$type][$name] = $row;
} elseif ($row['idaction'] < $actionsPerType[$type][$name]['idaction']) {
// keep the lowest idaction for this type, name
$actionsPerType[$type][$name] = $row;
}
}

$actionsToReturn = array();
foreach ($actionsPerType as $type => $actionsPerName) {
foreach ($actionsPerName as $actionPerName) {
$actionsToReturn[] = $actionPerName;
}
}

return $actionIds;
return $actionsToReturn;
}

public function updateEcommerceItem($originalIdOrder, $newItem)
Expand Down
14 changes: 7 additions & 7 deletions tests/PHPUnit/Integration/Tracker/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,26 @@ public function test_getIdsAction_CorrectlyReturnsRequestedActionIds()
));

$expectedResult = array(
array(
'idaction' => '2',
'type' => '1',
'name' => 'action1'
),
array(
'idaction' => '3',
'type' => '1',
'name' => 'ACTION1'
),
array(
'idaction' => '2',
'type' => '1',
'idaction' => '4',
'type' => '2',
'name' => 'action1'
),
array(
'idaction' => '5',
'type' => '2',
'name' => 'action2'
),
array(
'idaction' => '4',
'type' => '2',
'name' => 'action1'
)
);
$this->assertEquals($expectedResult, $result);
}
Expand Down

0 comments on commit aed8c10

Please sign in to comment.