Performance
Performance
Section titled “Performance”Pie provides multiple strategies to help you optimize MongoDB database operation performance.
1. Proper Index Usage
Section titled “1. Proper Index Usage”Ensure appropriate indexes are created on fields frequently used in Where, OrderBy, and Lookup operations.
type User struct { ID bson.ObjectID `bson:"_id,omitempty"` Email string `bson:"email" pie:"index:,unique"` // Unique index Age int `bson:"age" pie:"index:age_idx"` // Regular index CreatedAt time.Time `bson:"created_at" pie:"index:created_at_desc,order=-1"` // Descending index}
// Enable automatic index creationengine.WithAutoIndex(true)2. Bulk Operations
Section titled “2. Bulk Operations”For large insert, update, or delete operations, use bulk writes to significantly reduce network round trips and improve performance.
bulkWrite := pie.NewBulkWrite[User](engine).CollectionForStruct(User{})for _, user := range users { bulkWrite.InsertOne(user)}_, err := bulkWrite.ExecuteOrdered(ctx)3. Caching
Section titled “3. Caching”Leverage Pie’s built-in caching mechanism (memory cache or Redis cache) to cache infrequently changing but frequently read data.
// Enable Redis cachecfg := &pie.RedisCacheConfig{ Addr: "localhost:6379", Password: "", DB: 0, PoolSize: 10,}redisCache, err := pie.NewRedisCache(cfg)if err != nil { log.Fatal(err)}engine.UseCache(redisCache)
// Use cache in queriesusers, err := session.WithCache(5 * time.Minute).Cache("active_users").Find(ctx)4. Projection
Section titled “4. Projection”Only query the fields you need, avoiding unnecessary data transmission.
type UserProjection struct { Name string `bson:"name"` Email string `bson:"email"`}
users, err := session.Project("name", "email").Find(ctx)5. Limit and Skip
Section titled “5. Limit and Skip”In pagination queries, avoid using large Skip values as they cause MongoDB to scan many documents. Consider using cursors or range-based pagination.
// Avoid large Skip// session.Skip(100000).Limit(10).Find(ctx)
// Use cursorcursor, err := session.Where("status", "active").FindCursor(ctx)// ... process cursor ...6. Aggregation Pipeline Optimization
Section titled “6. Aggregation Pipeline Optimization”In aggregation queries, try to place $match stages early in the pipeline to filter out unnecessary documents as soon as possible.
aggregate := pie.NewAggregate[User](engine).CollectionForStruct(User{})result, err := aggregate. MatchStage().Where("status", "active").Done(). // Filter first GroupStage().By("role", "$role").Count("total").Done(). Exec(ctx)7. Connection Pool Configuration
Section titled “7. Connection Pool Configuration”Properly configure MongoDB client connection pool size to avoid too many or too few connections.
engine.WithMaxPoolSize(100).WithMinPoolSize(10)8. Query Logging and Monitoring
Section titled “8. Query Logging and Monitoring”Enable query logging and regularly analyze query performance.
engine.OnQuery(func(event *pie.QueryEvent) { log.Printf("Query: %s, Duration: %v", event.Query, event.Duration)})Next Steps
Section titled “Next Steps”- Best Practices - Learn development best practices
- Configuration - Learn about configuration options