-
Notifications
You must be signed in to change notification settings - Fork 34
Perf: Optimize shares loading by eliminating N+1 queries #2293
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\Db; | ||
|
|
||
| use OCP\IDBConnection; | ||
|
|
||
| /** | ||
| * Trait that helps mappers to avoid errors with too many parameters | ||
| */ | ||
| trait BulkFetchTrait { | ||
| private function getChunkSize(int $extraParameters = 0): int { | ||
| $maxParameters = match ($this->db->getDatabaseProvider()) { | ||
| IDBConnection::PLATFORM_ORACLE => 1000, | ||
| default => 65_535, | ||
| }; | ||
| return $maxParameters - $extraParameters; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
|
|
||
| /** @template-extends QBMapper<View> */ | ||
| class ViewMapper extends QBMapper { | ||
| use BulkFetchTrait; | ||
|
|
||
| protected string $table = 'tables_views'; | ||
|
|
||
| protected CappedMemoryCache $cache; | ||
|
|
@@ -50,6 +52,43 @@ public function find(int $id): View { | |
| return $this->cache[$cacheKey]; | ||
| } | ||
|
|
||
| /** | ||
| * @param int[] $ids | ||
| * @return array<int, View> indexed by view id | ||
| */ | ||
| public function findMany(array $ids): array { | ||
| $missing = []; | ||
| $result = []; | ||
| foreach ($ids as $id) { | ||
|
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. probably more theoretical than not, it must not be more than 1k $ids. On the safe side it would be be wrapped in array_chunk, for instance.
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. yeah, in theory it's can be possible, see #2242, but limit is 65535 for Postgres. Not sure, that it's real case scenario to have so many views. For rows definitely, but for views or tables... I'm not sure. If you really want to add
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. The server will complain at 1000 for Oracle (cf. #1061). Smallest possible value wins there. I am a bit torn about a defensive, more solid approach and little YAGNI. Views is more likely to reach that number. In the regular case, it won't really have much of an impact either. Let's do it with chunking.
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. added trait to determinate max available parameters for db vendor + chunking for shares/tables/views
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. It would still run against https://github.com/nextcloud/server/blob/master/lib/private/DB/QueryBuilder/QueryBuilder.php#L225 We avoid special solutions for different DB engines, or move it as much up as possible in the stack, if there has to be some sort of handling. In this case the change would also be better moving up; the risk is that it becomes harder to spot such cases.
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. mate, sorry, I didn't get what to do here? A. just hardcode 1000 for all DB engines
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. BTW there is similar PR from you where you don't care about parameters count |
||
| $cacheKey = (string)$id; | ||
| if (isset($this->cache[$cacheKey])) { | ||
| $result[$id] = $this->cache[$cacheKey]; | ||
| } else { | ||
| $missing[$id] = true; | ||
| } | ||
| } | ||
|
|
||
| if (!$missing) { | ||
| return $result; | ||
| } | ||
|
|
||
| $missing = array_keys($missing); | ||
| foreach (array_chunk($missing, $this->getChunkSize()) as $missingChunk) { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select('v.*', 't.ownership') | ||
| ->from($this->table, 'v') | ||
| ->innerJoin('v', 'tables_tables', 't', 't.id = v.table_id') | ||
| ->where($qb->expr()->in('v.id', $qb->createNamedParameter($missingChunk, IQueryBuilder::PARAM_INT_ARRAY))); | ||
|
|
||
| foreach ($this->findEntities($qb) as $entity) { | ||
| $id = $entity->getId(); | ||
| $this->cache[(string)$id] = $entity; | ||
| $result[$id] = $entity; | ||
| } | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| public function delete(Entity $entity): View { | ||
| unset($this->cache[(string)$entity->getId()]); | ||
| return parent::delete($entity); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.