1
0

PathPrefixOptimizer.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Files\Search\QueryOptimizer;
  8. use OC\Files\Search\SearchComparison;
  9. use OCP\Files\Search\ISearchBinaryOperator;
  10. use OCP\Files\Search\ISearchComparison;
  11. use OCP\Files\Search\ISearchOperator;
  12. class PathPrefixOptimizer extends QueryOptimizerStep {
  13. private bool $useHashEq = true;
  14. public function inspectOperator(ISearchOperator $operator): void {
  15. // normally any `path = "$path"` search filter would be generated as an `path_hash = md5($path)` sql query
  16. // since the `path_hash` sql column usually provides much faster querying that selecting on the `path` sql column
  17. //
  18. // however, if we're already doing a filter on the `path` column in the form of `path LIKE "$prefix/%"`
  19. // generating a `path = "$prefix"` sql query lets the database handle use the same column for both expressions and potentially use the same index
  20. //
  21. // If there is any operator in the query that matches this pattern, we change all `path = "$path"` instances to not the `path_hash` equality,
  22. // otherwise mariadb has a tendency of ignoring the path_prefix index
  23. if ($this->useHashEq && $this->isPathPrefixOperator($operator)) {
  24. $this->useHashEq = false;
  25. }
  26. parent::inspectOperator($operator);
  27. }
  28. public function processOperator(ISearchOperator &$operator) {
  29. if (!$this->useHashEq && $operator instanceof ISearchComparison && !$operator->getExtra() && $operator->getField() === 'path' && $operator->getType() === ISearchComparison::COMPARE_EQUAL) {
  30. $operator->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false);
  31. }
  32. parent::processOperator($operator);
  33. }
  34. private function isPathPrefixOperator(ISearchOperator $operator): bool {
  35. if ($operator instanceof ISearchBinaryOperator && $operator->getType() === ISearchBinaryOperator::OPERATOR_OR && count($operator->getArguments()) == 2) {
  36. $a = $operator->getArguments()[0];
  37. $b = $operator->getArguments()[1];
  38. if ($this->operatorPairIsPathPrefix($a, $b) || $this->operatorPairIsPathPrefix($b, $a)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. private function operatorPairIsPathPrefix(ISearchOperator $like, ISearchOperator $equal): bool {
  45. return (
  46. $like instanceof ISearchComparison && $equal instanceof ISearchComparison &&
  47. !$like->getExtra() && !$equal->getExtra() && $like->getField() === 'path' && $equal->getField() === 'path' &&
  48. $like->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $equal->getType() === ISearchComparison::COMPARE_EQUAL
  49. && $like->getValue() === SearchComparison::escapeLikeParameter($equal->getValue()) . '/%'
  50. );
  51. }
  52. }