Skip to content

Commit 4538437

Browse files
Prisma query performance (#65)
* Optimize healthcheck query Co-authored-by: me <me@kentcdodds.com> * Add indexes for slow queries Co-authored-by: me <me@kentcdodds.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent 9644987 commit 4538437

5 files changed

Lines changed: 19 additions & 37 deletions

File tree

app/routes/resources+/healthcheck.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22
import { type LoaderFunctionArgs } from 'react-router'
33
import { prisma } from '#app/utils/db.server.ts'
44

5-
export async function loader({ request }: LoaderFunctionArgs) {
6-
const host =
7-
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')
8-
5+
export async function loader(_args: LoaderFunctionArgs) {
96
try {
10-
// if we can connect to the database and make a simple query
11-
// and make a HEAD request to ourselves, then we're good.
12-
await Promise.all([
13-
prisma.user.count(),
14-
fetch(`${new URL(request.url).protocol}${host}`, {
15-
method: 'HEAD',
16-
headers: { 'X-Healthcheck': 'true' },
17-
}).then((r) => {
18-
if (!r.ok) return Promise.reject(r)
19-
}),
20-
])
7+
// If we can connect and run a trivial query, then we're good.
8+
await prisma.$queryRaw`SELECT 1`
219
return new Response('OK')
2210
} catch (error: unknown) {
2311
console.log('healthcheck ❌', { error })

docs/skills/epic-deployment/SKILL.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,9 @@ tls_skip_verify = false
116116

117117
```typescript
118118
// app/routes/resources/healthcheck.tsx
119-
export async function loader({ request }: Route.LoaderArgs) {
120-
const host =
121-
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')
122-
119+
export async function loader(_args: Route.LoaderArgs) {
123120
try {
124-
await Promise.all([
125-
prisma.user.count(), // Verify DB
126-
fetch(`${new URL(request.url).protocol}${host}`, {
127-
method: 'HEAD',
128-
headers: { 'X-Healthcheck': 'true' },
129-
}),
130-
])
121+
await prisma.$queryRaw`SELECT 1` // Verify DB connectivity
131122
return new Response('OK')
132123
} catch (error) {
133124
console.log('healthcheck ❌', { error })

docs/skills/epic-routing/SKILL.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,10 @@ Resource routes don't render UI; they only return data or perform actions.
200200

201201
```typescript
202202
// app/routes/resources/healthcheck.tsx
203-
export async function loader({ request }: Route.LoaderArgs) {
203+
export async function loader(_args: Route.LoaderArgs) {
204204
// Check application health
205-
const host =
206-
request.headers.get('X-Forwarded-Host') ?? request.headers.get('host')
207-
208205
try {
209-
await Promise.all([
210-
prisma.user.count(), // Check DB
211-
fetch(`${new URL(request.url).protocol}${host}`, {
212-
method: 'HEAD',
213-
headers: { 'X-Healthcheck': 'true' },
214-
}),
215-
])
206+
await prisma.$queryRaw`SELECT 1` // Check DB connectivity
216207
return new Response('OK')
217208
} catch (error) {
218209
return new Response('ERROR', { status: 500 })
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- CreateIndex
2+
CREATE INDEX "User_stripeId_idx" ON "User"("stripeId");
3+
4+
-- CreateIndex
5+
CREATE INDEX "Recipient_verified_disabled_idx" ON "Recipient"("verified", "disabled");
6+
7+
-- CreateIndex
8+
CREATE INDEX "Message_recipientId_sentAt_order_idx" ON "Message"("recipientId", "sentAt", "order");

prisma/schema.prisma

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ model User {
2424
roles Role[]
2525
sessions Session[]
2626
recipients Recipient[]
27+
28+
@@index([stripeId])
2729
}
2830

2931
model Password {
@@ -125,6 +127,7 @@ model Recipient {
125127
126128
// non-unique foreign key
127129
@@index([userId])
130+
@@index([verified, disabled])
128131
}
129132

130133
model Message {
@@ -142,6 +145,7 @@ model Message {
142145
143146
// non-unique foreign key
144147
@@index([recipientId])
148+
@@index([recipientId, sentAt, order])
145149
}
146150

147151
model SourceNumber {

0 commit comments

Comments
 (0)