Include() method. This prevents N+1 query problems and efficiently loads related data.
Eager vs Lazy Loading
Eager Loading
Load relations alongside the main query using
Include()Lazy Loading
Without
Include(), relations are not fetched automaticallyInclude (Eager Loading)
Load related entities alongside the main query:Design choice: ChameleonDB uses separate queries for eager loading (not JOINs) to avoid row duplication and keep results clean.
Why Separate Queries?
ChameleonDB uses separate queries instead of JOINs for eager loading because:- No row duplication - JOINs duplicate parent rows for each child
- Cleaner results - Each entity appears exactly once
- Better performance - For one-to-many relations, separate queries can be faster
- Simpler result mapping - No need to deduplicate or group rows
Nested Include
Load relations multiple levels deep:IdentityMap Deduplication
ChameleonDB automatically deduplicates objects in memory using an IdentityMap:How IdentityMap Works
- When loading related data, ChameleonDB tracks entities by their primary key
- If the same entity appears multiple times, only one instance is kept in memory
- All references point to the same object
- This dramatically reduces memory usage for large result sets
Performance: IdentityMap is enabled by default and requires no configuration. It’s especially beneficial when loading many-to-one or many-to-many relationships.
Query with Relations
Example using the Go SDK:Filter on Related Entity
Filter the main entity based on a condition on a related entity. This is different from filtering the included results:Filter + Include Combined
You can filter on a relation and also include it. The filter affects which users are returned; the include loads all their orders (not just matching ones):Key distinction:
- The filter determines which users have at least one order > 100
- The include loads all orders for those users (including orders ≤ 100)