Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit ee2e50a

Browse files
bugfix(export) Fix performance of export information endpoints (#1599)
Co-authored-by: Rafał Przędzik <46092482+rprzedzik@users.noreply.github.com>
1 parent 8cc2bd2 commit ee2e50a

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

22
## CHANGELOG FOR 1.0.x
3+
#### 1.0.5
4+
- bugfix [#1591](https://github.com/ergonode/backend/issues/1591) Slow export information endpoint requests (rprzedzik)
5+
36
#### 1.0.4
47
- bugfix [#1548](https://github.com/ergonode/backend/issues/1548) Plain text password in CreateUserEvent (rprzedzik)
58
- bugfix [#1545](https://github.com/ergonode/backend/issues/1545) Unnecessary sending of password changing e-mail (rprzedzik)

module/channel/src/Infrastructure/Persistence/Query/DbalExportQuery.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,30 @@ public function getProfileInfo(Language $language): array
3838

3939
return $query
4040
->addSelect('ch.name')
41-
->addSelect('(SELECT count(*) FROM exporter.export_line el WHERE el.export_id = e.id) as items')
42-
->addSelect('(SELECT count(*) FROM exporter.export_line el WHERE el.export_id = e.id
43-
AND processed_at IS NOT NULL) as processed')
44-
->addSelect('(SELECT count(*) FROM exporter.export_error el WHERE el.export_id = e.id) as errors')
41+
->addSelect('CASE WHEN ee.errors IS NULL THEN 0 ELSE ee.errors END AS errors')
42+
->addSelect('CASE WHEN ep.processed IS NULL THEN 0 ELSE ep.processed END AS processed')
43+
->addSelect('CASE WHEN ei.items IS NULL THEN 0 ELSE ei.items END AS items')
4544
->orderBy('started_at', 'DESC')
4645
->join('e', self::TABLE_CHANNEL, 'ch', 'ch.id = e.channel_id')
46+
->leftJoin(
47+
'e',
48+
'(SELECT count(*) as errors, export_id FROM exporter.export_error GROUP BY export_id)',
49+
'ee',
50+
'ee.export_id = e.id'
51+
)
52+
->leftJoin(
53+
'e',
54+
'(SELECT count(*) as items, export_id FROM exporter.export_line GROUP BY export_id)',
55+
'ei',
56+
'ei.export_id = e.id'
57+
)
58+
->leftJoin(
59+
'e',
60+
'(SELECT count(*) as processed, export_id FROM exporter.export_line
61+
WHERE processed_at IS NOT NULL GROUP BY export_id)',
62+
'ep',
63+
'ep.export_id = e.id'
64+
)
4765
->setMaxResults(10)
4866
->execute()
4967
->fetchAll();
@@ -57,11 +75,29 @@ public function getInformation(ExportId $exportId): array
5775
$query = $this->getQuery();
5876

5977
return $query
60-
->addSelect('(SELECT count(*) FROM exporter.export_line el WHERE el.export_id = e.id) as items')
61-
->addSelect('(SELECT count(*) FROM exporter.export_line el WHERE el.export_id = e.id
62-
AND processed_at IS NOT NULL) as processed')
63-
->addSelect('(SELECT count(*) FROM exporter.export_error el WHERE el.export_id = e.id) as errors')
78+
->addSelect('CASE WHEN ee.errors IS NULL THEN 0 ELSE ee.errors END AS errors')
79+
->addSelect('CASE WHEN ep.processed IS NULL THEN 0 ELSE ep.processed END AS processed')
80+
->addSelect('CASE WHEN ei.items IS NULL THEN 0 ELSE ei.items END AS items')
6481
->where($query->expr()->eq('id', ':exportId'))
82+
->leftJoin(
83+
'e',
84+
'(SELECT count(*) as errors, export_id FROM exporter.export_error GROUP BY export_id)',
85+
'ee',
86+
'ee.export_id = e.id'
87+
)
88+
->leftJoin(
89+
'e',
90+
'(SELECT count(*) as items, export_id FROM exporter.export_line GROUP BY export_id)',
91+
'ei',
92+
'ei.export_id = e.id'
93+
)
94+
->leftJoin(
95+
'e',
96+
'(SELECT count(*) as processed, export_id FROM exporter.export_line
97+
WHERE processed_at IS NOT NULL GROUP BY export_id)',
98+
'ep',
99+
'ep.export_id = e.id'
100+
)
65101
->setParameter(':exportId', $exportId->getValue())
66102
->execute()
67103
->fetch();

module/importer/src/Infrastructure/Persistence/Query/DbalImportQuery.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,39 @@ public function getProfileInfo(Language $language): array
9797
return $this->connection->createQueryBuilder()
9898
->select('i.id, status, started_at, ended_at')
9999
->addSelect('s.name')
100-
->addSelect('(SELECT count(*) FROM importer.import_line il WHERE il.import_id = i.id) as items')
101-
->addSelect('(SELECT count(*) FROM importer.import_line il
102-
WHERE il.import_id = i.id AND il.status IS NOT NULL) AS processed')
103-
->addSelect('(SELECT count(*) FROM importer.import_line il
104-
WHERE il.import_id = i.id AND il.status = \'success\') AS succeeded')
105-
->addSelect('(SELECT count(*) FROM importer.import_error ie WHERE ie.import_id = i.id) AS errors')
100+
->addSelect('(CASE WHEN ii.items IS NULL THEN 0 ELSE ii.items END) AS items')
101+
->addSelect('(CASE WHEN ip.processed IS NULL THEN 0 ELSE ip.processed END) AS processed')
102+
->addSelect('(CASE WHEN ic.succeeded IS NULL THEN 0 ELSE ic.succeeded END) AS succeeded')
103+
->addSelect('(CASE WHEN ie.errors IS NULL THEN 0 ELSE ie.errors END) AS errors')
106104
->from(self::TABLE, 'i')
107105
->orderBy('started_at', 'DESC')
108106
->join('i', self::TABLE_SOURCE, 's', 's.id = i.source_id')
107+
->leftJoin(
108+
'i',
109+
'(SELECT count(*) AS items, import_id FROM importer.import_line GROUP BY import_id)',
110+
'ii',
111+
'ii.import_id = i.id'
112+
)
113+
->leftJoin(
114+
'i',
115+
'(SELECT count(*) AS processed, import_id FROM importer.import_line
116+
WHERE status IS NOT NULL GROUP BY import_id)',
117+
'ip',
118+
'ip.import_id = i.id'
119+
)
120+
->leftJoin(
121+
'i',
122+
'(SELECT count(*) AS succeeded, import_id FROM importer.import_line
123+
WHERE status = \'success\' GROUP BY import_id)',
124+
'ic',
125+
'ic.import_id = i.id'
126+
)
127+
->leftJoin(
128+
'i',
129+
'(SELECT count(*) AS errors, import_id FROM importer.import_error GROUP BY import_id)',
130+
'ie',
131+
'ie.import_id = i.id'
132+
)
109133
->setMaxResults(10)
110134
->execute()
111135
->fetchAll();

0 commit comments

Comments
 (0)