Skip to content

Commit a29f305

Browse files
committed
feat: polish metadb index definition
1 parent 90638a2 commit a29f305

4 files changed

Lines changed: 187 additions & 15 deletions

File tree

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 = "6.6.1.68"
23+
DB_VERSION_EXPECTED = "6.6.1.69"
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
@@ -230,7 +230,8 @@ CREATE TABLE IF NOT EXISTS vtap (
230230
team_id INTEGER,
231231
expected_revision TEXT,
232232
upgrade_package TEXT,
233-
lcuuid CHAR(64)
233+
lcuuid CHAR(64),
234+
INDEX lcuuid_index(lcuuid)
234235
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
235236
TRUNCATE TABLE vtap;
236237

@@ -382,6 +383,7 @@ CREATE TABLE IF NOT EXISTS domain (
382383
synced_at DATETIME DEFAULT NULL,
383384
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
384385
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
386+
INDEX team_id_index(team_id),
385387
UNIQUE INDEX lcuuid_index(lcuuid)
386388
)ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
387389
TRUNCATE TABLE domain;
@@ -404,6 +406,7 @@ CREATE TABLE IF NOT EXISTS sub_domain (
404406
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
405407
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
406408
deleted_at DATETIME DEFAULT NULL,
409+
INDEX team_id_index(team_id),
407410
UNIQUE INDEX lcuuid_index(lcuuid)
408411
)ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
409412
TRUNCATE TABLE sub_domain;
@@ -484,8 +487,7 @@ CREATE TABLE IF NOT EXISTS vm (
484487
INDEX launch_server_index(launch_server),
485488
INDEX epc_id_index(epc_id),
486489
INDEX az_index(az),
487-
INDEX region_index(region),
488-
INDEX id_index(`id`)
490+
INDEX region_index(region)
489491
)ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
490492
TRUNCATE TABLE vm;
491493

@@ -590,7 +592,8 @@ CREATE TABLE IF NOT EXISTS vl2_net (
590592
lcuuid CHAR(64) DEFAULT '',
591593
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
592594
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
593-
PRIMARY KEY (id)
595+
PRIMARY KEY (id),
596+
INDEX vl2id_index(vl2id)
594597
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
595598
TRUNCATE TABLE vl2_net;
596599

@@ -668,7 +671,9 @@ CREATE TABLE IF NOT EXISTS vinterface (
668671
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
669672
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
670673
INDEX epc_id_index(epc_id),
671-
INDEX mac_index(mac)
674+
INDEX mac_index(mac),
675+
INDEX devicetype_index(devicetype),
676+
INDEX lcuuid_index(lcuuid)
672677
)ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
673678
TRUNCATE TABLE vinterface;
674679

@@ -689,7 +694,8 @@ CREATE TABLE IF NOT EXISTS vinterface_ip (
689694
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
690695
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
691696
INDEX ip_index(`ip`),
692-
INDEX vifid_index(`vifid`)
697+
INDEX vifid_index(`vifid`),
698+
INDEX lcuuid_index(lcuuid)
693699
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
694700
TRUNCATE TABLE vinterface_ip;
695701

@@ -711,7 +717,8 @@ CREATE TABLE IF NOT EXISTS ip_resource (
711717
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
712718
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
713719
INDEX ip_index(`ip`),
714-
INDEX vifid_index(`vifid`)
720+
INDEX vifid_index(`vifid`),
721+
INDEX lcuuid_index(lcuuid)
715722
)ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
716723
TRUNCATE TABLE ip_resource;
717724

@@ -979,7 +986,8 @@ CREATE TABLE IF NOT EXISTS vm_pod_node_connection (
979986
sub_domain CHAR(64) DEFAULT '',
980987
lcuuid CHAR(64) DEFAULT '',
981988
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
982-
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP
989+
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
990+
INDEX pod_node_id_index(pod_node_id)
983991
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
984992
TRUNCATE TABLE vm_pod_node_connection;
985993

@@ -1095,7 +1103,8 @@ CREATE TABLE IF NOT EXISTS pod_service_port (
10951103
lcuuid CHAR(64),
10961104
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
10971105
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1098-
INDEX pod_service_id_index(pod_service_id)
1106+
INDEX pod_service_id_index(pod_service_id),
1107+
INDEX lcuuid_index(lcuuid)
10991108
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11001109
TRUNCATE TABLE pod_service_port;
11011110

@@ -1124,7 +1133,8 @@ CREATE TABLE IF NOT EXISTS pod_group (
11241133
deleted_at DATETIME DEFAULT NULL,
11251134
INDEX pod_namespace_id_index(pod_namespace_id),
11261135
INDEX pod_cluster_id_index(pod_cluster_id),
1127-
INDEX lcuuid_index(lcuuid)
1136+
INDEX lcuuid_index(lcuuid),
1137+
INDEX deleted_at_index(deleted_at)
11281138
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11291139
TRUNCATE TABLE pod_group;
11301140

@@ -1162,7 +1172,8 @@ CREATE TABLE IF NOT EXISTS pod_rs (
11621172
updated_at DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
11631173
deleted_at DATETIME DEFAULT NULL,
11641174
INDEX pod_group_id_index(pod_group_id),
1165-
INDEX pod_namespace_id_index(pod_namespace_id)
1175+
INDEX pod_namespace_id_index(pod_namespace_id),
1176+
INDEX lcuuid_index(lcuuid)
11661177
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
11671178
TRUNCATE TABLE pod_rs;
11681179

@@ -1201,7 +1212,10 @@ CREATE TABLE IF NOT EXISTS pod (
12011212
INDEX az_index(az),
12021213
INDEX region_index(region),
12031214
INDEX domain_index(domain),
1204-
INDEX lcuuid_index(lcuuid)
1215+
INDEX lcuuid_index(lcuuid),
1216+
INDEX pod_ns_created_at_index(pod_namespace_id, created_at),
1217+
INDEX pod_cluster_created_at_index(pod_cluster_id, created_at),
1218+
INDEX deleted_at_index(deleted_at)
12051219
) ENGINE=innodb AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
12061220
TRUNCATE TABLE pod;
12071221

@@ -1268,7 +1282,8 @@ CREATE TABLE IF NOT EXISTS process (
12681282
deleted_at DATETIME DEFAULT NULL,
12691283
INDEX domain_sub_domain_gid_updated_at_index(domain, sub_domain, gid, updated_at DESC),
12701284
INDEX deleted_at_index(deleted_at),
1271-
INDEX lcuuid_index(lcuuid)
1285+
INDEX lcuuid_index(lcuuid),
1286+
INDEX vtap_id_index(vtap_id)
12721287
) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
12731288
TRUNCATE TABLE process;
12741289

@@ -1729,7 +1744,8 @@ CREATE TABLE IF NOT EXISTS ch_os_app_tag (
17291744
`sub_domain_id` INTEGER,
17301745
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
17311746
PRIMARY KEY (`id`, `key`),
1732-
INDEX updated_at_index(`updated_at`)
1747+
INDEX updated_at_index(`updated_at`),
1748+
INDEX domain_sub_domain_id_updated_at_index(domain_id, sub_domain_id, id, updated_at ASC)
17331749
)ENGINE=innodb DEFAULT CHARSET=utf8;
17341750
TRUNCATE TABLE ch_os_app_tag;
17351751

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='6.6.1.69';

0 commit comments

Comments
 (0)