SQL Querying Best Practices?

Just want to discuss query best practices. I using drizzle here but I'm just talking about SQL in general.
const userAccount = await db.query.accounts.findFirst({
where: eq(accounts.id, userId),
with: {
activeProfile: {
with: {
savedShows: {
where: eq(myShows.id, input.id),
limit: 1, // Does using limit matter here? It should speed up query right?
},
},
},
},
})
const userAccount = await db.query.accounts.findFirst({
where: eq(accounts.id, userId),
with: {
activeProfile: {
with: {
savedShows: {
where: eq(myShows.id, input.id),
limit: 1, // Does using limit matter here? It should speed up query right?
},
},
},
},
})
And since I only need the nested relations. Relevant Drizzle docs: https://orm.drizzle.team/docs/rqb#partial-fields-select
const userAccount = await db.query.accounts.findFirst({
columns: {}, // Would this matter? Does it speed up queries? It would transfer less data but is that all?
where: eq(accounts.id, userId),
with: {
activeProfile: {
with: {
savedShows: {
where: eq(myShows.id, input.id),
limit: 1,
},
},
},
},
})
const userAccount = await db.query.accounts.findFirst({
columns: {}, // Would this matter? Does it speed up queries? It would transfer less data but is that all?
where: eq(accounts.id, userId),
with: {
activeProfile: {
with: {
savedShows: {
where: eq(myShows.id, input.id),
limit: 1,
},
},
},
},
})
Also, this is a query to check if a show is saved to a profile. It just returns true or false. I feel there is probably a better way to query this but I'm not sure how. Maybe a way using count?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server