Skip to content

Commit

Permalink
Remove use of LOWER from city & street searches
Browse files Browse the repository at this point in the history
Adding LOWER to mysql queries makes them slower. lowercasing php strings
breaks under some character sets with some server configs.
  • Loading branch information
eileenmcnaughton committed Oct 23, 2018
1 parent 63ca24f commit 54e02ce
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
7 changes: 2 additions & 5 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2294,8 +2294,7 @@ public function restWhere(&$values) {

//get the location name
list($tName, $fldName) = self::getLocationTableName($field['where'], $locType);
// LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
$fieldName = "LOWER(`$tName`.$fldName)";
$fieldName = "`$tName`.$fldName";

// we set both _tables & whereTables because whereTables doesn't seem to do what the name implies it should
$this->_tables[$tName] = $this->_whereTables[$tName] = 1;
Expand All @@ -2307,8 +2306,7 @@ public function restWhere(&$values) {
}
else {
if ($op != 'IN' && !is_numeric($value) && !is_array($value)) {
// LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
$fieldName = "LOWER({$field['where']})";
$fieldName = "{$field['where']}";

This comment has been minimized.

Copy link
@mfb

mfb Nov 1, 2018

Contributor

Yay! This offers a significant speed boost for API calls. although now the surrounding logic could be removed altogether.

}
else {
$fieldName = "{$field['where']}";
Expand Down Expand Up @@ -3566,7 +3564,6 @@ public function street_address(&$values) {
$value = "%{$value}%";
}
$op = 'LIKE';
// LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
$this->_where[$grouping][] = self::buildClause('civicrm_address.street_address', $op, $value, 'String');
$this->_qill[$grouping][] = ts('Street') . " $op '$n'";
}
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ public function testSearchProfilePrimaryCityCRM14263($params, $selectClause, $wh
CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
$this->callAPISuccess('address', 'create', array(
'contact_id' => $contactID,
'city' => 'Cool City',
'street_address' => 'Long Street',
'city' => 'Cool CITY',
'street_address' => 'Long STREET',
'location_type_id' => 1,
));
$returnProperties = array(
Expand Down Expand Up @@ -257,7 +257,7 @@ public function testSearchProfilePrimaryCityCRM14263($params, $selectClause, $wh
public function getSearchProfileData() {
return [
[
[['city', '=', 'Cool City', 1, 0]], "civicrm_address.city as `city`", "LOWER(civicrm_address.city) = 'cool city'",
[['city', '=', 'Cool City', 1, 0]], "civicrm_address.city as `city`", "civicrm_address.city = 'cool city'",
],
[
// Note that in the query 'long street' is lower cased. We eventually want to change that & not mess with the vars - it turns out
Expand Down

0 comments on commit 54e02ce

Please sign in to comment.