Messages table fixes to improve query performance (#2026)

* Move to yaml sqlc config

* Add util for ungraceful sql execs

* Fix messages schema + add indexes

* Add migration to drop+recreate messages table

* Create index only if does not exist

* Fix typo

* Unexport function
This commit is contained in:
Gabe Kangas 2022-08-03 10:21:55 -07:00 committed by GitHub
parent 0b5ddf433b
commit eda62a91dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 28 deletions

View File

@ -17,7 +17,7 @@ import (
)
const (
schemaVersion = 5
schemaVersion = 6
)
var (

View File

@ -13,7 +13,7 @@ import (
func CreateMessagesTable(db *sql.DB) {
createTableSQL := `CREATE TABLE IF NOT EXISTS messages (
"id" string NOT NULL,
"user_id" INTEGER,
"user_id" TEXT,
"body" TEXT,
"eventType" TEXT,
"hidden_at" DATETIME,
@ -23,20 +23,16 @@ func CreateMessagesTable(db *sql.DB) {
"image" TEXT,
"link" TEXT,
PRIMARY KEY (id)
);CREATE INDEX index ON messages (id, user_id, hidden_at, timestamp);
CREATE INDEX id ON messages (id);
CREATE INDEX user_id ON messages (user_id);
CREATE INDEX hidden_at ON messages (hidden_at);
CREATE INDEX timestamp ON messages (timestamp);`
);`
mustExec(createTableSQL, db)
stmt, err := db.Prepare(createTableSQL)
if err != nil {
log.Fatal("error creating chat messages table", err)
}
defer stmt.Close()
if _, err := stmt.Exec(); err != nil {
log.Fatal("error creating chat messages table", err)
}
// Create indexes
mustExec(`CREATE INDEX IF NOT EXISTS user_id_hidden_at_timestamp ON messages (id, user_id, hidden_at, timestamp);`, db)
mustExec(`CREATE INDEX IF NOT EXISTS idx_id ON messages (id);`, db)
mustExec(`CREATE INDEX IF NOT EXISTS idx_user_id ON messages (user_id);`, db)
mustExec(`CREATE INDEX IF NOT EXISTS idx_hidden_at ON messages (hidden_at);`, db)
mustExec(`CREATE INDEX IF NOT EXISTS idx_timestamp ON messages (timestamp);`, db)
mustExec(`CREATE INDEX IF NOT EXISTS idx_messages_hidden_at_timestamp on messages(hidden_at, timestamp);`, db)
}
// GetMessagesCount will return the number of messages in the database.

View File

@ -29,6 +29,8 @@ func migrateDatabaseSchema(db *sql.DB, from, to int) error {
migrateToSchema4(db)
case 4:
migrateToSchema5(db)
case 5:
migrateToSchema6(db)
default:
log.Fatalln("missing database migration step")
}
@ -42,6 +44,16 @@ func migrateDatabaseSchema(db *sql.DB, from, to int) error {
return nil
}
func migrateToSchema6(db *sql.DB) {
// Fix chat messages table schema. Since chat is ephemeral we can drop
// the table and recreate it.
// Drop the old messages table
mustExec(`DROP TABLE messages`, db)
// Recreate it
CreateMessagesTable(db)
}
// nolint:cyclop
func migrateToSchema5(db *sql.DB) {
// Create the access tokens table.

20
core/data/utils.go Normal file
View File

@ -0,0 +1,20 @@
package data
import (
"database/sql"
log "github.com/sirupsen/logrus"
)
// mustExec will execute a SQL statement on a provided database instance.
func mustExec(s string, db *sql.DB) {
stmt, err := db.Prepare(s)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec()
if err != nil {
log.Warnln(err)
}
}

View File

@ -1,9 +0,0 @@
{
"version": "1",
"packages": [{
"schema": "db/schema.sql",
"queries": "db/query.sql",
"name": "db",
"path": "db"
}]
}

6
sqlc.yaml Normal file
View File

@ -0,0 +1,6 @@
version: 1
packages:
- path: db
name: db
schema: 'db/schema.sql'
queries: 'db/query.sql'