Skip to content

Commit e65b751

Browse files
committed
optimize insert
1 parent d3fb1f7 commit e65b751

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

src/lib/server/db/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export type Customer = {
1616
fax: string;
1717
};
1818

19+
export type OrderDetail = {
20+
id: number;
21+
orderId: number;
22+
productId: number;
23+
unitPrice: number;
24+
quantity: number;
25+
discount: number;
26+
};
27+
1928
type QueryWrapper = {
2029
moreRows: boolean;
2130
};
@@ -49,3 +58,26 @@ export function getCustomers({ offset = 0, limit = 50 }): QueryResult<Customer[]
4958
moreRows: getCustomerCount() > offset + limit
5059
};
5160
}
61+
62+
export function getOrderDetailCount(): number {
63+
const stmt = db.prepare(`SELECT COUNT(*) as "cnt" FROM "Order Details"`);
64+
const data = stmt.get() as { cnt: number };
65+
return data.cnt;
66+
}
67+
68+
export function getOrderDetails({ offset = 0, limit = 50 }): QueryResult<OrderDetail[]> {
69+
const stmt = db.prepare(`
70+
SELECT ROWID as "id"
71+
, OrderId as "orderId"
72+
, ProductId as "productId"
73+
, UnitPrice as "unitPrice"
74+
, Quantity as "quantity"
75+
, Discount as "discount"
76+
FROM "Order Details" LIMIT $limit OFFSET $offset`);
77+
const data = stmt.all({ limit, offset }) as OrderDetail[];
78+
79+
return {
80+
data,
81+
moreRows: getOrderDetailCount() > offset + limit
82+
};
83+
}

src/lib/sqlite/initStorages.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
type FillStorageResponseData
1111
} from './types';
1212

13-
const storages = ['customers_v1'];
13+
const storages = ['customers_v1', 'order_details_v1'];
1414

1515
async function getStructure(storage: string): Promise<TableStructure> {
1616
const res = await fetch(`/api/data/${storage}/structure`);
@@ -36,6 +36,8 @@ async function createStorage(storage: string, structure: TableStructure) {
3636
}
3737

3838
async function fillStorage(storage: string, structure: TableStructure) {
39+
console.time(`fillStorage-${storage}`);
40+
3941
const PAGE_SIZE = 100;
4042
let currOffset = 0;
4143
let fetchMore = false;
@@ -59,6 +61,8 @@ async function fillStorage(storage: string, structure: TableStructure) {
5961
currOffset += PAGE_SIZE;
6062
fetchMore = moreRows;
6163
} while (fetchMore);
64+
65+
console.timeEnd(`fillStorage-${storage}`);
6266
}
6367

6468
export default async function initStorages() {

src/lib/sqlite/worker/storageHandlers.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,21 @@ export function handleFillStorage(
6868
const src = genInsertSql(storageId, structure);
6969
console.log('Insert sql:', src);
7070

71-
for (const row of rows) {
72-
db.exec({ sql: src, bind: getBindObject(row) });
73-
}
71+
db.transaction(() => {
72+
const stmnt = db.prepare(src);
73+
74+
for (const row of rows) {
75+
try {
76+
stmnt.bind(getBindObject(row));
77+
} catch (err) {
78+
console.error('Error binding row:', row);
79+
} finally {
80+
stmnt.stepReset();
81+
}
82+
}
83+
84+
stmnt.finalize();
85+
});
7486

7587
return {};
7688
} catch (err) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getOrderDetails } from '$lib/server/db';
2+
import { error, json, type RequestHandler } from '@sveltejs/kit';
3+
4+
export const GET = (({ url }) => {
5+
// https://localhost:5173/api/data/customers_v1/data?offset=0&limit=50
6+
const offset = parseInt(url.searchParams.get('offset') ?? '');
7+
const limit = parseInt(url.searchParams.get('limit') ?? '');
8+
9+
if (isNaN(offset) || isNaN(limit)) {
10+
console.error('Invalid offset or limit', { offset, limit });
11+
throw error(400, 'Invalid offset or limit');
12+
}
13+
14+
const data = getOrderDetails({ offset, limit });
15+
return json(data);
16+
}) satisfies RequestHandler;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { json, type RequestHandler } from '@sveltejs/kit';
2+
import type { TableStructure } from '../../types';
3+
4+
export const GET = (() => {
5+
// https://localhost:5173/api/data/customers_v1/structure
6+
7+
const data: TableStructure = {
8+
columns: [
9+
{ name: 'id', type: 'number' },
10+
{ name: 'orderId', type: 'number' },
11+
{ name: 'productId', type: 'number' },
12+
{ name: 'unitPrice', type: 'number' },
13+
{ name: 'quantity', type: 'number' },
14+
{ name: 'discount', type: 'number' }
15+
],
16+
pkColumn: 'id'
17+
};
18+
return json(data);
19+
}) satisfies RequestHandler;

0 commit comments

Comments
 (0)