Best Practices
Optimize your Warehouse queries for performance, reliability, and maintainability.
Query Optimization
1. Select Only Needed Fields
Avoid using * in cross-database queries:
- TypeScript
- Java
- Go
- .NET
// ❌ Bad: Fetches all fields from all sources
fields: ['*']
// ✅ Good: Only fetch what you need
fields: ['u.id', 'u.name', 'o.total', 'o.status']
// ❌ Bad: Fetches all fields from all sources
fields: ['*']
// ✅ Good: Only fetch what you need
fields: ['u.id', 'u.name', 'o.total', 'o.status']
// ❌ Bad: Fetches all fields from all sources
fields: ['*']
// ✅ Good: Only fetch what you need
fields: ['u.id', 'u.name', 'o.total', 'o.status']
// ❌ Bad: Fetches all fields from all sources
fields: ['*']
// ✅ Good: Only fetch what you need
fields: ['u.id', 'u.name', 'o.total', 'o.status']
2. Filter Early
Apply filters on individual sources before joining:
- TypeScript
- Java
- Go
- .NET
// ✅ Good: Filter applied to joined source
join: [{
type: 'left',
source: { type: 'database', tag: 'orders-db', entity: 'orders', alias: 'o' },
on: { left: 'u.id', right: 'o.userId' },
where: { 'o.status': { $eq: 'completed' } } // Filter before join
}]
// Also filter primary source
where: { 'u.status': { $eq: 'active' } }
// ✅ Good: Filter applied to joined source
join: [Map.of(
"type", "left",
source: Map.of( "type", "database", "tag", "orders-db", "entity", "orders", "alias", "o" ),
on: Map.of( "left", "u.id", "right", "o.userId" ),
where: Map.of( 'o.status': Map.of( $"eq", "completed" ) ) // Filter before join
)]
// Also filter primary source
where: Map.of( 'u.status': Map.of( $"eq", "active" ) )
// ✅ Good: Filter applied to joined source
join: [{
"type": "left",
source: { "type": "database", "tag": "orders-db", "entity": "orders", "alias": "o" },
on: { "left": "u.id", "right": "o.userId" },
where: { 'o.status': { $"eq": "completed" } } // Filter before join
}]
// Also filter primary source
where: { 'u.status': { $"eq": "active" } }
// ✅ Good: Filter applied to joined source
join: [{
["type"] = "left",
source: { ["type"] = "database", ["tag"] = "orders-db", ["entity"] = "orders", ["alias"] = "o" },
on: { ["left"] = "u.id", ["right"] = "o.userId" },
where: { 'o.status': { $["eq"] = "completed" } } // Filter before join
}]
// Also filter primary source
where: { 'u.status': { $["eq"] = "active" } }
3. Use Appropriate Limits
Always use pagination for large datasets:
- TypeScript
- Java
- Go
- .NET
const result = await ductape.warehouse.query({
operation: 'select',
from: { type: 'database', tag: 'users-db', entity: 'users', alias: 'u' },
fields: ['u.id', 'u.name'],
limit: 50,
offset: page * 50
});
Map<String, Object> result = ductape.warehouse.query(Map.of(
"operation", "select",
from: Map.of( "type", "database", "tag", "users-db", "entity", "users", "alias", "u" ),
fields: ['u.id', 'u.name'],
"limit", 50,
offset: page * 50
));
result := client.warehouse.query({
"operation": "select",
from: { "type": "database", "tag": "users-db", "entity": "users", "alias": "u" },
fields: ['u.id', 'u.name'],
"limit": 50,
offset: page * 50
});
var result = await ductape.warehouse.query({
["operation"] = "select",
from: { ["type"] = "database", ["tag"] = "users-db", ["entity"] = "users", ["alias"] = "u" },
fields: ['u.id', 'u.name'],
["limit"] = 50,
offset: page * 50
});
4. Leverage Aliases
Use clear, consistent aliases for readability:
- TypeScript
- Java
- Go
- .NET
// ✅ Good: Clear aliases
from: { type: 'database', tag: 'users-postgres', entity: 'users', alias: 'u' },
join: [
{ source: { tag: 'orders-mongo', entity: 'orders', alias: 'o' } },
{ source: { tag: 'products-postgres', entity: 'products', alias: 'p' } }
]
// ✅ Good: Clear aliases
from: Map.of( "type", "database", "tag", "users-postgres", "entity", "users", "alias", "u" ),
join: [
Map.of( source: Map.of( "tag", "orders-mongo", "entity", "orders", "alias", "o" ) ),
Map.of( source: Map.of( "tag", "products-postgres", "entity", "products", "alias", "p" ) )
]
// ✅ Good: Clear aliases
from: { "type": "database", "tag": "users-postgres", "entity": "users", "alias": "u" },
join: [
{ source: { "tag": "orders-mongo", "entity": "orders", "alias": "o" } },
{ source: { "tag": "products-postgres", "entity": "products", "alias": "p" } }
]
// ✅ Good: Clear aliases
from: { ["type"] = "database", ["tag"] = "users-postgres", ["entity"] = "users", ["alias"] = "u" },
join: [
{ source: { ["tag"] = "orders-mongo", ["entity"] = "orders", ["alias"] = "o" } },
{ source: { ["tag"] = "products-postgres", ["entity"] = "products", ["alias"] = "p" } }
]
Join Performance
1. Put Smaller Dataset on Right
When joining, the Warehouse fetches the right side for each left record:
- TypeScript
- Java
- Go
- .NET
// ✅ If users is smaller than orders, use users as right
from: { type: 'database', tag: 'orders-db', entity: 'orders', alias: 'o' },
join: [{
type: 'inner',
source: { type: 'database', tag: 'users-db', entity: 'users', alias: 'u' },
on: { left: 'o.userId', right: 'u.id' }
}]
// ✅ If users is smaller than orders, use users as right
from: Map.of( "type", "database", "tag", "orders-db", "entity", "orders", "alias", "o" ),
join: [Map.of(
"type", "inner",
source: Map.of( "type", "database", "tag", "users-db", "entity", "users", "alias", "u" ),
on: Map.of( "left", "o.userId", "right", "u.id" )
)]
// ✅ If users is smaller than orders, use users as right
from: { "type": "database", "tag": "orders-db", "entity": "orders", "alias": "o" },
join: [{
"type": "inner",
source: { "type": "database", "tag": "users-db", "entity": "users", "alias": "u" },
on: { "left": "o.userId", "right": "u.id" }
}]
// ✅ If users is smaller than orders, use users as right
from: { ["type"] = "database", ["tag"] = "orders-db", ["entity"] = "orders", ["alias"] = "o" },
join: [{
["type"] = "inner",
source: { ["type"] = "database", ["tag"] = "users-db", ["entity"] = "users", ["alias"] = "u" },
on: { ["left"] = "o.userId", ["right"] = "u.id" }
}]
2. Limit Semantic Join Results
Control the number of vector matches:
- TypeScript
- Java
- Go
- .NET
semantic: {
embedField: 'description',
similarityThreshold: 0.8, // Higher threshold = fewer matches
topK: 5 // Limit matches per record
}
semantic: Map.of(
"embedField", "description",
"similarityThreshold", 0.8, // Higher threshold = fewer matches
"topK", 5 // Limit matches per record
)
semantic: {
"embedField": "description",
"similarityThreshold": 0.8, // Higher threshold = fewer matches
"topK": 5 // Limit matches per record
}
semantic: {
["embedField"] = "description",
["similarityThreshold"] = 0.8, // Higher threshold = fewer matches
["topK"] = 5 // Limit matches per record
}
3. Avoid Deep Graph Traversals
Keep graph depth minimal:
- TypeScript
- Java
- Go
- .NET
graph: {
relationship: 'FRIENDS_WITH',
direction: 'outgoing',
minDepth: 1,
maxDepth: 2 // Keep depth low
}
graph: Map.of(
"relationship", "FRIENDS_WITH",
"direction", "outgoing",
"minDepth", 1,
"maxDepth", 2 // Keep depth low
)
graph: {
"relationship": "FRIENDS_WITH",
"direction": "outgoing",
"minDepth": 1,
"maxDepth": 2 // Keep depth low
}
graph: {
["relationship"] = "FRIENDS_WITH",
["direction"] = "outgoing",
["minDepth"] = 1,
["maxDepth"] = 2 // Keep depth low
}
Transaction Design
1. Keep Transactions Small
Minimize the number of operations per transaction:
- TypeScript
- Java
- Go
- .NET
// ✅ Good: Essential operations only
await ductape.warehouse.transaction([
{ operation: 'insert', from: { tag: 'orders-db' }, data: order },
{ operation: 'update', from: { tag: 'inventory-db' }, data: stockUpdate, where: productFilter }
]);
// ❌ Bad: Too many unrelated operations
await ductape.warehouse.transaction([
{ /* order */ },
{ /* inventory */ },
{ /* audit log */ }, // Could be async
{ /* analytics */ }, // Could be async
{ /* notifications */ } // Could be async
]);
// ✅ Good: Essential operations only
ductape.warehouse.transaction([
Map.of( "operation", "insert", from: Map.of( "tag", "orders-db" ), data: order ),
Map.of( "operation", "update", from: Map.of( "tag", "inventory-db" ), data: stockUpdate, where: productFilter )
]);
// ❌ Bad: Too many unrelated operations
ductape.warehouse.transaction([
Map.of( /* order */ ),
Map.of( /* inventory */ ),
Map.of( /* audit log */ ), // Could be async
Map.of( /* analytics */ ), // Could be async
Map.of( /* notifications */ ) // Could be async
]);
// ✅ Good: Essential operations only
client.warehouse.transaction([
{ "operation": "insert", from: { "tag": "orders-db" }, data: order },
{ "operation": "update", from: { "tag": "inventory-db" }, data: stockUpdate, where: productFilter }
]);
// ❌ Bad: Too many unrelated operations
client.warehouse.transaction([
{ /* order */ },
{ /* inventory */ },
{ /* audit log */ }, // Could be async
{ /* analytics */ }, // Could be async
{ /* notifications */ } // Could be async
]);
// ✅ Good: Essential operations only
await ductape.warehouse.transaction([
{ ["operation"] = "insert", from: { ["tag"] = "orders-db" }, data: order },
{ ["operation"] = "update", from: { ["tag"] = "inventory-db" }, data: stockUpdate, where: productFilter }
]);
// ❌ Bad: Too many unrelated operations
await ductape.warehouse.transaction([
{ /* order */ },
{ /* inventory */ },
{ /* audit log */ }, // Could be async
{ /* analytics */ }, // Could be async
{ /* notifications */ } // Could be async
]);