Skip to content

Commit

Permalink
Better index order
Browse files Browse the repository at this point in the history
  • Loading branch information
marco6 committed Oct 30, 2024
1 parent 3e390bd commit 3859761
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions pkg/kine/drivers/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ var (
HAVING deleted = 0
ORDER BY name
)
SELECT kv.id,
name,
SELECT kv.id,
name,
CASE
WHEN kv.created THEN kv.id
ELSE kv.create_revision
END AS create_revision,
lease,
lease,
value
FROM maxkv CROSS JOIN kine kv
ON maxkv.id = kv.id`
Expand All @@ -143,7 +143,6 @@ var (
AND id <= ?
GROUP BY name
HAVING deleted = 0
ORDER BY name
)`

afterSQLPrefix = `
Expand All @@ -160,8 +159,8 @@ var (
ORDER BY id ASC`

ttlSQL = `
SELECT kv.id,
name,
SELECT kv.id,
name,
lease
FROM kine AS kv
JOIN (
Expand All @@ -184,11 +183,11 @@ var (

deleteSQL = `
INSERT INTO kine(name, created, deleted, create_revision, prev_revision, lease, value, old_value)
SELECT
SELECT
name,
0 AS created,
1 AS deleted,
CASE
CASE
WHEN kine.created THEN id
ELSE create_revision
END AS create_revision,
Expand All @@ -202,14 +201,14 @@ var (

createSQL = `
INSERT INTO kine(name, created, deleted, create_revision, prev_revision, lease, value, old_value)
SELECT
SELECT
? AS name,
1 AS created,
0 AS deleted,
0 AS create_revision,
COALESCE(id, 0) AS prev_revision,
? AS lease,
? AS value,
0 AS create_revision,
COALESCE(id, 0) AS prev_revision,
? AS lease,
? AS value,
NULL AS old_value
FROM (
SELECT MAX(id) AS id, deleted
Expand Down
2 changes: 1 addition & 1 deletion pkg/kine/drivers/sqlite/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func applySchemaV0_2(ctx context.Context, txn *sql.Tx) error {
return err
}

if _, err := txn.ExecContext(ctx, `CREATE UNIQUE INDEX kine_name_index ON kine(name, id, deleted)`); err != nil {
if _, err := txn.ExecContext(ctx, `CREATE UNIQUE INDEX kine_name_index ON kine(name ASC, id DESC, deleted)`); err != nil {
return err
}

Expand Down

0 comments on commit 3859761

Please sign in to comment.