feat: complete shared database tenant sharding

This commit is contained in:
richjohnrambo
2026-09-23 02:19:07 +08:00
parent 1ec2feee47
commit e37b52c3c0
37 changed files with 946 additions and 1420 deletions
@@ -59,19 +59,9 @@ ALTER TABLE `im_group_message_shards`
ADD UNIQUE INDEX `uk_im_group_message_shards_table` (`tenant_id`,`table_name`),
ADD INDEX `idx_im_group_message_shards_status` (`tenant_id`,`status`,`shard_month`);
ALTER TABLE `im_group_message_topic_shards`
DROP PRIMARY KEY,
DROP INDEX `idx_im_group_message_topic_shards_seq`,
DROP INDEX `idx_im_group_message_topic_shards_month`,
ADD PRIMARY KEY (`tenant_id`,`topic`,`shard_month`),
ADD INDEX `idx_im_group_message_topic_shards_seq` (`tenant_id`,`topic`,`max_seq`),
ADD INDEX `idx_im_group_message_topic_shards_month` (`tenant_id`,`shard_month`,`topic`);
DROP TABLE IF EXISTS `im_group_message_topic_shards`;
ALTER TABLE `im_group_message_dedup`
DROP PRIMARY KEY,
DROP INDEX `idx_im_group_message_dedup_topic_seq`,
ADD PRIMARY KEY (`tenant_id`,`topic`,`from_user`,`client_msg_id`),
ADD INDEX `idx_im_group_message_dedup_topic_seq` (`tenant_id`,`topic`,`seqid`);
DROP TABLE IF EXISTS `im_group_message_dedup`;
-- 3. 创建商户 2 的好友与单聊消息分表并复制快照数据。
CREATE TABLE `im_002_friendships` LIKE `im_friendships`;
@@ -208,21 +198,7 @@ SELECT 1,`shard_month`,CONCAT('im_001_group_messages_',`shard_month`),`shard_sta
`status`,`row_count`,`min_created_at`,`max_created_at`,`created_at`,`updated_at`
FROM `tinode`.`im_group_message_shards`;
INSERT INTO `im_group_message_topic_shards` (
`tenant_id`,`topic`,`shard_month`,`min_seq`,`max_seq`,`message_count`,
`min_created_at`,`max_created_at`,`created_at`,`updated_at`
)
SELECT 1,`topic`,`shard_month`,`min_seq`,`max_seq`,`message_count`,
`min_created_at`,`max_created_at`,`created_at`,`updated_at`
FROM `tinode`.`im_group_message_topic_shards`;
INSERT INTO `im_group_message_dedup` (
`tenant_id`,`topic`,`from_user`,`client_msg_id`,`seqid`,`shard_month`,
`message_id`,`status`,`expires_at`,`created_at`,`updated_at`
)
SELECT 1,`topic`,`from_user`,`client_msg_id`,`seqid`,`shard_month`,
`message_id`,`status`,`expires_at`,`created_at`,`updated_at`
FROM `tinode`.`im_group_message_dedup`;
-- Topic-to-month routes are Redis-backed and are rebuilt by runtime reads.
-- 以实际复制后的物理表为准刷新统计,避免源库路由统计滞后。
DELIMITER $$