Summary
For UNIQUE/PK indexes, the index key is built twice per prepared INSERT:
- Line 304: During constraint validation loop
- Line 368: During index insertion loop
Current Code
// Loop 1: Constraint validation (UNIQUE/PK indexes only)
for index_plan in &plan.indexes {
if !index_plan.is_unique && !index_plan.is_pk { continue; }
// ...
Self::build_index_key(index_plan, params); // BUILD #1
// ... constraint check ...
}
// Main table insert happens here...
// Loop 2: Index insertion (ALL indexes)
for index_plan in &plan.indexes {
// ...
Self::build_index_key(index_plan, params); // BUILD #2 (same key!)
// ... insert into index ...
}
Impact
- High - This happens on every prepared INSERT
- Wastes CPU cycles rebuilding the same key
- The key buffer is already in
index_plan.key_buffer
Potential Fix
Track which indexes have had keys built in loop 1, skip rebuilding in loop 2:
// After constraint validation, mark key as built
// In loop 2, check if key already built for this index
if !index_plan.key_already_built.get() {
Self::build_index_key(index_plan, params);
}
Or restructure to build all keys once before both loops.
Files
src/database/batch.rs:282-321, 358-380
Summary
For UNIQUE/PK indexes, the index key is built twice per prepared INSERT:
Current Code
Impact
index_plan.key_bufferPotential Fix
Track which indexes have had keys built in loop 1, skip rebuilding in loop 2:
Or restructure to build all keys once before both loops.
Files
src/database/batch.rs:282-321, 358-380