Skip to content

Commit

Permalink
PDO: Add support for index hints with MySQL/MariaDB to improve perfor…
Browse files Browse the repository at this point in the history
…mance.
  • Loading branch information
EreMaijala committed Jan 9, 2024
1 parent 407969b commit 4732023
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions conf/recordmanager.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ counts = false
;username = "recman"
; Password
;password = "topsecret"
; Whether to use index hints (MySQL/MariaDB). Recommended to keep enabled (default)
; unless trying to solve performance issues.
use_index_hints = true

[Solr]
; Update URL. Note that RecordManager requires that the json update method be available.
Expand Down
42 changes: 42 additions & 0 deletions src/RecordManager/Base/Database/PDODatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ class PDODatabase extends AbstractDatabase
*/
protected $lastRecordAttrsId = [];

/**
* Whether to use index hints (MySQL/MariaDB)
*
* @var bool
*/
protected $useIndexHints = false;

/**
* Constructor.
*
Expand All @@ -119,6 +126,7 @@ public function __construct(array $config)
$this->dsn = $config['connection'] ?? '';
$this->username = $config['username'] ?? '';
$this->password = $config['password'] ?? '';
$this->useIndexHints = (bool)($config['use_index_hints'] ?? true);
}

/**
Expand Down Expand Up @@ -712,6 +720,9 @@ protected function findPDORecords(
[$where, $params] = $this->filterToSQL($collection, $filter);
[$fields, $sqlOptions] = $this->optionsToSQL($options);
$sql = "select $fields from $collection";
if ($hints = $this->getIndexHints($collection, $filter, $options)) {
$sql .= " $hints";
}
if ($where) {
$sql .= " where $where";
}
Expand Down Expand Up @@ -1224,4 +1235,35 @@ protected function getMainFields(string $collection): array
}
return $this->mainFields[$collection];
}

/**
* Check if the database is MySQL or MariaDB
*
* @return bool
*/
protected function isMySQLCompatible(): bool
{
return str_starts_with($this->dsn, 'mysql')
|| str_starts_with($this->dsn, 'mariadb');
}

/**
* Get index hints
*
* @param string $collection Collection
* @param array $filter Search filter
* @param array $options Options such as sorting
*
* @return string
*/
protected function getIndexHints(string $collection, array $filter, array $options): string
{
if (!$this->useIndexHints || !$this->isMySQLCompatible()) {
return '';
}
if ('record' === $collection && isset($options['sort']['_id'])) {
return 'USE INDEX (source_update_needed)';
}
return '';
}
}

0 comments on commit 4732023

Please sign in to comment.