-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
⚡️ perf(database): missing user_id and user_memory_id index
#10643
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
⚡️ perf(database): missing user_id and user_memory_id index
#10643
Conversation
|
@nekomeowww is attempting to deploy a commit to the LobeHub OSS Team on Vercel. A member of the Team first needs to authorize it. |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds missing btree indexes on user_id and user_memory_id across user memories tables while removing two unused HNSW vector indexes, and wires the changes into the SQL and schema metadata migrations. Entity relationship diagram for updated user memories indexeserDiagram
user_memories {
int id
int user_id_indexed
vector summary_vector_1024
vector details_vector_1024
}
user_memories_contexts {
int id
int user_id_indexed
int user_memory_id
text type
vector description_vector
}
user_memories_preferences {
int id
int user_id_indexed
int user_memory_id_indexed
vector conclusion_directives_vector
}
user_memories_identities {
int id
int user_id_indexed
int user_memory_id_indexed
text type
vector description_vector
}
user_memories_experiences {
int id
int user_id_indexed
int user_memory_id_indexed
text type
vector key_learning_vector
}
user_memories ||--o{ user_memories_contexts : user_memory_id
user_memories ||--o{ user_memories_preferences : user_memory_id
user_memories ||--o{ user_memories_identities : user_memory_id
user_memories ||--o{ user_memories_experiences : user_memory_id
user_memories ||--o{ user_memories_contexts : user_id
user_memories ||--o{ user_memories_preferences : user_id
user_memories ||--o{ user_memories_identities : user_id
user_memories ||--o{ user_memories_experiences : user_id
Flow diagram for migration 0060 index changesflowchart TD
A[start_migration_0060] --> B[drop_index_user_memories_summary_vector_1024_index]
B --> C[drop_index_user_memories_details_vector_1024_index]
C --> D[create_index_user_memories_user_id_index_btree]
D --> E[create_index_user_memories_contexts_user_id_index_btree]
E --> F[create_index_user_memories_experiences_user_id_index_btree]
F --> G[create_index_user_memories_experiences_user_memory_id_index_btree]
G --> H[create_index_user_memories_identities_user_id_index_btree]
H --> I[create_index_user_memories_identities_user_memory_id_index_btree]
I --> J[create_index_user_memories_preferences_user_id_index_btree]
J --> K[create_index_user_memories_preferences_user_memory_id_index_btree]
K --> L[end_migration_0060]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
TestGru AssignmentSummary
Tip You can |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- The migration drops
user_memories_*_vector_1024_indexwithoutIF EXISTS; to make repeated or partially-applied migrations safer, consider usingDROP INDEX IF EXISTSfor these two vector indexes as well. - Given you now have separate
user_idandtypeindexes on several tables, if common queries filter on both fields together it may be worth changing these to composite indexes (e.g.(user_id, type)) to better match the access patterns. - By fully removing the HNSW vector indexes from
user_memories, any remaining vector similarity queries on that table will fall back to sequential scans; if those queries are still in use, consider whether a different indexing strategy or relocation of vector search is needed rather than a full removal.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The migration drops `user_memories_*_vector_1024_index` without `IF EXISTS`; to make repeated or partially-applied migrations safer, consider using `DROP INDEX IF EXISTS` for these two vector indexes as well.
- Given you now have separate `user_id` and `type` indexes on several tables, if common queries filter on both fields together it may be worth changing these to composite indexes (e.g. `(user_id, type)`) to better match the access patterns.
- By fully removing the HNSW vector indexes from `user_memories`, any remaining vector similarity queries on that table will fall back to sequential scans; if those queries are still in use, consider whether a different indexing strategy or relocation of vector search is needed rather than a full removal.
## Individual Comments
### Comment 1
<location> `packages/database/migrations/0060_add_user_memory_user_id_index.sql:1-2` </location>
<code_context>
+DROP INDEX "user_memories_summary_vector_1024_index";--> statement-breakpoint
+DROP INDEX "user_memories_details_vector_1024_index";--> statement-breakpoint
+CREATE INDEX IF NOT EXISTS "user_memories_user_id_index" ON "user_memories" USING btree ("user_id");--> statement-breakpoint
+CREATE INDEX IF NOT EXISTS "user_memories_contexts_user_id_index" ON "user_memories_contexts" USING btree ("user_id");--> statement-breakpoint
</code_context>
<issue_to_address>
**issue:** Use `IF EXISTS` for `DROP INDEX` to make the migration more robust across environments.
These `DROP INDEX` statements will error if the indexes were never created or were removed manually in some environments, causing the migration to fail. Using `DROP INDEX IF EXISTS` keeps the migration idempotent and safe on partially migrated or manually modified databases.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| DROP INDEX "user_memories_summary_vector_1024_index";--> statement-breakpoint | ||
| DROP INDEX "user_memories_details_vector_1024_index";--> statement-breakpoint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Use IF EXISTS for DROP INDEX to make the migration more robust across environments.
These DROP INDEX statements will error if the indexes were never created or were removed manually in some environments, causing the migration to fail. Using DROP INDEX IF EXISTS keeps the migration idempotent and safe on partially migrated or manually modified databases.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #10643 +/- ##
=========================================
Coverage 80.60% 80.60%
=========================================
Files 977 977
Lines 66579 66579
Branches 10266 8802 -1464
=========================================
Hits 53668 53668
Misses 12911 12911
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
💻 Change Type
🔗 Related Issue
🔀 Description of Change
🧪 How to Test
📸 Screenshots / Videos
📝 Additional Information
Summary by Sourcery
Optimize user memory-related database queries by adjusting indexes on user and user_memory identifiers.
Enhancements: