tauri-apps / tauri-apps/plugins-workspace

[sql] Insert and select operations are slow (SQLite)

Open
#2,049 3 comments 0 reactions 0 assignees View on GitHub
plugin: sql type: bug
Dominant language
Rust
Stars
1.8k
Forks
602
Avg merge
4d 14h
Merged PRs (30d)
9

Description

Tauri - `@tauri-apps/plugin-sql`

![M4c5LFtRIV](https://github.com/user-attachments/assets/b78ff7c6-af88-40bd-b721-c7f906732ab0)

Source Code

```ts
import SQL from "@tauri-apps/plugin-sql";

function bulkInsert(table: string, cols: string[], vals: unknown[][]): [string, unknown[]] {
const values = vals.flat();
const def = [];
let x = 1;
for (let i = 0; i < vals.length; i++) {
const f = [];
for (let j = 0; j < cols.length; j++) {
f[j] = "$" + x;
x++;
}
def[i] = "(" + f.join(",") + ")";
}
return [`INSERT INTO ${table} (${cols.join(",")}) VALUES ${def.join(",")}`, values];
}

console.time("load database");
const db = await SQL.load("sqlite:test.sqlite");
console.timeEnd("load database");

console.time("create table");
await db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, avatar BLOB)");
console.timeEnd("create table");

const [statement, values] = bulkInsert("users", ["name", "avatar"], new Array(100).fill([ "John Doe", new Uint8Array(10000) ]));

console.time("insert 100 rows");
await db.execute(statement, values);
console.timeEnd("insert 100 rows");

console.time("select 100 rows");
const rows = await db.select("SELECT * FROM users LIMIT 100") as any[];
console.timeEnd("select 100 rows");

console.time("parse rows");
rows.map(row => ({ ...row, avatar: new Uint8Array(JSON.parse(row.avatar)) }));
console.timeEnd("parse rows");
```

Bun - `bun:sqlite`

![dEb4sa3WoW](https://github.com/user-attachments/assets/c1393db6-57dc-4513-856f-4a95b4257ff0)

Source Code

```ts
import { Database } from "bun:sqlite";

function bulkInsert(table: string, cols: string[], vals: unknown[][]): [string, unknown[]] {
const values = vals.flat();
const def = [];
let x = 1;
for (let i = 0; i < vals.length; i++) {
const f = [];
for (let j = 0; j < cols.length; j++) {
f[j] = "$" + x;
x++;
}
def[i] = "(" + f.join(",") + ")";
}
return [`INSERT INTO ${table} (${cols.join(",")}) VALUES ${def.join(",")}`, values];
}

console.time("load database");
const db = new Database("test.sqlite");
console.timeEnd("load database");

console.time("create table");
db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, avatar BLOB)");
console.timeEnd("create table");

const [statement, values] = bulkInsert("users", ["name", "avatar"], new Array(100).fill([ "John Doe", new Uint8Array(10000) ]));

console.time("insert 100 rows");
db.exec(statement, values as any[]);
console.timeEnd("insert 100 rows");

console.time("select 100 rows");
db.query("SELECT * FROM users LIMIT 100").all();
console.timeEnd("select 100 rows");
```

Aside from this, it would be nice if blobs were treated as `Uint8Array` like in Bun.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.