Index Management
Optimize query performance with indexes.
Creating Indexes
Indexes support efficient execution of queries. Without indexes, Tubox must perform a collection scan.
Single Field Index
# Create standard index on "email"
name = await collection.create_index(
field="email",
unique=True
)
print(f"Created index: {name}")
Compound Index
# Index on status (asc) and created_at (desc)
await collection.create_compound_index([
("status", 1),
("created_at", -1)
])
Index Types & Options
| Type / Option | Description |
|---|---|
| unique=True | Enforces uniqueness. Duplicate values will raise an error. |
| sparse=True | Only indexes documents that contain the indexed field. |
| ttl_seconds=3600 | Automatically deletes documents after the specified seconds. |
TTL Index Example
# Documents expire 1 hour after "created_at"
await collection.create_index(
field="created_at",
ttl_seconds=3600
)
Management API
list_indexes() -> Dict[str, Dict]
Returns all indexes on the collection.
drop_index(index_name: str) -> bool
Removes an index.
rebuild_index(index_name: str) -> bool
Rebuilds index from scratch. Useful if index is corrupted or after bulk load.
analyze_indexes() -> Dict
Get usage statistics and optimization recommendations.
Last updated: Dec 14, 2025