From 7e6f74cd42492c5202b71a3aeb33740a1c2be207 Mon Sep 17 00:00:00 2001 From: zhengya Date: Fri, 27 Mar 2026 17:38:12 +0800 Subject: [PATCH] feat: add index to some metadb tables --- .../db/metadb/migrator/schema/const.go | 2 +- .../schema/rawsql/mysql/ddl_create_table.sql | 12 ++++--- .../schema/rawsql/mysql/issu/6.6.1.68.sql | 35 +++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 server/controller/db/metadb/migrator/schema/rawsql/mysql/issu/6.6.1.68.sql diff --git a/server/controller/db/metadb/migrator/schema/const.go b/server/controller/db/metadb/migrator/schema/const.go index 86df0b90ab4..3259d3fd4e3 100644 --- a/server/controller/db/metadb/migrator/schema/const.go +++ b/server/controller/db/metadb/migrator/schema/const.go @@ -20,5 +20,5 @@ const ( RAW_SQL_ROOT_DIR = "/etc/metadb/schema/rawsql" DB_VERSION_TABLE = "db_version" - DB_VERSION_EXPECTED = "6.6.1.67" + DB_VERSION_EXPECTED = "6.6.1.68" ) diff --git a/server/controller/db/metadb/migrator/schema/rawsql/mysql/ddl_create_table.sql b/server/controller/db/metadb/migrator/schema/rawsql/mysql/ddl_create_table.sql index 618d9a73608..4d44a6d4a6f 100644 --- a/server/controller/db/metadb/migrator/schema/rawsql/mysql/ddl_create_table.sql +++ b/server/controller/db/metadb/migrator/schema/rawsql/mysql/ddl_create_table.sql @@ -1123,7 +1123,8 @@ CREATE TABLE IF NOT EXISTS pod_group ( updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP, deleted_at DATETIME DEFAULT NULL, INDEX pod_namespace_id_index(pod_namespace_id), - INDEX pod_cluster_id_index(pod_cluster_id) + INDEX pod_cluster_id_index(pod_cluster_id), + INDEX lcuuid_index(lcuuid) ) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; TRUNCATE TABLE pod_group; @@ -1138,7 +1139,8 @@ CREATE TABLE IF NOT EXISTS pod_group_port ( domain CHAR(64) DEFAULT '', lcuuid CHAR(64) DEFAULT '', created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP + updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + INDEX lcuuid_index(lcuuid) ) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; TRUNCATE TABLE pod_group_port; @@ -1198,7 +1200,8 @@ CREATE TABLE IF NOT EXISTS pod ( INDEX epc_id_index(epc_id), INDEX az_index(az), INDEX region_index(region), - INDEX domain_index(domain) + INDEX domain_index(domain), + INDEX lcuuid_index(lcuuid) ) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; TRUNCATE TABLE pod; @@ -1264,7 +1267,8 @@ CREATE TABLE IF NOT EXISTS process ( updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP, deleted_at DATETIME DEFAULT NULL, INDEX domain_sub_domain_gid_updated_at_index(domain, sub_domain, gid, updated_at DESC), - INDEX deleted_at_index(deleted_at) + INDEX deleted_at_index(deleted_at), + INDEX lcuuid_index(lcuuid) ) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; TRUNCATE TABLE process; diff --git a/server/controller/db/metadb/migrator/schema/rawsql/mysql/issu/6.6.1.68.sql b/server/controller/db/metadb/migrator/schema/rawsql/mysql/issu/6.6.1.68.sql new file mode 100644 index 00000000000..9716b8b2629 --- /dev/null +++ b/server/controller/db/metadb/migrator/schema/rawsql/mysql/issu/6.6.1.68.sql @@ -0,0 +1,35 @@ +DROP PROCEDURE IF EXISTS AddIndexIfNotExists; + +CREATE PROCEDURE AddIndexIfNotExists( + IN tableName VARCHAR(255), + IN indexName VARCHAR(255), + IN indexCol VARCHAR(255) +) +BEGIN + DECLARE index_count INT; + + -- check if index exists on the target column (regardless of index name) + SELECT COUNT(*) + INTO index_count + FROM information_schema.statistics + WHERE table_schema = DATABASE() + AND table_name = tableName + AND column_name = indexCol; + + -- if no index on this column, add index + IF index_count = 0 THEN + SET @sql = CONCAT('ALTER TABLE ', tableName, ' ADD INDEX ', indexName, ' (', indexCol, ') USING BTREE'); + PREPARE stmt FROM @sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + END IF; +END; + +CALL AddIndexIfNotExists('pod', 'lcuuid_index', 'lcuuid'); +CALL AddIndexIfNotExists('pod_group', 'lcuuid_index', 'lcuuid'); +CALL AddIndexIfNotExists('pod_group_port', 'lcuuid_index', 'lcuuid'); +CALL AddIndexIfNotExists('process', 'lcuuid_index', 'lcuuid'); + +DROP PROCEDURE AddIndexIfNotExists; + +UPDATE db_version SET version='6.6.1.68';