tursodatabase / tursodatabase/libsql

libsql is extremely slow inserting data into a local file

Open
#1,850 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
17.2k
Forks
531
Avg merge
1h 12m
Merged PRs (30d)
1

Description

In comparing libsql to better-sqlite3 I wrote the following simple test code:

import DB from "libsql";
import DB2 from 'better-sqlite3';

async function run(testName, db, numRecs) {
  console.log('Running', testName)

  await db.exec('DROP TABLE IF EXISTS test');
  await db.exec('CREATE TABLE IF NOT EXISTS test (a INT, b INT, c INT)');
  await db.pragma('journal_mode = WAL');
  var stmt = await db.prepare('INSERT INTO test(a,b,c) VALUES(?,?,?)');
  var start = new Date();

  for (var i = 1; i < numRecs + 1; i++) {
    await stmt.run(i, i + 1, i + 2);

    if ( !(i % 1000) )
      console.log('writing:', i);
  }

  var end = new Date();
  console.log(`DONE in ${end - start}msec`);
}

const numRecs = 10000;
const dbOpts = {};

var db = new DB('libsql.db', dbOpts);
await run('libsql', db, numRecs);
await db.close();
      
console.log();

var db = new DB2('better-sqlite3.db', dbOpts);
await run('better-sqlite3', db, numRecs);
await db.close();

The results are:

Running libsql
writing: 1000
. . .
writing: 10000
DONE in 23322msec

Running better-sqlite3
writing: 1000
. . .
writing: 10000
DONE in 400msec

libsql is ~60x slower.

To verify if the issue is in the node wrapper, I put together the same test in rust code:

use libsql::Builder;
use std::time::Instant;

#[tokio::main]
async fn main() {
  let db = Builder::new_local("test.db").build().await.unwrap();
  let conn = db.connect().unwrap();

  conn.execute("CREATE TABLE IF NOT EXISTS test (a INT, b INT, c INT)", ()).await.unwrap();
  let _ = conn.execute("pragma journal_mode = WAL", ()).await;
  let mut stmt = conn.prepare("INSERT INTO test(a,b,c) VALUES(?,?,?)").await.unwrap();
  let now = Instant::now();
  let num_recs = 10000;

  for i in 1..num_recs + 1 {
    stmt.execute([i, i + 1, i + 2]).await.unwrap();
    if i % 1000 == 0 {
      println!("writing {i}");
    }
  }

  println!("Elapsed: {:.2?}", now.elapsed());
}

The results are:

cargo run --release
Finished `release` profile [optimized] target(s) in 0.27s
Running `target\release\test.exe`
writing 1000
. . .
writing 10000
Elapsed: 23.47s

Note that I'm not a rust coder, but the results are exactly the same, indicating that something is seriously wrong with the rust library itself.

These tests were done on windows, however I also ran the node tests in WSL (libsql is ~130x slower) and in a vbox linux VM (libsql ~50X slower).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Node.js and Rust benchmarks against local database files on Windows or Linux, including the WAL setup and repeated prepared-statement execution. Compare the per-insert behavior with better-sqlite3 and determine the source of the large gap; done means the cause is identified and the benchmark shows substantially improved comparable timings.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, rust, sqlite
Domain
databases, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.