Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Optimization of Tags for Emails
Problem
The loading of emails is slow because it requires fetching tags for each email in a workspace. This problem is particularly pronounced when the number of emails exceeds 15,000.
Proposed Solution
Approach
id
,label
, andcompanyId
.tags
collection.Collection Structure
Tag Collection Schema
Email Collection Schema
Query Optimization Using Aggregation and Joins
tags
collection.Update Tag Operations
tags
collection.emails
collection are updated accordingly.emails
collection are consistent.Migration Script
Since we have more than 30,000 emails and more than 5,000 tags, we need to be careful with the migration script to update all the old data with the new method.
Extract Unique Tags:
companyId
.Insert Unique Tags into Tags Collection:
tags
collection.companyId
to tag_id
.Update Mailings with Tag References:
_id
using the mapping.Ensure Data Integrity:
By following these instructions and carefully planning each step, you can ensure a smooth and efficient migration process while maintaining data integrity and minimizing downtime.
Advantages of This Approach
Reduction in Number of Documents Scanned:
Optimized Queries:
Better Scalability:
tags
collection, which is smaller and easier to manage.Conclusion
By reorganizing the data structure and using aggregation and joins techniques, you can significantly improve the performance of tag filters and avoid the slowdown caused by scanning each email individually.