motedb 0.9.0

AI-native embedded multimodal database for embodied intelligence (robots, AR glasses, industrial arms).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! SQL Query Benchmark — GROUP BY, HAVING, DISTINCT, ORDER BY, LIMIT/OFFSET,
//! LIKE, BETWEEN, IN, subquery, arithmetic expressions, complex WHERE
//!
//! Run: cargo test --test bench_sql_queries --release -- --nocapture --test-threads=1

use motedb::{DBConfig, Database};
use std::time::Instant;
use tempfile::TempDir;

fn is_ci() -> bool {
    std::env::var("CI").is_ok()
}

fn edge_config() -> DBConfig {
    DBConfig::for_edge()
}

fn create_db() -> (Database, TempDir) {
    let dir = TempDir::new().expect("temp dir");
    let db = Database::create_with_config(dir.path(), edge_config()).expect("create db");
    (db, dir)
}

fn exec(db: &Database, sql: &str) -> motedb::sql::QueryResult {
    db.execute(sql)
        .expect("execute SQL")
        .materialize()
        .expect("materialize")
}

fn print_result(name: &str, ops: usize, elapsed_ms: u64) {
    let per_op_us = if ops > 0 {
        (elapsed_ms as f64 * 1000.0) / ops as f64
    } else {
        0.0
    };
    let throughput = if elapsed_ms > 0 {
        ops as f64 / (elapsed_ms as f64 / 1000.0)
    } else {
        f64::INFINITY
    };
    println!(
        "  {:<60} | {:>7} ops | {:>8.1} ms | {:>8.1} µs/op | {:>10.0} ops/s",
        name, ops, elapsed_ms as f64, per_op_us, throughput
    );
}

fn print_separator() {
    println!("  {}", "-".repeat(100));
}

/// Seed a sales table with n rows: id, customer, product, amount, qty, region, ts
fn seed_sales(db: &Database, n: usize) {
    exec(db, "CREATE TABLE sales (id INT PRIMARY KEY, customer TEXT, product TEXT, amount FLOAT, qty INT, region TEXT, ts INT)");

    let customers = ["Alice", "Bob", "Charlie", "Diana", "Eve"];
    let products = [
        "Widget",
        "Gadget",
        "Doohickey",
        "Thingamajig",
        "Whatchamacallit",
    ];
    let regions = ["US", "EU", "APAC", "LATAM"];

    for i in 1..=n as i64 {
        let c = customers[(i as usize) % customers.len()];
        let p = products[(i as usize) % products.len()];
        let r = regions[(i as usize) % regions.len()];
        let amount = 10.0 + (i as f64 % 990.0);
        let qty = 1 + (i % 100);
        let ts = 1700000000 + i * 3600;
        exec(
            db,
            &format!(
                "INSERT INTO sales VALUES ({}, '{}', '{}', {:.1}, {}, '{}', {})",
                i, c, p, amount, qty, r, ts
            ),
        );
    }
}

// ═══════════════════════════════════════════════════════════════
// Test 1: GROUP BY + Aggregate Performance
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_group_by_aggregates() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 5_000 } else { 30_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 20 } else { 100 };

    // GROUP BY single column
    let gb1_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT customer, COUNT(*) FROM sales GROUP BY customer",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("GROUP BY customer × {}", q), q, gb1_ms);

    // GROUP BY with SUM/AVG/MIN/MAX
    let gb2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT customer, SUM(amount), AVG(qty), MIN(amount), MAX(amount) FROM sales GROUP BY customer");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("GROUP BY + 4 aggregates × {}", q), q, gb2_ms);

    // GROUP BY two columns
    let gb3_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT customer, product, SUM(amount) FROM sales GROUP BY customer, product",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("GROUP BY (2 cols) × {}", q), q, gb3_ms);

    let gb1_per = gb1_ms as f64 * 1000.0 / q as f64;
    let gb3_per = gb3_ms as f64 * 1000.0 / q as f64;
    println!(
        "  -> Single-col: {:.1}µs, Multi-col: {:.1}µs",
        gb1_per, gb3_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 2: HAVING Clause Performance
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_having() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 5_000 } else { 30_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 20 } else { 100 };

    // HAVING with COUNT
    let h1_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT customer, COUNT(*) FROM sales GROUP BY customer HAVING COUNT(*) > 100",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("GROUP BY + HAVING COUNT × {}", q), q, h1_ms);

    // HAVING with SUM
    let h2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT customer, SUM(amount) FROM sales GROUP BY customer HAVING SUM(amount) > 5000");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("GROUP BY + HAVING SUM × {}", q), q, h2_ms);

    // GROUP BY + WHERE + HAVING
    let h3_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT customer, SUM(amount) FROM sales WHERE qty > 10 GROUP BY customer HAVING SUM(amount) > 1000");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE + GROUP BY + HAVING × {}", q), q, h3_ms);

    let h1_per = h1_ms as f64 * 1000.0 / q as f64;
    let h3_per = h3_ms as f64 * 1000.0 / q as f64;
    println!(
        "  -> HAVING only: {:.1}µs, WHERE+HAVING: {:.1}µs",
        h1_per, h3_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 3: ORDER BY + LIMIT/OFFSET
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_order_by_limit() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 5_000 } else { 30_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 50 } else { 200 };

    // ORDER BY ASC
    let ob1_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id, amount FROM sales ORDER BY amount ASC");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("ORDER BY amount ASC × {}", q), q, ob1_ms);

    // ORDER BY DESC
    let ob2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id, amount FROM sales ORDER BY amount DESC");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("ORDER BY amount DESC × {}", q), q, ob2_ms);

    // ORDER BY + LIMIT
    let lim_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT id, amount FROM sales ORDER BY amount DESC LIMIT 10",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("ORDER BY DESC + LIMIT 10 × {}", q), q, lim_ms);

    // ORDER BY + LIMIT + OFFSET (pagination)
    let page_ms = {
        let start = Instant::now();
        for p in 0..q {
            let offset = p * 10;
            exec(
                &db,
                &format!(
                    "SELECT id, amount FROM sales ORDER BY id LIMIT 10 OFFSET {}",
                    offset
                ),
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("ORDER BY + LIMIT 10 + OFFSET × {}", q), q, page_ms);

    let ob_per = ob1_ms as f64 * 1000.0 / q as f64;
    let lim_per = lim_ms as f64 * 1000.0 / q as f64;
    println!(
        "  -> Full sort: {:.1}µs, Sort+LIMIT: {:.1}µs",
        ob_per, lim_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 4: DISTINCT Performance
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_distinct() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 5_000 } else { 30_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 50 } else { 200 };

    // DISTINCT single column
    let d1_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT DISTINCT customer FROM sales");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("DISTINCT customer × {}", q), q, d1_ms);

    // DISTINCT two columns
    let d2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT DISTINCT customer, product FROM sales");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("DISTINCT (2 cols) × {}", q), q, d2_ms);

    // DISTINCT three columns
    let d3_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT DISTINCT customer, product, region FROM sales");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("DISTINCT (3 cols) × {}", q), q, d3_ms);

    let d1_per = d1_ms as f64 * 1000.0 / q as f64;
    let d3_per = d3_ms as f64 * 1000.0 / q as f64;
    println!("  -> 1-col: {:.1}µs, 3-col: {:.1}µs", d1_per, d3_per);
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 5: WHERE Clause Patterns (LIKE, BETWEEN, IN)
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_where_patterns() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 5_000 } else { 30_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 50 } else { 200 };

    // LIKE with prefix
    let like_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM sales WHERE customer LIKE 'A%'");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE LIKE 'A%' × {}", q), q, like_ms);

    // LIKE with suffix
    let like2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM sales WHERE product LIKE '%et'");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE LIKE '%et' × {}", q), q, like2_ms);

    // BETWEEN
    let between_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM sales WHERE amount BETWEEN 100 AND 500");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE BETWEEN × {}", q), q, between_ms);

    // IN (list)
    let in_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT id FROM sales WHERE customer IN ('Alice', 'Bob', 'Charlie')",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE IN (3 vals) × {}", q), q, in_ms);

    // Compound WHERE: AND + OR
    let compound_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM sales WHERE (customer = 'Alice' AND qty > 50) OR (region = 'EU' AND amount > 200)");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE compound (AND+OR) × {}", q), q, compound_ms);

    let like_per = like_ms as f64 * 1000.0 / q as f64;
    let between_per = between_ms as f64 * 1000.0 / q as f64;
    let in_per = in_ms as f64 * 1000.0 / q as f64;
    println!(
        "  -> LIKE: {:.1}µs, BETWEEN: {:.1}µs, IN: {:.1}µs",
        like_per, between_per, in_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 6: Arithmetic Expressions in SELECT/WHERE
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_arithmetic_expressions() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 5_000 } else { 30_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 50 } else { 200 };

    // Computed column: amount * qty
    let expr1_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id, amount * qty FROM sales");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("SELECT amount * qty × {}", q), q, expr1_ms);

    // Nested: (amount + 10) * qty / 2
    let expr2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id, (amount + 10) * qty / 2 FROM sales");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("SELECT complex expr × {}", q), q, expr2_ms);

    // WHERE with expression
    let expr3_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM sales WHERE amount * qty > 5000");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE amount * qty > 5000 × {}", q), q, expr3_ms);

    // SELECT with scalar functions on columns
    let func_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT id, ROUND(amount), ABS(qty - 50), LOWER(customer) FROM sales",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("SELECT with scalar functions × {}", q), q, func_ms);

    let expr1_per = expr1_ms as f64 * 1000.0 / q as f64;
    let func_per = func_ms as f64 * 1000.0 / q as f64;
    println!(
        "  -> Expr: {:.1}µs, Func+Expr: {:.1}µs",
        expr1_per, func_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 7: Subquery Performance
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_subquery() {
    let (db, _dir) = create_db();
    let n: usize = if is_ci() { 3_000 } else { 20_000 };
    seed_sales(&db, n);

    print_separator();

    let q = if is_ci() { 10 } else { 50 };

    // Subquery in WHERE
    let sub1_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(
                &db,
                "SELECT id FROM sales WHERE amount > (SELECT AVG(amount) FROM sales)",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE > subquery (AVG) × {}", q), q, sub1_ms);

    // IN subquery
    let sub2_ms = {
        let start = Instant::now();
        for _ in 0..q {
            match db.execute("SELECT id FROM sales WHERE customer IN (SELECT customer FROM sales WHERE region = 'US')") {
                Ok(r) => { let _ = r.materialize(); }
                Err(_) => {}
            }
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE IN subquery × {}", q), q, sub2_ms);

    let sub1_per = sub1_ms as f64 * 1000.0 / q as f64;
    println!("  -> Scalar subquery: {:.1}µs/op", sub1_per);
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 8: NULL Handling Performance
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_null_handling() {
    let (db, _dir) = create_db();
    exec(
        &db,
        "CREATE TABLE nullable (id INT PRIMARY KEY, val INT, name TEXT)",
    );

    let n: usize = if is_ci() { 5_000 } else { 30_000 };

    // 50% NULLs
    for i in 1..=n as i64 {
        if i % 2 == 0 {
            exec(
                &db,
                &format!("INSERT INTO nullable VALUES ({}, NULL, NULL)", i),
            );
        } else {
            exec(
                &db,
                &format!(
                    "INSERT INTO nullable VALUES ({}, {}, 'name_{}')",
                    i,
                    i * 10,
                    i
                ),
            );
        }
    }

    print_separator();

    let q = if is_ci() { 50 } else { 200 };

    // IS NULL
    let is_null_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM nullable WHERE val IS NULL");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE IS NULL × {}", q), q, is_null_ms);

    // IS NOT NULL
    let not_null_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id FROM nullable WHERE val IS NOT NULL");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("WHERE IS NOT NULL × {}", q), q, not_null_ms);

    // COALESCE
    let coal_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id, COALESCE(val, 0) FROM nullable");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("SELECT COALESCE × {}", q), q, coal_ms);

    let is_null_per = is_null_ms as f64 * 1000.0 / q as f64;
    let coal_per = coal_ms as f64 * 1000.0 / q as f64;
    println!(
        "  -> IS NULL: {:.1}µs, COALESCE: {:.1}µs",
        is_null_per, coal_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 9: Prepared Statement Throughput
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_prepared_statements() {
    use motedb::types::Value;

    let (db, _dir) = create_db();
    exec(
        &db,
        "CREATE TABLE prep (id INT PRIMARY KEY, name TEXT, score FLOAT)",
    );

    let n: usize = if is_ci() { 5_000 } else { 30_000 };

    // Seed via prepared insert
    let insert_sql = "INSERT INTO prep VALUES (?, ?, ?)";
    for i in 1..=n as i64 {
        db.execute_prepared(
            insert_sql,
            vec![
                Value::Integer(i),
                Value::text(format!("user_{}", i)),
                Value::Float(i as f64 * 1.5),
            ],
        )
        .expect("prepared insert");
    }

    print_separator();

    let q = if is_ci() { 1_000 } else { 5_000 };

    // Prepared SELECT by PK
    let sel_sql = "SELECT * FROM prep WHERE id = ?";
    let sel_ms = {
        let start = Instant::now();
        for i in 1..=q as i64 {
            db.execute_prepared(sel_sql, vec![Value::Integer(i)])
                .expect("prepared select");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("Prepared SELECT by PK × {}", q), q, sel_ms);

    // Prepared UPDATE
    let upd_sql = "UPDATE prep SET score = ? WHERE id = ?";
    let upd_count = q / 3;
    let upd_ms = {
        let start = Instant::now();
        for i in 1..=upd_count as i64 {
            db.execute_prepared(
                upd_sql,
                vec![Value::Float(i as f64 * 2.0), Value::Integer(i)],
            )
            .expect("prepared update");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(
        &format!("Prepared UPDATE × {}", upd_count),
        upd_count,
        upd_ms,
    );

    // Prepared DELETE + re-insert cycle
    let del_sql = "DELETE FROM prep WHERE id = ?";
    let cycle_count = if is_ci() { 200 } else { 1000 };
    let cycle_ms = {
        let start = Instant::now();
        for i in 1..=cycle_count as i64 {
            db.execute_prepared(del_sql, vec![Value::Integer(i)])
                .expect("prepared delete");
            db.execute_prepared(
                insert_sql,
                vec![
                    Value::Integer(i),
                    Value::text(format!("re_{}", i)),
                    Value::Float(i as f64),
                ],
            )
            .expect("prepared re-insert");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(
        &format!("Prepared DELETE+INSERT cycle × {}", cycle_count),
        cycle_count * 2,
        cycle_ms,
    );

    let sel_per = sel_ms as f64 * 1000.0 / q as f64;
    let upd_per = upd_ms as f64 * 1000.0 / upd_count as f64;
    println!(
        "  -> SELECT: {:.1}µs/op, UPDATE: {:.1}µs/op",
        sel_per, upd_per
    );
    db.close().ok();
}

// ═══════════════════════════════════════════════════════════════
// Test 10: Multi-table Queries
// ═══════════════════════════════════════════════════════════════

#[test]
#[ignore = "bench/stress/perf: slow in debug, run with --ignored or via bench examples"]
fn bench_multi_table() {
    let (db, _dir) = create_db();

    let n: usize = if is_ci() { 3_000 } else { 15_000 };

    exec(
        &db,
        "CREATE TABLE users (id INT PRIMARY KEY, name TEXT, region TEXT)",
    );
    exec(
        &db,
        "CREATE TABLE orders (id INT PRIMARY KEY, user_id INT, amount FLOAT, product TEXT)",
    );

    for i in 1..=n as i64 {
        let region = match i % 3 {
            0 => "US",
            1 => "EU",
            _ => "APAC",
        };
        exec(
            &db,
            &format!(
                "INSERT INTO users VALUES ({}, 'user_{}', '{}')",
                i, i, region
            ),
        );
        exec(
            &db,
            &format!(
                "INSERT INTO orders VALUES ({}, {}, {:.1}, 'prod_{}')",
                i,
                i,
                10.0 + (i as f64 % 990.0),
                i % 5
            ),
        );
    }

    print_separator();

    let q = if is_ci() { 20 } else { 100 };

    // Single table filtered scan (baseline)
    let single_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT id, amount FROM orders WHERE amount > 500");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("Single table scan × {}", q), q, single_ms);

    // COUNT on each table
    let count_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT COUNT(*) FROM users");
            exec(&db, "SELECT COUNT(*) FROM orders");
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("COUNT(*) × 2 tables × {}", q), q * 2, count_ms);

    // Aggregate per table
    let agg_ms = {
        let start = Instant::now();
        for _ in 0..q {
            exec(&db, "SELECT region, COUNT(*) FROM users GROUP BY region");
            exec(
                &db,
                "SELECT product, SUM(amount) FROM orders GROUP BY product",
            );
        }
        start.elapsed().as_millis() as u64
    };
    print_result(&format!("GROUP BY × 2 tables × {}", q), q * 2, agg_ms);

    let single_per = single_ms as f64 * 1000.0 / q as f64;
    println!("  -> Single table scan: {:.1}µs/op", single_per);
    db.close().ok();
}