Skip to content

Commit d435cd1

Browse files
ZhengYa-0110SongZhen0704
authored andcommitted
feat: add index to some metadb tables
1 parent 44fbe25 commit d435cd1

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

server/controller/db/metadb/migrator/schema/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ const (
2020
RAW_SQL_ROOT_DIR = "/etc/metadb/schema/rawsql"
2121

2222
DB_VERSION_TABLE = "db_version"
23-
DB_VERSION_EXPECTED = "7.1.0.36"
23+
DB_VERSION_EXPECTED = "7.1.0.37"
2424
)

server/controller/db/metadb/migrator/schema/rawsql/mysql/ddl_create_table.sql

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,8 @@ CREATE TABLE IF NOT EXISTS pod_group (
11281128
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
11291129
deleted_at DATETIME DEFAULT NULL,
11301130
INDEX pod_namespace_id_index(pod_namespace_id),
1131-
INDEX pod_cluster_id_index(pod_cluster_id)
1131+
INDEX pod_cluster_id_index(pod_cluster_id),
1132+
INDEX lcuuid_index(lcuuid)
11321133
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11331134
TRUNCATE TABLE pod_group;
11341135

@@ -1143,7 +1144,8 @@ CREATE TABLE IF NOT EXISTS pod_group_port (
11431144
domain CHAR(64) DEFAULT '',
11441145
lcuuid CHAR(64) DEFAULT '',
11451146
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
1146-
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP
1147+
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1148+
INDEX lcuuid_index(lcuuid)
11471149
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11481150
TRUNCATE TABLE pod_group_port;
11491151

@@ -1203,7 +1205,8 @@ CREATE TABLE IF NOT EXISTS pod (
12031205
INDEX epc_id_index(epc_id),
12041206
INDEX az_index(az),
12051207
INDEX region_index(region),
1206-
INDEX domain_index(domain)
1208+
INDEX domain_index(domain),
1209+
INDEX lcuuid_index(lcuuid)
12071210
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
12081211
TRUNCATE TABLE pod;
12091212

@@ -1269,7 +1272,8 @@ CREATE TABLE IF NOT EXISTS process (
12691272
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
12701273
deleted_at DATETIME DEFAULT NULL,
12711274
INDEX domain_sub_domain_gid_updated_at_index(domain, sub_domain, gid, updated_at DESC),
1272-
INDEX deleted_at_index(deleted_at)
1275+
INDEX deleted_at_index(deleted_at),
1276+
INDEX lcuuid_index(lcuuid)
12731277
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
12741278
TRUNCATE TABLE process;
12751279

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
DROP PROCEDURE IF EXISTS AddIndexIfNotExists;
2+
3+
CREATE PROCEDURE AddIndexIfNotExists(
4+
IN tableName VARCHAR(255),
5+
IN indexName VARCHAR(255),
6+
IN indexCol VARCHAR(255)
7+
)
8+
BEGIN
9+
DECLARE index_count INT;
10+
11+
-- check if index exists on the target column (regardless of index name)
12+
SELECT COUNT(*)
13+
INTO index_count
14+
FROM information_schema.statistics
15+
WHERE table_schema = DATABASE()
16+
AND table_name = tableName
17+
AND column_name = indexCol;
18+
19+
-- if no index on this column, add index
20+
IF index_count = 0 THEN
21+
SET @sql = CONCAT('ALTER TABLE ', tableName, ' ADD INDEX ', indexName, ' (', indexCol, ') USING BTREE');
22+
PREPARE stmt FROM @sql;
23+
EXECUTE stmt;
24+
DEALLOCATE PREPARE stmt;
25+
END IF;
26+
END;
27+
28+
CALL AddIndexIfNotExists('pod', 'lcuuid_index', 'lcuuid');
29+
CALL AddIndexIfNotExists('pod_group', 'lcuuid_index', 'lcuuid');
30+
CALL AddIndexIfNotExists('pod_group_port', 'lcuuid_index', 'lcuuid');
31+
CALL AddIndexIfNotExists('process', 'lcuuid_index', 'lcuuid');
32+
33+
DROP PROCEDURE AddIndexIfNotExists;
34+
35+
UPDATE db_version SET version='7.1.0.37';

0 commit comments

Comments
 (0)