feat: adding indexes for vault tables

This commit is contained in:
Kacper Kula 2025-02-01 13:33:23 +00:00
parent ad90093e65
commit a0a7192fde
10 changed files with 50 additions and 3 deletions

View file

@ -1,3 +1,6 @@
# 0.22.1 (2025-02-01)
- Added indexes to the `files`, `tags`, `links` and `tasks` tables. This should speed up a lot of common queries performed agains these tables.
# 0.22.0 (2025-01-31)
Breaking change: `created_at` and `modified_at` has been changed from JS unix epoch to ISO 8601 dates (human readable). Thanks to that many of date operations are now easier to perform.
Example, extracting year from the creation date:

View file

@ -1,7 +1,7 @@
{
"id": "sqlseal",
"name": "SQLSeal",
"version": "0.22.0",
"version": "0.22.1",
"minAppVersion": "0.15.0",
"description": "Use SQL in your notes to query your vault files and CSV content.",
"author": "hypersphere",

View file

@ -1,6 +1,6 @@
{
"name": "sqlseal",
"version": "0.22.0",
"version": "0.22.1",
"description": "A plugin for Obsidian that allows you to run SQL queries on your notes.",
"main": "main.js",
"scripts": {

View file

@ -80,6 +80,10 @@ export class SqlSealDatabase {
await this.db.createTableNoTypes(name, columns, noDrop)
}
async createIndex(indexName: string, tableName: string, columns: string[]) {
await this.db.createIndex(indexName, tableName, columns)
}
async getColumns(tableName: string) {
return this.db.getColumns(tableName)
}

View file

@ -216,9 +216,24 @@ export class WorkerDatabase {
}
}
async explainQuery(query: string, params: Record<string, unknown>) {
const stmt = this.db.prepare(`EXPLAIN QUERY PLAN ${query}`, recordToBindParams(params))
const plan = []
while (stmt.step()) {
plan.push(stmt.getAsObject())
}
stmt.free()
return plan
}
async disconnect() {
// FIXME: implement.
}
async createIndex(indexName: string, tableName: string, columns: string[]) {
const query = `CREATE INDEX IF NOT EXISTS ${indexName} ON ${tableName} (${columns.join(', ')})`
this.db.run(query)
}
}
Comlink.expose(WorkerDatabase);

View file

@ -82,5 +82,11 @@ export class FilesFileSyncTable extends AFileSyncTable {
async onInit(): Promise<void> {
this.db.createTableNoTypes(FILES_TABLE_NAME, ['id', 'name', 'path', 'created_at', 'modified_at', 'file_size'])
this.columns = await this.db.getColumns(FILES_TABLE_NAME)
// Indexes
const toIndex = ['id', 'name', 'path']
await Promise.all(toIndex.map(column =>
this.db.createIndex(`files_${column}_idx`, FILES_TABLE_NAME, [column])
))
}
}

View file

@ -51,5 +51,12 @@ export class LinksFileSyncTable extends AFileSyncTable {
async onInit(): Promise<void> {
await this.db.createTableNoTypes(this.tableName, ['path', 'target', 'position', 'display_text', 'target_exists'])
// Indexes
const toIndex = ['path', 'target']
await Promise.all(toIndex.map(column =>
this.db.createIndex(`links_${column}_idx`, this.tableName, [column])
))
}
}

View file

@ -36,5 +36,10 @@ export class TagsFileSyncTable extends AFileSyncTable {
async onInit(): Promise<void> {
await this.db.createTableNoTypes('tags', ['tag', 'fileId'])
// Indexes
const toIndex = ['fileId']
await Promise.all(toIndex.map(column =>
this.db.createIndex(`tags_${column}_idx`, this.tableName, [column])
))
}
}

View file

@ -49,5 +49,11 @@ export class TasksFileSyncTable extends AFileSyncTable {
async onInit(): Promise<void> {
await this.db.createTableNoTypes('tasks', ['task', 'completed', 'filePath'])
// Indexes
const toIndex = ['filePath']
await Promise.all(toIndex.map(column =>
this.db.createIndex(`tasks_${column}_idx`, this.tableName, [column])
))
}
}

View file

@ -36,5 +36,6 @@
"0.21.1": "0.15.0",
"0.21.2": "0.15.0",
"0.21.3": "0.15.0",
"0.22.0": "0.15.0"
"0.22.0": "0.15.0",
"0.22.1": "0.15.0"
}