Repository navigation
perf: add indexes to improve query speed - #1613
Conversation
WalkthroughThis update introduces a new database migration that enhances referential integrity and query performance for the "mail0" tables. It adds cascading delete behaviors to foreign keys, defines explicit indexes on frequently queried columns, and updates the schema definitions and migration snapshots to reflect these optimizations. Changes
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
PR Summary
Comprehensive database performance optimization through strategic index additions and foreign key modifications in the Mail-0/Zero schema.
- Added migration
apps/server/src/db/migrations/0035_uneven_shiva.sqlimplementing btree indexes for frequently queried columns like user_id and expiration timestamps - Modified foreign key constraints with CASCADE delete behavior in schema.ts to ensure proper cleanup of related records
- Added composite indexes for commonly joined fields to optimize query patterns
- Improved query performance for session management and OAuth operations through strategic index placement
4 files reviewed, 2 comments
Edit PR Review Bot Settings | Greptile
| "connectionId": { | ||
| "name": "connectionId", | ||
| "type": "text", | ||
| "primaryKey": false, | ||
| "notNull": true |
There was a problem hiding this comment.
style: Column name should be snake_case (connection_id) to maintain consistency with other tables
| "updatedAt": { | ||
| "name": "updatedAt", | ||
| "type": "timestamp", | ||
| "primaryKey": false, | ||
| "notNull": true, |
There was a problem hiding this comment.
style: Column name should be snake_case (updated_at) to maintain consistency with other tables
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
apps/server/src/db/migrations/0035_uneven_shiva.sql (1)
42-43: Consider GIN indexes for JSONB columnsWhile btree indexes on JSONB columns are valid for equality comparisons, consider using GIN indexes if you need to query inside the JSON structure (e.g., containment queries with
@>operator).Example:
CREATE INDEX "user_hotkeys_shortcuts_gin_idx" ON "mail0_user_hotkeys" USING gin ("shortcuts");Also applies to: 46-46
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
apps/server/src/db/migrations/0035_uneven_shiva.sql(1 hunks)apps/server/src/db/migrations/meta/0035_snapshot.json(1 hunks)apps/server/src/db/migrations/meta/_journal.json(1 hunks)apps/server/src/db/schema.ts(4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
apps/server/src/db/schema.ts (1)
apps/server/src/lib/schemas.ts (1)
defaultUserSettings(137-151)
🔇 Additional comments (5)
apps/server/src/db/migrations/meta/_journal.json (1)
257-263: LGTM!The new migration journal entry follows the established pattern and correctly tracks the performance optimization migration.
apps/server/src/db/migrations/0035_uneven_shiva.sql (2)
17-46: Excellent indexing strategy for query optimization!The indexes cover all the critical query patterns:
- Foreign key columns for JOIN operations
- Timestamp columns for time-based queries and cleanup operations
- Boolean flags for filtering
- Composite indexes for multi-column queries
11-16: Fix missing statement breakpoints between ALTER TABLE statementsThe ALTER TABLE statements are concatenated without proper statement breakpoints, which could cause SQL execution errors.
Apply this diff to add the missing statement breakpoints:
-ALTER TABLE "mail0_account" ADD CONSTRAINT "mail0_account_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint -ALTER TABLE "mail0_connection" ADD CONSTRAINT "mail0_connection_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint -ALTER TABLE "mail0_session" ADD CONSTRAINT "mail0_session_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint -ALTER TABLE "mail0_summary" ADD CONSTRAINT "mail0_summary_connection_id_mail0_connection_id_fk" FOREIGN KEY ("connection_id") REFERENCES "public"."mail0_connection"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint -ALTER TABLE "mail0_user_hotkeys" ADD CONSTRAINT "mail0_user_hotkeys_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint -ALTER TABLE "mail0_user_settings" ADD CONSTRAINT "mail0_user_settings_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "mail0_account" ADD CONSTRAINT "mail0_account_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action; +--> statement-breakpoint +ALTER TABLE "mail0_connection" ADD CONSTRAINT "mail0_connection_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action; +--> statement-breakpoint +ALTER TABLE "mail0_session" ADD CONSTRAINT "mail0_session_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action; +--> statement-breakpoint +ALTER TABLE "mail0_summary" ADD CONSTRAINT "mail0_summary_connection_id_mail0_connection_id_fk" FOREIGN KEY ("connection_id") REFERENCES "public"."mail0_connection"("id") ON DELETE cascade ON UPDATE no action; +--> statement-breakpoint +ALTER TABLE "mail0_user_hotkeys" ADD CONSTRAINT "mail0_user_hotkeys_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action; +--> statement-breakpoint +ALTER TABLE "mail0_user_settings" ADD CONSTRAINT "mail0_user_settings_user_id_mail0_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."mail0_user"("id") ON DELETE cascade ON UPDATE no action;Likely an incorrect or invalid review comment.
apps/server/src/db/migrations/meta/0035_snapshot.json (1)
1-1610: Schema snapshot correctly reflects the migration changesThe snapshot properly captures:
- All new indexes for query optimization
- Cascading delete behavior on foreign keys
- Complete table structures with appropriate constraints
apps/server/src/db/schema.ts (1)
10-10: Schema definitions properly updated with indexes and cascading deletesThe refactoring to explicit index definitions and cascading foreign keys is well-implemented:
- All indexes match the migration script
- Cascading deletes ensure referential integrity
- Index naming convention is consistent and descriptive
Also applies to: 30-303
Currently, a lot of relations in schema.ts are missing indexes which increases the query time in the db.
This PR addresses the problem and improves query speed.
Also, adds cascase
Type of Change
Please delete options that are not relevant.
Areas Affected
Please check all that apply:
Security Considerations
For changes involving data or authentication:
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the project's license.
Summary by CodeRabbit
New Features
Chores