mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
[Files Refactor] Performance tuning (#2809)
* Use cache during migration * Avoid use of query views * Use FindMany to find related objects * Log slow queries * Add folders to generated files * Use SlimScene for scene queries * Include filename in migration error message
This commit is contained in:
@@ -3,8 +3,14 @@ package sqlite
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
slowLogTime = time.Millisecond * 200
|
||||
)
|
||||
|
||||
type dbReader interface {
|
||||
@@ -14,6 +20,15 @@ type dbReader interface {
|
||||
QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
|
||||
}
|
||||
|
||||
func logSQL(start time.Time, query string, args ...interface{}) {
|
||||
since := time.Since(start)
|
||||
if since >= slowLogTime {
|
||||
logger.Debugf("SLOW SQL [%v]: %s, args: %v", since, query, args)
|
||||
} else {
|
||||
logger.Tracef("SQL [%v]: %s, args: %v", since, query, args)
|
||||
}
|
||||
}
|
||||
|
||||
type dbWrapper struct{}
|
||||
|
||||
func (*dbWrapper) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||
@@ -22,7 +37,11 @@ func (*dbWrapper) Get(ctx context.Context, dest interface{}, query string, args
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Get(dest, query, args...)
|
||||
start := time.Now()
|
||||
err = tx.Get(dest, query, args...)
|
||||
logSQL(start, query, args...)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (*dbWrapper) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||
@@ -31,7 +50,11 @@ func (*dbWrapper) Select(ctx context.Context, dest interface{}, query string, ar
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Select(dest, query, args...)
|
||||
start := time.Now()
|
||||
err = tx.Select(dest, query, args...)
|
||||
logSQL(start, query, args...)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (*dbWrapper) Queryx(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
|
||||
@@ -40,7 +63,11 @@ func (*dbWrapper) Queryx(ctx context.Context, query string, args ...interface{})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tx.Queryx(query, args...)
|
||||
start := time.Now()
|
||||
ret, err := tx.Queryx(query, args...)
|
||||
logSQL(start, query, args...)
|
||||
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (*dbWrapper) NamedExec(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
|
||||
@@ -49,7 +76,11 @@ func (*dbWrapper) NamedExec(ctx context.Context, query string, arg interface{})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tx.NamedExec(query, arg)
|
||||
start := time.Now()
|
||||
ret, err := tx.NamedExec(query, arg)
|
||||
logSQL(start, query, arg)
|
||||
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (*dbWrapper) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||
@@ -58,5 +89,9 @@ func (*dbWrapper) Exec(ctx context.Context, query string, args ...interface{}) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tx.Exec(query, args...)
|
||||
start := time.Now()
|
||||
ret, err := tx.Exec(query, args...)
|
||||
logSQL(start, query, args...)
|
||||
|
||||
return ret, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user