Reduce disk reads when editing documents https://github.com/siyuan-note/siyuan/issues/10961

This commit is contained in:
Daniel 2024-04-10 11:49:35 +08:00
parent 59d7b4dfc5
commit 06a2f3fb5c
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -130,6 +130,11 @@ func initDBTables() {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks] failed: %s", err) logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks] failed: %s", err)
} }
_, err = db.Exec("CREATE INDEX idx_blocks_id ON blocks(id)")
if nil != err {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_blocks_id] failed: %s", err)
}
_, err = db.Exec("CREATE INDEX idx_blocks_root_id ON blocks(root_id)") _, err = db.Exec("CREATE INDEX idx_blocks_root_id ON blocks(root_id)")
if nil != err { if nil != err {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_blocks_root_id] failed: %s", err) logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create index [idx_blocks_root_id] failed: %s", err)
@ -139,7 +144,7 @@ func initDBTables() {
if nil != err { if nil != err {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks_fts] failed: %s", err) logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks_fts] failed: %s", err)
} }
_, err = db.Exec("CREATE VIRTUAL TABLE blocks_fts USING fts5(id UNINDEXED, parent_id UNINDEXED, root_id UNINDEXED, hash UNINDEXED, box UNINDEXED, path UNINDEXED, hpath, name, alias, memo, tag, content, fcontent, markdown UNINDEXED, length UNINDEXED, type UNINDEXED, subtype UNINDEXED, ial, sort UNINDEXED, created UNINDEXED, updated UNINDEXED, tokenize=\"siyuan\")") _, err = db.Exec("CREATE VIRTUAL TABLE blocks_fts USING fts5(id, parent_id UNINDEXED, root_id UNINDEXED, hash UNINDEXED, box UNINDEXED, path UNINDEXED, hpath, name, alias, memo, tag, content, fcontent, markdown UNINDEXED, length UNINDEXED, type UNINDEXED, subtype UNINDEXED, ial, sort UNINDEXED, created UNINDEXED, updated UNINDEXED, tokenize=\"siyuan\")")
if nil != err { if nil != err {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks_fts] failed: %s", err) logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks_fts] failed: %s", err)
} }
@ -148,7 +153,7 @@ func initDBTables() {
if nil != err { if nil != err {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks_fts_case_insensitive] failed: %s", err) logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "drop table [blocks_fts_case_insensitive] failed: %s", err)
} }
_, err = db.Exec("CREATE VIRTUAL TABLE blocks_fts_case_insensitive USING fts5(id UNINDEXED, parent_id UNINDEXED, root_id UNINDEXED, hash UNINDEXED, box UNINDEXED, path UNINDEXED, hpath, name, alias, memo, tag, content, fcontent, markdown UNINDEXED, length UNINDEXED, type UNINDEXED, subtype UNINDEXED, ial, sort UNINDEXED, created UNINDEXED, updated UNINDEXED, tokenize=\"siyuan case_insensitive\")") _, err = db.Exec("CREATE VIRTUAL TABLE blocks_fts_case_insensitive USING fts5(id, parent_id UNINDEXED, root_id UNINDEXED, hash UNINDEXED, box UNINDEXED, path UNINDEXED, hpath, name, alias, memo, tag, content, fcontent, markdown UNINDEXED, length UNINDEXED, type UNINDEXED, subtype UNINDEXED, ial, sort UNINDEXED, created UNINDEXED, updated UNINDEXED, tokenize=\"siyuan case_insensitive\")")
if nil != err { if nil != err {
logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks_fts_case_insensitive] failed: %s", err) logging.LogFatalf(logging.ExitCodeReadOnlyDatabase, "create table [blocks_fts_case_insensitive] failed: %s", err)
} }
@ -954,30 +959,59 @@ func deleteByBoxTx(tx *sql.Tx, box string) (err error) {
} }
func deleteBlocksByIDs(tx *sql.Tx, ids []string) (err error) { func deleteBlocksByIDs(tx *sql.Tx, ids []string) (err error) {
in := bytes.Buffer{} placeholders := strings.Repeat("?,", len(ids))
in.Grow(4096) placeholders = placeholders[:len(placeholders)-1]
in.WriteString("(") stmt := "DELETE FROM blocks WHERE id IN (" + placeholders + ")"
args := make([]interface{}, len(ids))
for i, id := range ids { for i, id := range ids {
in.WriteString("'") args[i] = id
in.WriteString(id) }
in.WriteString("'") if err = execStmtTx(tx, stmt, args...); nil != err {
if i < len(ids)-1 { return
in.WriteString(",")
} }
var ftsIDs []string
for _, id := range ids {
removeBlockCache(id) removeBlockCache(id)
ftsIDs = append(ftsIDs, "\""+id+"\"")
} }
in.WriteString(")") stmt = "SELECT ROWID FROM blocks_fts WHERE blocks_fts MATCH 'id:(" + strings.Join(ftsIDs, " OR ") + ")'"
stmt := "DELETE FROM blocks WHERE id IN " + in.String() rows, err := tx.Query(stmt)
if err = execStmtTx(tx, stmt); nil != err { if nil != err {
return logging.LogErrorf("query blocks_fts failed: %s", err)
} return
stmt = "DELETE FROM blocks_fts WHERE id IN " + in.String() }
var rowIDs []string
for rows.Next() {
var rowID int
if err = rows.Scan(&rowID); nil != err {
return
}
rowIDs = append(rowIDs, fmt.Sprintf("%d", rowID))
}
rows.Close()
stmt = "DELETE FROM blocks_fts WHERE rowid IN (" + strings.Join(rowIDs, ",") + ")"
if err = execStmtTx(tx, stmt); nil != err { if err = execStmtTx(tx, stmt); nil != err {
return return
} }
if !caseSensitive { if !caseSensitive {
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE id IN " + in.String() stmt = "SELECT ROWID FROM blocks_fts_case_insensitive WHERE blocks_fts_case_insensitive MATCH 'id:(" + strings.Join(ftsIDs, " OR ") + ")'"
rows, err = tx.Query(stmt)
if nil != err {
logging.LogErrorf("query blocks_fts_case_insensitive failed: %s", err)
return
}
rowIDs = nil
for rows.Next() {
var rowID int
if err = rows.Scan(&rowID); nil != err {
return
}
rowIDs = append(rowIDs, fmt.Sprintf("%d", rowID))
}
rows.Close()
stmt = "DELETE FROM blocks_fts_case_insensitive WHERE rowid IN (" + strings.Join(rowIDs, ",") + ")"
if err = execStmtTx(tx, stmt); nil != err { if err = execStmtTx(tx, stmt); nil != err {
return return
} }