-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Improve database performance by adding indexes #1593
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Ignored Deployments
|
Thank you for following the naming conventions for pull request titles! 🙏 |
AttributeSettingsTab.tsxConsider destructuring the const { id, name, description, type, archived } = attributeClass;
const defaultValues = { name, description };
const data = { archived: !archived };
const onSubmit = async (data) => {
try {
setisAttributeBeingSubmitted(true);
setOpen(false);
await updateAttributeClass(id, data);
router.refresh();
} catch (error) {
console.error(error);
} finally {
setisAttributeBeingSubmitted(false);
}
};
const handleArchiveToggle = async () => {
try {
setisAttributeBeingSubmitted(true);
await updateAttributeClass(id, data);
} catch (error) {
console.error(error);
} finally {
setisAttributeBeingSubmitted(false);
}
}; packages/lib/team/service.tsThe code in the // Define the start of the month
const now = new Date();
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
// Get all environment IDs for the team
const products = await getProducts(teamId);
const environmentIds = products.flatMap((product) => product.environments.map((env) => env.id));
// Use Prisma's aggregate to count active people for all environments
const peopleAggregations = await prisma.person.aggregate({
_count: {
id: true,
},
where: {
AND: [
{ environmentId: { in: environmentIds } },
{
sessions: {
some: {
createdAt: { gte: firstDayOfMonth },
},
},
},
],
},
});
return peopleAggregations._count.id; The code in the // Define the start of the month
const now = new Date();
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
// Get all environment IDs for the team
const products = await getProducts(teamId);
const environmentIds = products.flatMap((product) => product.environments.map((env) => env.id));
// Use Prisma's aggregate to count responses for all environments
const responseAggregations = await prisma.response.aggregate({
_count: {
id: true,
},
where: {
AND: [
{ survey: { environmentId: { in: environmentIds } } },
{ survey: { type: "web" } },
{ createdAt: { gte: firstDayOfMonth } },
],
},
});
// The result is an aggregation of the total count
return responseAggregations._count.id; packages/lib/survey/service.tsThe functions export const updateSurvey = async (updatedSurvey: TSurvey): Promise<TSurvey> => {
// function body
};
export const createSurvey = async (environmentId: string, surveyBody: TSurveyInput): Promise<TSurvey> => {
// function body
};
export const deleteSurvey = async (surveyId: string) => {
// function body
};
export const duplicateSurvey = async (environmentId: string, surveyId: string) => {
// function body
}; schema.prismaAdding indexes to the database can significantly improve the performance of read operations. However, it's important to note that while indexes speed up read operations, they slow down write operations (such as INSERT and UPDATE) because the database needs to update the index every time a record is added or modified. Therefore, it's crucial to find a balance and only add indexes to columns that are frequently used in WHERE clauses or JOIN operations. model User {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
email String @unique
hashedPassword String?
name String?
avatarUrl String?
emailVerified Boolean @default(false)
emailVerificationCode String? @unique
providerAccounts ProviderAccount[]
memberships Membership[]
invitesCreated Invite[] @relation("inviteCreatedBy")
invitesAccepted Invite[] @relation("inviteAcceptedBy")
role Role?
objective Objective?
notificationSettings Json @default("{}")
@@index([email])
} Adding indexes to the database can significantly improve the performance of read operations. However, it's important to note that while indexes speed up read operations, they slow down write operations (such as INSERT and UPDATE) because the database needs to update the index every time a record is added or modified. Therefore, it's crucial to find a balance and only add indexes to columns that are frequently used in WHERE clauses or JOIN operations. model ProviderAccount {
id String @id @default(cuid())
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
provider IdentityProvider
providerAccountId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
access_token String? @db.Text
token_type String?
refresh_token String? @db.Text
expires_in Int?
id_token String? @db.Text
session_state String?
@@unique([provider, providerAccountId])
@@index([userId])
} |
What does this PR do?
chore: Improve database performance by adding indexes
Type of change