-
Notifications
You must be signed in to change notification settings - Fork 48
perf: Enhance objects using constant sql queries count to get rid of N+1 queries #2857
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 |
|---|---|---|
|
|
@@ -328,7 +328,8 @@ public function deleteTable(int $tableId): DataResponse { | |
| #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)] | ||
| public function indexViews(int $tableId): DataResponse { | ||
| try { | ||
| return new DataResponse($this->viewService->formatViews($this->viewService->findAll($this->tableService->find($tableId)))); | ||
| $table = $this->tableService->find($tableId); | ||
| return new DataResponse($this->viewService->formatViews($table->getViews() ?? [])); | ||
|
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. initially, we return 403 for read-only users . Now, read-only user gets 200 with an empty list, since enhanceTables only loads views for owned or managed tables. So we need to mofify the annotation? |
||
| } catch (PermissionError $e) { | ||
| $this->logger->warning('A permission error occurred: ' . $e->getMessage(), ['exception' => $e]); | ||
| $message = ['message' => $e->getMessage()]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,24 +165,30 @@ public function getColumnTypes(array $neededColumnIds): array { | |
| } | ||
|
|
||
| /** | ||
| * @param int $tableId | ||
| * @return int | ||
| * @param int[] $tableIds | ||
| * @return array<int, int> | ||
| */ | ||
| public function countColumns(int $tableId): int { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select($qb->func()->count('*', 'counter')); | ||
| $qb->from($this->table); | ||
| $qb->where( | ||
| $qb->expr()->eq('table_id', $qb->createNamedParameter($tableId)) | ||
| ); | ||
| public function countColumnsForTables(array $tableIds): array { | ||
| if (empty($tableIds)) { | ||
| return []; | ||
| } | ||
|
|
||
| try { | ||
| $result = $this->findOneQuery($qb); | ||
| return (int)$result['counter']; | ||
| } catch (DoesNotExistException|MultipleObjectsReturnedException|Exception $e) { | ||
| $this->logger->warning('Exception occurred: ' . $e->getMessage() . ' Returning 0.'); | ||
| return 0; | ||
| $counts = array_fill_keys($tableIds, 0); | ||
| foreach (array_chunk($tableIds, 1000 - 1) as $tableIdsChunk) { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select('table_id', $qb->func()->count('*', 'counter')) | ||
| ->from($this->table) | ||
| ->where($qb->expr()->in('table_id', $qb->createNamedParameter($tableIdsChunk, IQueryBuilder::PARAM_INT_ARRAY))) | ||
| ->groupBy('table_id'); | ||
|
|
||
| $result = $qb->executeQuery(); | ||
| while ($row = $result->fetch()) { | ||
| $counts[(int)$row['table_id']] = (int)$row['counter']; | ||
| } | ||
| $result->closeCursor(); | ||
|
Comment on lines
+176
to
+188
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. without try...catch, a |
||
| } | ||
|
|
||
| return $counts; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ public function findAllSharesFor(string $nodeType, array $receivers, string $use | |
| return []; | ||
| } | ||
|
|
||
| $chunks = []; | ||
| $chunks = [[]]; | ||
| // deduct extra parameters (sender, node type, receiver type) | ||
| foreach (array_chunk($receivers, 1000 - 3) as $receiversChunk) { | ||
| $qb = $this->db->getQueryBuilder(); | ||
|
|
@@ -130,13 +130,88 @@ public function findAllSharesForNode(string $nodeType, int $nodeId, string $send | |
| ->andWhere($qb->expr()->eq('node_type', $qb->createNamedParameter($nodeType, IQueryBuilder::PARAM_STR))) | ||
| ->andWhere($qb->expr()->eq('node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))); | ||
|
|
||
| if ($sender !== '') { | ||
| $qb->andWhere($qb->expr()->eq('sender', $qb->createNamedParameter($sender, IQueryBuilder::PARAM_STR))); | ||
| } | ||
|
|
||
|
Comment on lines
+133
to
+136
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. Should this be moved to plural |
||
| if (!empty($excluded)) { | ||
| $qb->andWhere($qb->expr()->notIn('receiver_type', $qb->createNamedParameter($excluded, IQueryBuilder::PARAM_STR_ARRAY))); | ||
| } | ||
|
|
||
| return $this->findEntities($qb); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $nodeType | ||
| * @param int[] $nodeIds | ||
| * @param string $sender | ||
| * @param array<string> $excluded receiver types to exclude from results | ||
| * @return Share[] | ||
| * @throws Exception | ||
| */ | ||
| public function findAllSharesForNodes(string $nodeType, array $nodeIds, string $sender = '', array $excluded = []): array { | ||
|
Koc marked this conversation as resolved.
|
||
| if (empty($nodeIds)) { | ||
| return []; | ||
| } | ||
|
|
||
| $extraParams = 2; | ||
| if ($sender !== '') { | ||
| $extraParams++; | ||
| } | ||
| if (!empty($excluded)) { | ||
| $extraParams++; | ||
| } | ||
|
|
||
| $chunks = [[]]; | ||
| foreach (array_chunk($nodeIds, 1000 - $extraParams) as $nodeIdsChunk) { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select('*') | ||
| ->from($this->table) | ||
| ->andWhere($qb->expr()->eq('node_type', $qb->createNamedParameter($nodeType, IQueryBuilder::PARAM_STR))) | ||
| ->andWhere($qb->expr()->in('node_id', $qb->createNamedParameter($nodeIdsChunk, IQueryBuilder::PARAM_INT_ARRAY))); | ||
|
|
||
| if ($sender !== '') { | ||
| $qb->andWhere($qb->expr()->eq('sender', $qb->createNamedParameter($sender, IQueryBuilder::PARAM_STR))); | ||
| } | ||
|
|
||
| if (!empty($excluded)) { | ||
| $qb->andWhere($qb->expr()->notIn('receiver_type', $qb->createNamedParameter($excluded, IQueryBuilder::PARAM_STR_ARRAY))); | ||
| } | ||
|
|
||
| $chunks[] = $this->findEntities($qb); | ||
| } | ||
|
|
||
| return array_merge(...$chunks); | ||
| } | ||
|
|
||
| /** | ||
| * @param int[] $tableIds | ||
| * @return int[] | ||
| */ | ||
| public function findLinkSharesForTables(array $tableIds): array { | ||
| if (empty($tableIds)) { | ||
| return []; | ||
| } | ||
|
|
||
| $linkTableIds = []; | ||
| foreach (array_chunk($tableIds, 1000 - 2) as $tableIdsChunk) { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->selectDistinct('node_id') | ||
| ->from($this->table) | ||
| ->andWhere($qb->expr()->eq('node_type', $qb->createNamedParameter('table', IQueryBuilder::PARAM_STR))) | ||
| ->andWhere($qb->expr()->eq('receiver_type', $qb->createNamedParameter(ShareReceiverType::LINK, IQueryBuilder::PARAM_STR))) | ||
| ->andWhere($qb->expr()->in('node_id', $qb->createNamedParameter($tableIdsChunk, IQueryBuilder::PARAM_INT_ARRAY))); | ||
|
|
||
| $result = $qb->executeQuery(); | ||
| while ($row = $result->fetch()) { | ||
| $linkTableIds[] = (int)$row['node_id']; | ||
| } | ||
| $result->closeCursor(); | ||
| } | ||
|
|
||
| return $linkTableIds; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $nodeType | ||
| * @param int $nodeId | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,6 +127,8 @@ public function insert(Entity $entity): View { | |
| } | ||
|
|
||
| /** | ||
| * @deprecated Use {@link findAllByTableIds} instead | ||
| * | ||
| * @return View[] | ||
| * @throws Exception | ||
| */ | ||
|
|
@@ -142,6 +144,25 @@ public function findAll(?int $tableId = null): array { | |
| return $this->findEntities($qb); | ||
| } | ||
|
|
||
| /** | ||
| * @param int[] $tableIds | ||
| * @return View[] | ||
| * @throws Exception | ||
| */ | ||
| public function findAllByTableIds(array $tableIds): array { | ||
| if (empty($tableIds)) { | ||
| return []; | ||
| } | ||
|
|
||
| $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.table_id', $qb->createNamedParameter($tableIds, IQueryBuilder::PARAM_INT_ARRAY))); | ||
|
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. don't we need same |
||
|
|
||
| return $this->findEntities($qb); | ||
| } | ||
|
|
||
| /** | ||
| * @return View[] | ||
| * @throws Exception | ||
|
|
||
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.
As a downstream effect of the
findAllSharesForNodechange above, the list all shares" endpoint now returns only shares created by the current user. An owner will no longer see shares created by co-managers on their own table or view, so they cannot review or revoke them here?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.
same for sharecontroller.php