Skip to content

perf: Double key building for UNIQUE/PK indexes in prepared INSERT #17

@mjm918

Description

@mjm918

Summary

For UNIQUE/PK indexes, the index key is built twice per prepared INSERT:

  1. Line 304: During constraint validation loop
  2. 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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions