Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Speed-up queries

Blue Mouse873e714c7436
diff --git a/backend/apps/cloud/src/alert/alert.controller.ts b/backend/apps/cloud/src/alert/alert.controller.ts
index 8f3cf1fce..a41334175 100644
--- a/backend/apps/cloud/src/alert/alert.controller.ts
+++ b/backend/apps/cloud/src/alert/alert.controller.ts
@@ -137,7 +137,6 @@ export class AlertController {
const user = await this.userService.findOne({
where: { id: uid },
- relations: ['projects'],
})
if (_isEmpty(user)) {
@@ -167,7 +166,7 @@ export class AlertController {
'You are not allowed to add alerts to this project',
)
- const pids = _map(user.projects, (userProject) => userProject.id)
+ const pids = await this.projectService.getProjectIdsByAdminId(uid)
const alertsCount = await this.alertService.count({
where: { project: { id: In(pids) } },
})
diff --git a/backend/apps/cloud/src/experiment/experiment.controller.ts b/backend/apps/cloud/src/experiment/experiment.controller.ts
index 735534a36..52f230267 100644
--- a/backend/apps/cloud/src/experiment/experiment.controller.ts
+++ b/backend/apps/cloud/src/experiment/experiment.controller.ts
@@ -189,7 +189,6 @@ export class ExperimentController {
const user = await this.userService.findOne({
where: { id: uid },
- relations: ['projects'],
})
if (!user.isActive) {
diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
index 229336136..4e9bf5317 100644
--- a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
@@ -168,7 +168,6 @@ export class FeatureFlagController {
const user = await this.userService.findOne({
where: { id: uid },
- relations: ['projects'],
})
if (!user.isActive) {
diff --git a/backend/apps/cloud/src/goal/goal.controller.ts b/backend/apps/cloud/src/goal/goal.controller.ts
index 14642305f..657554968 100644
--- a/backend/apps/cloud/src/goal/goal.controller.ts
+++ b/backend/apps/cloud/src/goal/goal.controller.ts
@@ -174,7 +174,6 @@ export class GoalController {
const user = await this.userService.findOne({
where: { id: uid },
- relations: ['projects'],
})
if (!user.isActive) {
diff --git a/backend/apps/cloud/src/project/project.controller.ts b/backend/apps/cloud/src/project/project.controller.ts
index 80b6a697f..89324e5fb 100644
--- a/backend/apps/cloud/src/project/project.controller.ts
+++ b/backend/apps/cloud/src/project/project.controller.ts
@@ -899,7 +899,7 @@ export class ProjectController {
const organisation = await this.organisationService.findOne({
where: { id: orgId },
- relations: ['members', 'members.user', 'projects'],
+ relations: ['members', 'members.user'],
})
if (_isEmpty(organisation)) {
@@ -933,7 +933,7 @@ export class ProjectController {
const organisation = await this.organisationService.findOne({
where: { id: orgId },
- relations: ['members', 'members.user', 'projects'],
+ relations: ['members', 'members.user'],
})
if (_isEmpty(organisation)) {
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index 1fcc0117b..052b4f1ba 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -598,6 +598,14 @@ export class ProjectService implements OnModuleDestroy {
return this.projectsRepository.count({ where: { admin: { id: adminId } } })
}
+ async getProjectIdsByAdminId(adminId: string): Promise<string[]> {
+ const projects = await this.projectsRepository.find({
+ where: { admin: { id: adminId } },
+ select: ['id'],
+ })
+ return _map(projects, 'id')
+ }
+
async create(project: DeepPartial<Project>) {
return this.projectsRepository.save(project)
}
diff --git a/backend/apps/cloud/src/task-manager/task-manager.service.ts b/backend/apps/cloud/src/task-manager/task-manager.service.ts
index f9bb5f5dd..724132e02 100644
--- a/backend/apps/cloud/src/task-manager/task-manager.service.ts
+++ b/backend/apps/cloud/src/task-manager/task-manager.service.ts
@@ -1018,7 +1018,6 @@ export class TaskManagerService {
dashboardBlockReason: IsNull(),
},
],
- relations: ['projects'],
select: ['id', 'email', 'planCode'],
})
const emailParams = {
@@ -1027,9 +1026,11 @@ export class TaskManagerService {
}
const promises = _map(users, async (user) => {
- const { id, email, planCode, projects } = user
+ const { id, email, planCode } = user
- if (_isEmpty(projects) || _isNull(projects)) {
+ const hasProjects = (await this.projectService.countByAdminId(id)) > 0
+
+ if (!hasProjects) {
return
}
@@ -1096,7 +1097,13 @@ export class TaskManagerService {
},
},
],
- relations: ['admin'],
+ select: [
+ 'id',
+ 'paddleApiKeyEnc',
+ 'stripeApiKeyEnc',
+ 'revenueCurrency',
+ 'revenueLastSyncAt',
+ ],
})
if (_isEmpty(projects)) {
@@ -1218,6 +1225,7 @@ export class TaskManagerService {
where: {
cancellationEffectiveDate: Not(IsNull()),
},
+ select: ['id', 'cancellationEffectiveDate'],
})
const promises = _map(users, async (user) => {
@@ -1259,6 +1267,7 @@ export class TaskManagerService {
),
trialReminderSent: false,
},
+ select: ['id', 'email'],
})
const promises = _map(users, async (user) => {
@@ -1289,6 +1298,7 @@ export class TaskManagerService {
planCode: PlanCode.trial,
trialEndDate: IsNull(),
},
+ select: ['id', 'created'],
})
const promises = _map(users, async (user) => {
@@ -1321,6 +1331,7 @@ export class TaskManagerService {
trialEndDate: IsNull(),
},
],
+ select: ['id', 'email'],
})
const promises = _map(users, async (user) => {
@@ -1869,7 +1880,6 @@ export class TaskManagerService {
where: {
reportFrequency: Not(UserReportFrequency.Never),
},
- relations: ['projects'],
select: ['id'],
})
const now = dayjs.utc().format('YYYY-MM-DD')
@@ -1877,15 +1887,15 @@ export class TaskManagerService {
const nineWeeksAgo = dayjs.utc().subtract(9, 'w').format('YYYY-MM-DD')
await mapLimit(users, REPORTS_USERS_CONCURRENCY, async (user) => {
- const { id, projects } = user
+ const { id } = user
try {
- if (_isEmpty(projects) || _isNull(projects)) {
+ const pids = await this.projectService.getProjectIdsByAdminId(id)
+
+ if (_isEmpty(pids)) {
return
}
- const pids = _map(projects, (p) => p.id)
-
// No need to check for performance activity because it's not tracked without tracking analytics
const queryAnalytics = `SELECT count() FROM analytics WHERE pid IN ({pids:Array(FixedString(12))}) AND created BETWEEN {nineWeeksAgo:String} AND {now:String}`
const queryCaptcha = `SELECT count() FROM captcha WHERE pid IN ({pids:Array(FixedString(12))}) AND created BETWEEN {nineWeeksAgo:String} AND {now:String}`
diff --git a/backend/apps/cloud/src/user/user.controller.ts b/backend/apps/cloud/src/user/user.controller.ts
index 1d7d7d034..17072d929 100644
--- a/backend/apps/cloud/src/user/user.controller.ts
+++ b/backend/apps/cloud/src/user/user.controller.ts
@@ -211,7 +211,6 @@ export class UserController {
const user = await this.userService.findOne({
where: { id },
- relations: ['projects'],
select: ['id', 'planCode'],
})
@@ -224,7 +223,9 @@ export class UserController {
}
try {
- if (!_isEmpty(user.projects)) {
+ const hasProjects = (await this.projectService.countByAdminId(id)) > 0
+
+ if (hasProjects) {
await this.projectService.deleteProjectsForUser(id)
}
await this.actionTokensService.deleteMultiple({ user: { id } })