Skip to content

Commit a2507df

Browse files
ZhengYa-0110lzf575
authored andcommitted
feat: polish metadb index definition
1 parent d27fceb commit a2507df

File tree

4 files changed

+186
-15
lines changed

4 files changed

+186
-15
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.38"
23+
DB_VERSION_EXPECTED = "7.1.0.39"
2424
)

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ CREATE TABLE IF NOT EXISTS vtap (
232232
team_id INTEGER,
233233
expected_revision TEXT,
234234
upgrade_package TEXT,
235-
lcuuid CHAR(64)
235+
lcuuid CHAR(64),
236+
INDEX lcuuid_index(lcuuid)
236237
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
237238
TRUNCATE TABLE vtap;
238239

@@ -385,6 +386,7 @@ CREATE TABLE IF NOT EXISTS domain (
385386
synced_at DATETIME DEFAULT NULL,
386387
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
387388
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
389+
INDEX team_id_index(team_id),
388390
UNIQUE INDEX lcuuid_index(lcuuid)
389391
)ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
390392
TRUNCATE TABLE domain;
@@ -408,6 +410,7 @@ CREATE TABLE IF NOT EXISTS sub_domain (
408410
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
409411
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
410412
deleted_at DATETIME DEFAULT NULL,
413+
INDEX team_id_index(team_id),
411414
UNIQUE INDEX lcuuid_index(lcuuid)
412415
)ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
413416
TRUNCATE TABLE sub_domain;
@@ -488,8 +491,7 @@ CREATE TABLE IF NOT EXISTS vm (
488491
INDEX launch_server_index(launch_server),
489492
INDEX epc_id_index(epc_id),
490493
INDEX az_index(az),
491-
INDEX region_index(region),
492-
INDEX id_index(`id`)
494+
INDEX region_index(region)
493495
)ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
494496
TRUNCATE TABLE vm;
495497

@@ -594,7 +596,8 @@ CREATE TABLE IF NOT EXISTS vl2_net (
594596
lcuuid CHAR(64) DEFAULT '',
595597
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
596598
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
597-
PRIMARY KEY (id)
599+
PRIMARY KEY (id),
600+
INDEX vl2id_index(vl2id)
598601
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
599602
TRUNCATE TABLE vl2_net;
600603

@@ -672,7 +675,9 @@ CREATE TABLE IF NOT EXISTS vinterface (
672675
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
673676
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
674677
INDEX epc_id_index(epc_id),
675-
INDEX mac_index(mac)
678+
INDEX mac_index(mac),
679+
INDEX devicetype_index(devicetype),
680+
INDEX lcuuid_index(lcuuid)
676681
)ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
677682
TRUNCATE TABLE vinterface;
678683

@@ -693,7 +698,8 @@ CREATE TABLE IF NOT EXISTS vinterface_ip (
693698
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
694699
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
695700
INDEX ip_index(`ip`),
696-
INDEX vifid_index(`vifid`)
701+
INDEX vifid_index(`vifid`),
702+
INDEX lcuuid_index(lcuuid)
697703
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
698704
TRUNCATE TABLE vinterface_ip;
699705

@@ -715,7 +721,8 @@ CREATE TABLE IF NOT EXISTS ip_resource (
715721
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
716722
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
717723
INDEX ip_index(`ip`),
718-
INDEX vifid_index(`vifid`)
724+
INDEX vifid_index(`vifid`),
725+
INDEX lcuuid_index(lcuuid)
719726
)ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
720727
TRUNCATE TABLE ip_resource;
721728

@@ -985,7 +992,8 @@ CREATE TABLE IF NOT EXISTS vm_pod_node_connection (
985992
sub_domain CHAR(64) DEFAULT '',
986993
lcuuid CHAR(64) DEFAULT '',
987994
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
988-
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP
995+
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
996+
INDEX pod_node_id_index(pod_node_id)
989997
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
990998
TRUNCATE TABLE vm_pod_node_connection;
991999

@@ -1101,7 +1109,8 @@ CREATE TABLE IF NOT EXISTS pod_service_port (
11011109
lcuuid CHAR(64),
11021110
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
11031111
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1104-
INDEX pod_service_id_index(pod_service_id)
1112+
INDEX pod_service_id_index(pod_service_id),
1113+
INDEX lcuuid_index(lcuuid)
11051114
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11061115
TRUNCATE TABLE pod_service_port;
11071116

@@ -1130,7 +1139,8 @@ CREATE TABLE IF NOT EXISTS pod_group (
11301139
deleted_at DATETIME DEFAULT NULL,
11311140
INDEX pod_namespace_id_index(pod_namespace_id),
11321141
INDEX pod_cluster_id_index(pod_cluster_id),
1133-
INDEX lcuuid_index(lcuuid)
1142+
INDEX lcuuid_index(lcuuid),
1143+
INDEX deleted_at_index(deleted_at)
11341144
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11351145
TRUNCATE TABLE pod_group;
11361146

@@ -1168,7 +1178,8 @@ CREATE TABLE IF NOT EXISTS pod_rs (
11681178
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
11691179
deleted_at DATETIME DEFAULT NULL,
11701180
INDEX pod_group_id_index(pod_group_id),
1171-
INDEX pod_namespace_id_index(pod_namespace_id)
1181+
INDEX pod_namespace_id_index(pod_namespace_id),
1182+
INDEX lcuuid_index(lcuuid)
11721183
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11731184
TRUNCATE TABLE pod_rs;
11741185

@@ -1207,7 +1218,10 @@ CREATE TABLE IF NOT EXISTS pod (
12071218
INDEX az_index(az),
12081219
INDEX region_index(region),
12091220
INDEX domain_index(domain),
1210-
INDEX lcuuid_index(lcuuid)
1221+
INDEX lcuuid_index(lcuuid),
1222+
INDEX pod_ns_created_at_index(pod_namespace_id, created_at),
1223+
INDEX pod_cluster_created_at_index(pod_cluster_id, created_at),
1224+
INDEX deleted_at_index(deleted_at)
12111225
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
12121226
TRUNCATE TABLE pod;
12131227

@@ -1274,7 +1288,8 @@ CREATE TABLE IF NOT EXISTS process (
12741288
deleted_at DATETIME DEFAULT NULL,
12751289
INDEX domain_sub_domain_gid_updated_at_index(domain, sub_domain, gid, updated_at DESC),
12761290
INDEX deleted_at_index(deleted_at),
1277-
INDEX lcuuid_index(lcuuid)
1291+
INDEX lcuuid_index(lcuuid),
1292+
INDEX vtap_id_index(vtap_id)
12781293
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
12791294
TRUNCATE TABLE process;
12801295

@@ -1743,7 +1758,8 @@ CREATE TABLE IF NOT EXISTS ch_os_app_tag (
17431758
`sub_domain_id` INTEGER,
17441759
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
17451760
PRIMARY KEY (`id`, `key`),
1746-
INDEX updated_at_index(`updated_at`)
1761+
INDEX updated_at_index(`updated_at`),
1762+
INDEX domain_sub_domain_id_updated_at_index(domain_id, sub_domain_id, id, updated_at ASC)
17471763
)ENGINE=innodb DEFAULT CHARSET=utf8;
17481764
TRUNCATE TABLE ch_os_app_tag;
17491765

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
SELECT COUNT(*)
12+
INTO index_count
13+
FROM information_schema.statistics
14+
WHERE table_schema = DATABASE()
15+
AND table_name = tableName
16+
AND column_name = indexCol;
17+
18+
IF index_count = 0 THEN
19+
SET @sql = CONCAT('ALTER TABLE ', tableName, ' ADD INDEX ', indexName, ' (', indexCol, ') USING BTREE');
20+
PREPARE stmt FROM @sql;
21+
EXECUTE stmt;
22+
DEALLOCATE PREPARE stmt;
23+
END IF;
24+
END;
25+
26+
DROP PROCEDURE IF EXISTS AddNamedIndexIfNotExists;
27+
28+
-- AddNamedIndexIfNotExists checks by index name (required for composite indexes where
29+
-- the leading column may already have a separate single-column index).
30+
CREATE PROCEDURE AddNamedIndexIfNotExists(
31+
IN tableName VARCHAR(255),
32+
IN indexName VARCHAR(255),
33+
IN indexDef VARCHAR(1024)
34+
)
35+
BEGIN
36+
DECLARE index_count INT;
37+
38+
SELECT COUNT(*)
39+
INTO index_count
40+
FROM information_schema.statistics
41+
WHERE table_schema = DATABASE()
42+
AND table_name = tableName
43+
AND index_name = indexName;
44+
45+
IF index_count = 0 THEN
46+
SET @sql = CONCAT('ALTER TABLE ', tableName, ' ADD INDEX ', indexName, ' ', indexDef);
47+
PREPARE stmt FROM @sql;
48+
EXECUTE stmt;
49+
DEALLOCATE PREPARE stmt;
50+
END IF;
51+
END;
52+
53+
DROP PROCEDURE IF EXISTS DeleteIndexIfExists;
54+
55+
CREATE PROCEDURE DeleteIndexIfExists(
56+
IN tableName VARCHAR(255),
57+
IN indexName VARCHAR(255)
58+
)
59+
BEGIN
60+
DECLARE index_count INT;
61+
62+
SELECT COUNT(*)
63+
INTO index_count
64+
FROM information_schema.statistics
65+
WHERE table_schema = DATABASE()
66+
AND table_name = tableName
67+
AND index_name = indexName;
68+
69+
IF index_count > 0 THEN
70+
SET @sql = CONCAT('ALTER TABLE ', tableName, ' DROP INDEX ', indexName);
71+
PREPARE stmt FROM @sql;
72+
EXECUTE stmt;
73+
DEALLOCATE PREPARE stmt;
74+
END IF;
75+
END;
76+
77+
-- P0: Remove redundant index on vm(id) — id is already the PRIMARY KEY in InnoDB
78+
CALL DeleteIndexIfExists('vm', 'id_index');
79+
80+
-- P0: vinterface — 10w-50w rows; devicetype_index speeds up pod/vm data-build subqueries
81+
-- (WHERE devicetype = N); lcuuid_index speeds up recorder per-resource Update/Delete
82+
CALL AddIndexIfNotExists('vinterface', 'devicetype_index', 'devicetype');
83+
CALL AddIndexIfNotExists('vinterface', 'lcuuid_index', 'lcuuid');
84+
85+
-- P0: vinterface_ip — 10w-50w rows; recorder per-resource Update/Delete by lcuuid
86+
CALL AddIndexIfNotExists('vinterface_ip', 'lcuuid_index', 'lcuuid');
87+
88+
-- P0: ip_resource — 5w-50w rows; recorder per-resource Update/Delete by lcuuid
89+
CALL AddIndexIfNotExists('ip_resource', 'lcuuid_index', 'lcuuid');
90+
91+
-- P1: vtap — multi-module (monitor/genesis/trisolaris) high-frequency lcuuid operations
92+
CALL AddIndexIfNotExists('vtap', 'lcuuid_index', 'lcuuid');
93+
94+
-- P1: pod_rs — recorder CRUD by lcuuid
95+
CALL AddIndexIfNotExists('pod_rs', 'lcuuid_index', 'lcuuid');
96+
97+
-- P1: pod_service_port — 1w-1.5w rows; recorder CRUD by lcuuid
98+
CALL AddIndexIfNotExists('pod_service_port', 'lcuuid_index', 'lcuuid');
99+
100+
-- P1: vl2_net(subnet) — cleaner cascade delete and subnet queries by vl2id
101+
CALL AddIndexIfNotExists('vl2_net', 'vl2id_index', 'vl2id');
102+
103+
-- P1: vm_pod_node_connection — cleaner cascade delete by pod_node_id
104+
CALL AddIndexIfNotExists('vm_pod_node_connection', 'pod_node_id_index', 'pod_node_id');
105+
106+
-- P1: pod — composite indexes for HTTP API paginated + ORDER BY created_at queries
107+
-- pod_namespace_id and pod_cluster_id already have single-column indexes; these
108+
-- composites add ORDER BY support without filesort (use AddNamedIndexIfNotExists
109+
-- since the leading column already has a separate index)
110+
CALL AddNamedIndexIfNotExists('pod', 'pod_ns_created_at_index', '(pod_namespace_id, created_at)');
111+
CALL AddNamedIndexIfNotExists('pod', 'pod_cluster_created_at_index', '(pod_cluster_id, created_at)');
112+
113+
-- P1: process — large table (10w-50w rows); vtap_id_index enables DB-side filtering
114+
-- once the application layer is refactored to push vtap_id filters to SQL
115+
CALL AddIndexIfNotExists('process', 'vtap_id_index', 'vtap_id');
116+
117+
-- P2: domain / sub_domain — team_id_index for FPermit non-admin per-request WHERE team_id IN (?)
118+
CALL AddIndexIfNotExists('domain', 'team_id_index', 'team_id');
119+
CALL AddIndexIfNotExists('sub_domain', 'team_id_index', 'team_id');
120+
121+
-- P2: pod / pod_group — deleted_at_index for cleaner WHERE deleted_at < ? (soft-delete cleanup)
122+
CALL AddIndexIfNotExists('pod', 'deleted_at_index', 'deleted_at');
123+
CALL AddIndexIfNotExists('pod_group', 'deleted_at_index', 'deleted_at');
124+
125+
-- P2: ch_os_app_tag — 10w+ rows (PK=(id,key)); composite index matches tagrecorder healer
126+
-- WHERE domain_id = ? AND sub_domain_id = ? and ClickHouse dict ORDER BY updated_at
127+
CALL AddNamedIndexIfNotExists('ch_os_app_tag', 'domain_sub_domain_id_updated_at_index',
128+
'(domain_id, sub_domain_id, id, updated_at ASC)');
129+
130+
DROP PROCEDURE IF EXISTS AddIndexIfNotExists;
131+
DROP PROCEDURE IF EXISTS AddNamedIndexIfNotExists;
132+
DROP PROCEDURE IF EXISTS DeleteIndexIfExists;
133+
134+
UPDATE db_version SET version='7.1.0.39';

0 commit comments

Comments
 (0)