-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve monitor query performance
- Loading branch information
1 parent
89fed46
commit f5c13cb
Showing
10 changed files
with
136 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { describe, bench } from 'vitest'; | ||
import { prisma } from '../_client'; | ||
|
||
const workspaceId = process.env.BENCH_MONITOR_WORKSPACEID; | ||
const monitorId = process.env.BENCH_MONITOR_ID; | ||
|
||
describe.runIf(workspaceId && monitorId)('getMonitorRecentData', () => { | ||
bench('find with join', async () => { | ||
await prisma.monitorData | ||
.findMany({ | ||
where: { | ||
monitor: { | ||
id: monitorId, | ||
workspaceId, | ||
}, | ||
}, | ||
take: 20, | ||
orderBy: { | ||
createdAt: 'desc', | ||
}, | ||
select: { | ||
value: true, | ||
createdAt: true, | ||
}, | ||
}) | ||
.then((arr) => arr.reverse()); | ||
}); | ||
|
||
bench('find with double check', async () => { | ||
await prisma.monitor.findFirstOrThrow({ | ||
where: { | ||
workspaceId, | ||
id: monitorId, | ||
}, | ||
}); | ||
|
||
await prisma.monitorData | ||
.findMany({ | ||
where: { | ||
monitorId, | ||
}, | ||
take: 20, | ||
orderBy: { | ||
createdAt: 'desc', | ||
}, | ||
select: { | ||
value: true, | ||
createdAt: true, | ||
}, | ||
}) | ||
.then((arr) => arr.reverse()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/server/prisma/migrations/20240423074833_add_indexes_for_monitor/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- CreateIndex | ||
CREATE INDEX "MonitorData_monitorId_createdAt_idx" ON "MonitorData"("monitorId", "createdAt" DESC); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "MonitorData_monitorId_createdAt_value_idx" ON "MonitorData"("monitorId", "createdAt", "value"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "MonitorEvent_monitorId_idx" ON "MonitorEvent"("monitorId"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters