18
Indexes & PerformanceSingle-Field Indexes
Create an index on a single field, understand when SapixDB uses it automatically.
Prerequisite: Lesson 11 complete
What you'll learn
- ✓POST /v1/agents/:id/indexes with name and field
- ✓GET /v1/agents/:id/indexes — list and check build status
- ✓Index used automatically for eq, in, starts_with on indexed field
- ✓O(n) full scan vs O(k) index scan
- ✓When to index: high-cardinality fields with frequent eq filters
Challenge
Create index on email for users agent. Query by exact email. Verify with explain (Lesson 21) that index was used.
Interactive Walkthrough
What you'll learn
Create an index on a single field, understand when SapixDB uses it, and verify with explain.
Without an index
Without an index, every filtered query does a full strand scan — O(n). Fine for small strands. Slow at 100k+ records.
Create a single-field index
curl -s -X POST http://localhost:7475/v1/agents/users/indexes \
-H "Content-Type: application/json" \
-H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
-d '{
"name": "idx_plan",
"field": "plan"
}' | python3 -m json.toolThe index is built asynchronously in the background. Status:
curl -s http://localhost:7475/v1/agents/users/indexes \
-H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
| python3 -m json.toolQuery using the index (automatic)
# SapixDB automatically uses idx_plan for eq/in/starts_with filters on 'plan'
curl -s -X POST http://localhost:7475/v1/agents/users/query \
-H "Content-Type: application/json" \
-H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
-d '{"type": "scan", "limit": 50, "filter": {"field": "plan", "op": "eq", "value": "pro"}}' \
| python3 -m json.toolYou do not name the index in the query — the engine picks it automatically when it sees a filter on an indexed field.
Challenge
Create an index on email for the users agent. Write 50 users. Query by exact email match and verify with explain (Lesson 21) that the index was used.
---