reduce unnecessary sqlite indexes
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -354,8 +354,12 @@ func EnsureSnapshotIndexes(ctx context.Context, dbConn *sqlx.DB, tableName strin
|
||||
driver := strings.ToLower(dbConn.DriverName())
|
||||
indexes := []string{
|
||||
fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %s_vm_vcenter_idx ON %s ("VmId","Vcenter")`, tableName, tableName),
|
||||
fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %s_snapshottime_idx ON %s ("SnapshotTime")`, tableName, tableName),
|
||||
fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %s_resourcepool_idx ON %s ("ResourcePool")`, tableName, tableName),
|
||||
}
|
||||
if driver != "sqlite" {
|
||||
indexes = append(indexes,
|
||||
fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %s_snapshottime_idx ON %s ("SnapshotTime")`, tableName, tableName),
|
||||
fmt.Sprintf(`CREATE INDEX IF NOT EXISTS %s_resourcepool_idx ON %s ("ResourcePool")`, tableName, tableName),
|
||||
)
|
||||
}
|
||||
// PG-specific helpful indexes; safe no-ops on SQLite if executed, but keep them gated to reduce file bloat.
|
||||
if driver == "pgx" || driver == "postgres" {
|
||||
@@ -373,6 +377,86 @@ func EnsureSnapshotIndexes(ctx context.Context, dbConn *sqlx.DB, tableName strin
|
||||
return nil
|
||||
}
|
||||
|
||||
func listTablesByPrefix(ctx context.Context, dbConn *sqlx.DB, prefix string) ([]string, error) {
|
||||
driver := strings.ToLower(dbConn.DriverName())
|
||||
pattern := prefix + "%"
|
||||
switch driver {
|
||||
case "sqlite":
|
||||
rows, err := dbConn.QueryxContext(ctx, `
|
||||
SELECT name
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table'
|
||||
AND name LIKE ?
|
||||
ORDER BY name DESC
|
||||
`, pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var tables []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tables = append(tables, name)
|
||||
}
|
||||
return tables, rows.Err()
|
||||
case "pgx", "postgres":
|
||||
rows, err := dbConn.QueryxContext(ctx, `
|
||||
SELECT tablename
|
||||
FROM pg_catalog.pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename LIKE $1
|
||||
ORDER BY tablename DESC
|
||||
`, pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var tables []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tables = append(tables, name)
|
||||
}
|
||||
return tables, rows.Err()
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported driver: %s", driver)
|
||||
}
|
||||
}
|
||||
|
||||
// CleanupHourlySnapshotIndexes drops low-value per-table indexes on hourly snapshot tables.
|
||||
func CleanupHourlySnapshotIndexes(ctx context.Context, dbConn *sqlx.DB) (int, error) {
|
||||
driver := strings.ToLower(dbConn.DriverName())
|
||||
if driver != "sqlite" {
|
||||
return 0, fmt.Errorf("hourly snapshot index cleanup is only supported for sqlite")
|
||||
}
|
||||
tables, err := listTablesByPrefix(ctx, dbConn, "inventory_hourly_")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
dropped := 0
|
||||
for _, tableName := range tables {
|
||||
if _, err := SafeTableName(tableName); err != nil {
|
||||
continue
|
||||
}
|
||||
indexes := []string{
|
||||
fmt.Sprintf(`DROP INDEX IF EXISTS %s_snapshottime_idx`, tableName),
|
||||
fmt.Sprintf(`DROP INDEX IF EXISTS %s_resourcepool_idx`, tableName),
|
||||
}
|
||||
for _, stmt := range indexes {
|
||||
if _, err := execLog(ctx, dbConn, stmt); err != nil {
|
||||
return dropped, err
|
||||
}
|
||||
dropped++
|
||||
}
|
||||
}
|
||||
return dropped, nil
|
||||
}
|
||||
|
||||
// BackfillSerialColumn sets missing values in a serial-like column for Postgres tables.
|
||||
func BackfillSerialColumn(ctx context.Context, dbConn *sqlx.DB, tableName, columnName string) error {
|
||||
if err := ValidateTableName(tableName); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user