Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexes for cleanup #173

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pkg/database/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ type CleanupStmt struct {
func (stmt *CleanupStmt) Build(driverName string, limit uint64) string {
switch driverName {
case MySQL, "mysql":
return fmt.Sprintf(`DELETE FROM %[1]s WHERE %[2]s < :time
ORDER BY %[2]s LIMIT %[3]d`, stmt.Table, stmt.Column, limit)
return fmt.Sprintf(`DELETE FROM %[1]s WHERE %[2]s < :time LIMIT %[3]d`, stmt.Table, stmt.Column, limit)
case PostgreSQL, "postgres":
return fmt.Sprintf(`WITH rows AS (
SELECT %[1]s FROM %[2]s WHERE %[3]s < :time ORDER BY %[3]s LIMIT %[4]d
)
return fmt.Sprintf(`WITH rows AS (SELECT %[1]s FROM %[2]s WHERE %[3]s < :time LIMIT %[4]d)
DELETE FROM %[2]s WHERE %[1]s IN (SELECT %[1]s FROM rows)`, stmt.PK, stmt.Table, stmt.Column, limit)
default:
panic(fmt.Sprintf("invalid database type %s", driverName))
Expand Down
16 changes: 11 additions & 5 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,11 @@ CREATE TABLE event (
count int unsigned NOT NULL,
yaml mediumblob DEFAULT NULL,
created bigint unsigned NOT NULL,
PRIMARY KEY (uuid)
PRIMARY KEY (uuid),
INDEX idx_event_created (created) COMMENT 'Filter for deleting old events'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;


CREATE TABLE ingress (
uuid binary(16) NOT NULL,
cluster_uuid binary(16) NOT NULL,
Expand Down Expand Up @@ -713,7 +715,8 @@ CREATE TABLE prometheus_cluster_metric (
category varchar(255) NOT NULL,
name varchar(255) NOT NULL,
value double NOT NULL,
PRIMARY KEY (cluster_uuid, timestamp, category, name)
PRIMARY KEY (cluster_uuid, timestamp, category, name),
INDEX idx_prometheus_cluster_metric_timestamp (timestamp) COMMENT 'Filter for deleting old cluster metrics'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE prometheus_container_metric (
Expand All @@ -722,7 +725,8 @@ CREATE TABLE prometheus_container_metric (
category varchar(255) NOT NULL,
name varchar(255) NOT NULL,
value double NOT NULL,
PRIMARY KEY (container_uuid, timestamp, category, name)
PRIMARY KEY (container_uuid, timestamp, category, name),
INDEX idx_prometheus_container_metric_timestamp (timestamp) COMMENT 'Filter for deleting old container metrics'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE prometheus_node_metric (
Expand All @@ -731,7 +735,8 @@ CREATE TABLE prometheus_node_metric (
category varchar(255) NOT NULL,
name varchar(255) NOT NULL,
value double NOT NULL,
PRIMARY KEY (node_uuid, timestamp, category, name)
PRIMARY KEY (node_uuid, timestamp, category, name),
INDEX idx_prometheus_node_metric_timestamp (timestamp) COMMENT 'Filter for deleting old node metrics'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE prometheus_pod_metric (
Expand All @@ -740,7 +745,8 @@ CREATE TABLE prometheus_pod_metric (
category varchar(255) NOT NULL,
name varchar(255) NOT NULL,
value double NOT NULL,
PRIMARY KEY (pod_uuid, timestamp, category, name)
PRIMARY KEY (pod_uuid, timestamp, category, name),
INDEX idx_prometheus_pod_metric_timestamp (timestamp) COMMENT 'Filter for deleting old pod metrics'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE pvc (
Expand Down
Loading