-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Performance of WebDAV search requests with many shared files #37061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
51b8e53
f4486d9
e063d9f
7814687
f649864
5e1e692
2987c0d
f86dab9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |||
| use OC\Files\Cache\Wrapper\CacheJail; | ||||
| use OC\Files\Search\QueryOptimizer\QueryOptimizer; | ||||
| use OC\Files\Search\SearchBinaryOperator; | ||||
| use OC\Files\Search\SearchComparison; | ||||
| use OC\SystemConfig; | ||||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||||
| use OCP\Files\Cache\ICache; | ||||
|
|
@@ -36,6 +37,8 @@ | |||
| use OCP\Files\IRootFolder; | ||||
| use OCP\Files\Mount\IMountPoint; | ||||
| use OCP\Files\Search\ISearchBinaryOperator; | ||||
| use OCP\Files\Search\ISearchComparison; | ||||
| use OCP\Files\Search\ISearchOperator; | ||||
| use OCP\Files\Search\ISearchQuery; | ||||
| use OCP\IDBConnection; | ||||
| use OCP\IGroupManager; | ||||
|
|
@@ -82,11 +85,72 @@ protected function getQueryBuilder() { | |||
| ); | ||||
| } | ||||
|
|
||||
| private function isCompareEqual(ISearchOperator $operator, string $fieldName): bool { | ||||
| return | ||||
| $operator instanceof ISearchComparison && | ||||
| $operator->getType() === ISearchComparison::COMPARE_EQUAL && | ||||
| $operator->getField() === $fieldName; | ||||
| } | ||||
|
|
||||
| private function checkStorageAndPathFilter(ISearchOperator $operator, array &$storageToPathsMap, array &$storageOtherFilters): void { | ||||
| if ($operator instanceof ISearchBinaryOperator && $operator->getType() === ISearchBinaryOperator::OPERATOR_AND && count($operator->getArguments()) == 2) { | ||||
| $a = $operator->getArguments()[0]; | ||||
| $b = $operator->getArguments()[1]; | ||||
| if ($this->isCompareEqual($a, "storage") && $this->isCompareEqual($b, "path")) { | ||||
| /** @psalm-suppress UndefinedInterfaceMethod */ | ||||
| $storage = $a->getValue(); | ||||
| /** @psalm-suppress UndefinedInterfaceMethod */ | ||||
| $path = $b->getValue(); | ||||
| $storageToPathsMap[$storage][] = $path; | ||||
| return; | ||||
| } | ||||
|
||||
| } | ||||
| $storageOtherFilters[] = $operator; | ||||
| } | ||||
|
|
||||
| private function optimizeStorageFilters(array $storageFilters): array { | ||||
| // | ||||
| // Optimize the storage filters query, when there are many shared files. | ||||
| // | ||||
| // Originally for each shared file the following section is added to the SQL WHERE clause: | ||||
| // | ||||
| // (`storage` = <storage-id>) AND (`path` = <file-path>) | ||||
| // | ||||
| // When many files are shared between the same two users, the storage part of the filter is repeated many times. | ||||
| // | ||||
| // Here we want to refactor the query to have a single filter for each storage | ||||
| // and provide all `path_hash` values for the same storage in the IN clause: | ||||
| // | ||||
| // (`storage` = <storage-id>) AND (`path_hash` IN (<path-hash-1>, <path-hash-2>, ...)) | ||||
|
|
||||
| // Pick up single file shares to prepare more efficient query | ||||
| $storageToPathsMap = []; | ||||
| $storageOtherFilters = []; | ||||
| foreach ($storageFilters as $storageFilter) { | ||||
| $this->checkStorageAndPathFilter($storageFilter, $storageToPathsMap, $storageOtherFilters); | ||||
| } | ||||
|
|
||||
| // Create filters for single file shares | ||||
| $singleFileFilters = []; | ||||
| foreach ($storageToPathsMap as $storage => $paths) { | ||||
| $singleFileFilters[] = new SearchBinaryOperator( | ||||
| ISearchBinaryOperator::OPERATOR_AND, | ||||
| [ | ||||
| new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'storage', $storage), | ||||
| new SearchComparison(ISearchComparison::COMPARE_IN, 'path_hash', array_map(fn ($path) => md5($path), $paths)) | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be careful with "IN" it'll break i.e. on Oracle for 1000+ items.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. Would it make sense to provide a workaround at a lower level? Rewrite the query with a sequence of
I thought this might be resolved already by Doctrine DBAL, but apparently a similar workaround has been rejected. 😞
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't tell since I am not an expert on php/doctrine, but @nickvergessen @juliushaertl or @icewind1991 might know.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are using Not a big fan, but there is no other way if you want to work with all IDs in one result. |
||||
| ] | ||||
| ); | ||||
| } | ||||
|
|
||||
| return array_merge($storageOtherFilters, $singleFileFilters); | ||||
| } | ||||
|
|
||||
| protected function applySearchConstraints(CacheQueryBuilder $query, ISearchQuery $searchQuery, array $caches): void { | ||||
| $storageFilters = array_values(array_map(function (ICache $cache) { | ||||
| return $cache->getQueryFilterForStorage(); | ||||
| }, $caches)); | ||||
| $storageFilter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $storageFilters); | ||||
| $optimizedStorageFilters = $this->optimizeStorageFilters($storageFilters); | ||||
| $storageFilter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $optimizedStorageFilters); | ||||
| $filter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$searchQuery->getSearchOperation(), $storageFilter]); | ||||
| $this->queryOptimizer->processOperator($filter); | ||||
|
|
||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.