|
29 | 29 | use OCP\Files\Search\ISearchOperator; |
30 | 30 |
|
31 | 31 | class PathPrefixOptimizer extends QueryOptimizerStep { |
32 | | - public function processOperator(ISearchOperator &$operator) { |
33 | | - // normally the `path = "$prefix"` search query part of the prefix filter would be generated as an `path_hash = md5($prefix)` sql query |
| 32 | + private bool $useHashEq = true; |
| 33 | + |
| 34 | + public function inspectOperator(ISearchOperator $operator): void { |
| 35 | + // normally any `path = "$path"` search filter would be generated as an `path_hash = md5($path)` sql query |
34 | 36 | // since the `path_hash` sql column usually provides much faster querying that selecting on the `path` sql column |
35 | 37 | // |
36 | | - // however, since we're already doing a filter on the `path` column in the form of `path LIKE "$prefix/%"` |
| 38 | + // however, if we're already doing a filter on the `path` column in the form of `path LIKE "$prefix/%"` |
37 | 39 | // generating a `path = "$prefix"` sql query lets the database handle use the same column for both expressions and potentially use the same index |
| 40 | + // |
| 41 | + // If there is any operator in the query that matches this pattern, we change all `path = "$path"` instances to not the `path_hash` equality, |
| 42 | + // otherwise mariadb has a tendency of ignoring the path_prefix index |
| 43 | + if ($this->useHashEq && $this->isPathPrefixOperator($operator)) { |
| 44 | + $this->useHashEq = false; |
| 45 | + } |
| 46 | + |
| 47 | + parent::inspectOperator($operator); |
| 48 | + } |
| 49 | + |
| 50 | + public function processOperator(ISearchOperator &$operator) { |
| 51 | + if (!$this->useHashEq && $operator instanceof ISearchComparison && $operator->getField() === 'path' && $operator->getType() === ISearchComparison::COMPARE_EQUAL) { |
| 52 | + $operator->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false); |
| 53 | + } |
| 54 | + |
| 55 | + parent::processOperator($operator); |
| 56 | + } |
| 57 | + |
| 58 | + private function isPathPrefixOperator(ISearchOperator $operator): bool { |
38 | 59 | if ($operator instanceof ISearchBinaryOperator && $operator->getType() === ISearchBinaryOperator::OPERATOR_OR && count($operator->getArguments()) == 2) { |
39 | 60 | $a = $operator->getArguments()[0]; |
40 | 61 | $b = $operator->getArguments()[1]; |
41 | | - if ($a instanceof ISearchComparison && $b instanceof ISearchComparison && $a->getField() === 'path' && $b->getField() === 'path') { |
42 | | - if ($a->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $b->getType() === ISearchComparison::COMPARE_EQUAL |
43 | | - && $a->getValue() === SearchComparison::escapeLikeParameter($b->getValue()) . '/%') { |
44 | | - $b->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false); |
45 | | - } |
46 | | - if ($b->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $a->getType() === ISearchComparison::COMPARE_EQUAL |
47 | | - && $b->getValue() === SearchComparison::escapeLikeParameter($a->getValue()) . '/%') { |
48 | | - $a->setQueryHint(ISearchComparison::HINT_PATH_EQ_HASH, false); |
49 | | - } |
| 62 | + if ($this->operatorPairIsPathPrefix($a, $b) || $this->operatorPairIsPathPrefix($b, $a)) { |
| 63 | + return true; |
50 | 64 | } |
51 | 65 | } |
| 66 | + return false; |
| 67 | + } |
52 | 68 |
|
53 | | - parent::processOperator($operator); |
| 69 | + private function operatorPairIsPathPrefix(ISearchOperator $like, ISearchOperator $equal): bool { |
| 70 | + return ( |
| 71 | + $like instanceof ISearchComparison && $equal instanceof ISearchComparison && |
| 72 | + $like->getField() === 'path' && $equal->getField() === 'path' && |
| 73 | + $like->getType() === ISearchComparison::COMPARE_LIKE_CASE_SENSITIVE && $equal->getType() === ISearchComparison::COMPARE_EQUAL |
| 74 | + && $like->getValue() === SearchComparison::escapeLikeParameter($equal->getValue()) . '/%' |
| 75 | + ); |
54 | 76 | } |
55 | 77 | } |
0 commit comments