tauri-apps / tauri-apps/plugins-workspace
[sql] Insert and select operations are slow (SQLite)
- Dominant language
- Rust
- Stars
- 1.8k
- Forks
- 602
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 9
Description
Tauri - `@tauri-apps/plugin-sql`

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`

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
Assessment
This issue has not been assessed yet.