-
-
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 7 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,74 @@ 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(); | ||||||
| \OC::$server->getLogger()->debug("QuerySearchHelper::checkStorageAndPathFilter: storage=" . $storage . " " . "path=" . $path); | ||||||
| $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) { | ||||||
| \OC::$server->getLogger()->debug("QuerySearchHelper::optimizeStorageFilters: storage=" . $storage . " " . "paths=" . implode(", ", $paths)); | ||||||
|
||||||
| \OC::$server->getLogger()->debug("QuerySearchHelper::optimizeStorageFilters: storage=" . $storage . " " . "paths=" . implode(", ", $paths)); | |
| $this->logger->debug("QuerySearchHelper::optimizeStorageFilters: storage=" . $storage . " " . "paths=" . implode(", ", $paths)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to remove the logging. This was useful during development, quite unlikely to be useful in the future. Removed in f86dab9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 ((x IN (list1-1000)) or (x IN (list1001-2000)) ...) e.g. here:
| public function in($x, $y, $type = null): string { |
I thought this might be resolved already by Doctrine DBAL, but apparently a similar workaround has been rejected. 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using (IN(1-1000) OR IN(1001-2000)) in mail: https://github.com/nextcloud/mail/blob/0da32c97f64e0cdf950368257447ec8fa6fe7fea/lib/Db/MessageMapper.php#L986-L992.
Not a big fan, but there is no other way if you want to work with all IDs in one result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to remove the logging. This was useful during development, quite unlikely to be useful in the future. Removed in f86dab9