From ac9aab63bd48702dac29b1e6fa9721289037eda5 Mon Sep 17 00:00:00 2001 From: Albert S Date: Tue, 28 Sep 2021 18:10:53 +0200 Subject: [PATCH] Change 'timestamp' to simple unix epoch Subjectively, 6d4e7a96dc6e4b62a1151d38172839961c705623 increases lag on a years-old production database with ~240k entries when searching with CTRL+R. So change timestamp column from datetime type (which is a string) to integer, storing unix epoch now. As we deal with those primarily, we also avoid conversions now. Mentioned database shrinks by 2MB compared to previously (both VACCUMed)... DROP unused count_by_date VIEW. --- main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 2b66914..1c3fb5e 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,11 @@ func migrateDatabase(conn *sql.DB, currentVersion int) { migrations := []string{ "ALTER TABLE history ADD COLUMN workdir varchar(4096) DEFAULT ''", "ALTER TABLE history ADD COLUMN retval integer DEFAULT -9001", + "ALTER TABLE history ADD COLUMN unix_tmp integer", + "UPDATE history SET unix_tmp = strftime('%s', timestamp)", + "DROP VIEW count_by_date", + "ALTER TABLE history DROP COLUMN timestamp", + "ALTER TABLE history RENAME COLUMN unix_tmp TO timestamp", } if !(len(migrations) > currentVersion) { @@ -164,7 +169,7 @@ type searchopts struct { func search(conn *sql.DB, opts searchopts) list.List { args := make([]interface{}, 0) var sb strings.Builder - sb.WriteString("SELECT id, command, workdir, user, hostname, retval, strftime(\"%s\", timestamp) ") + sb.WriteString("SELECT id, command, workdir, user, hostname, retval, timestamp ") sb.WriteString("FROM history ") sb.WriteString("WHERE 1=1 ") //1=1 so we can append as many AND foo as we want, or none @@ -177,11 +182,11 @@ func search(conn *sql.DB, opts searchopts) list.List { args = append(args, opts.workdir) } if opts.after != nil { - sb.WriteString("AND timestamp > datetime(?, 'unixepoch') ") + sb.WriteString("AND timestamp > ? ") args = append(args, opts.after.Unix()) } if opts.before != nil { - sb.WriteString("AND timestamp < datetime(?, 'unixepoch') ") + sb.WriteString("AND timestamp < ? ") args = append(args, opts.before.Unix()) } if opts.retval != nil { @@ -234,7 +239,7 @@ func delete(conn *sql.DB, entryId uint32) { } func add(conn *sql.DB, entry HistoryEntry) { - stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir, timestamp, retval) VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'),?)") + stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir, timestamp, retval) VALUES (?, ?, ?, ?, ?,?)") if err != nil { log.Panic(err) }