字符編碼,字符集
後面 字符編碼,字符集統一 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
開發數據庫
10.10.60.195 9701 ksuser ksuser
數據分庫分表思想
如果用戶id 有序自增 最好的方式以用戶id 分段模式 決定分庫 , 然後對用戶id 進行取模決定分表
目前公司的用戶id是uuid 所以說不能做到分段分庫 。 但是我們可以根據用戶創建時間來做分段
比如 2021年 7 月 起 到 2023年 8 月爲庫1 (可爲邏輯庫)
2023年 8 月 起 到 2024年 8 月爲庫2 (可爲邏輯庫)
每個庫下面的表 ,以用戶id uuid hash 取模 分32個分片 。保證用戶會路由到指定數據庫的指定table上
會話消息表 需要單獨設計 ,每張表1000 w ,128張表也就12億 會話消息 。保存會話未讀消息
歷史消息 ,以月進行一個劃分,每個月32張表。 後面應該會用hbase ,歷史消息會存在大量數據 ,而這個數據保存期限,需要和產品商量。目前保存一個月 。 一個月後清除 (時間可以配置)
數據庫分表設計
分庫目前沒做,但是預留了分庫的代碼,可以後面接入。
分表目前是按照:用戶uuid的hash值模以32取絕對值來分,Math.abs(uuid.hashCode()) % 32)。對應32張表。表命名格式:基礎表_取模值,取模值爲[0,31] 例如:conversation _ index _ 31
私信相關表:
conversation_index_31 // 會話索引表,分表
conversation_msg_31 // 會話表,分表
msg_record_31 // 未讀消息表,分表
msg_record_history_202106_31 // 消息歷史表,按 月份 和 uuid取模 兩個維度分,單獨說明
關注相關表:
attention_conversation_31 // 會話表,分表
attention_msg // 消息記錄表,未分表
點贊回覆相關表:
system_conversation_31 // 系統會話表,分表
system_conversation_msg // 系統會話消息記錄表,未分表
msg_record和msg_record_history:
msg_record_history可以看成msg_record的備份表,私信聊天的消息會雙寫到這兩張表中,已讀 回調的時候會刪掉msg_record中所有的消息,msg_record中專門存放未讀消息,msg_record_history中存放所有的消息記錄。
msg_record_history會在每個月的4號5號0點時,定時任務創建下個月的分表。以年月爲索引,創建32張分表。
數據庫原始表
/*
Navicat Premium Data Transfer
Source Server : 10.10.60.195 開發
Source Server Type : MySQL
Source Server Version : 50620
Source Host : 10.10.60.195:9701
Source Schema : private_message
Target Server Type : MySQL
Target Server Version : 50620
File Encoding : 65001
Date: 15/04/2021 15:00:02
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for conversation_index
-- ----------------------------
DROP TABLE IF EXISTS `conversation_index`;
CREATE TABLE `conversation_index` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` bigint(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`read_msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '已讀消息id',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創建時間',
`ext_index1` int(11) NOT NULL DEFAULT '0',
`ext_index2` int(11) NOT NULL DEFAULT '0',
`ext_field1` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_cid` (`conversation_id`) USING BTREE,
KEY `idx_read_msg_id` (`read_msg_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '會話索引表';
-- ----------------------------
-- Table structure for conversation_msg
-- ----------------------------
DROP TABLE IF EXISTS `conversation_msg`;
CREATE TABLE `conversation_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` bigint(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`msg_to` varchar(16) NOT NULL DEFAULT '' COMMENT '接受者userid',
`msg_from` varchar(16) NOT NULL DEFAULT '' COMMENT '發送者userid',
`sender_nickname` varchar(50) NOT NULL DEFAULT '' COMMENT '發送者nickname',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '會話的最新的msgid ',
`msg_content` text COMMENT '消息內容',
`msg_img` varchar(255) NOT NULL DEFAULT '' COMMENT '會話展示圖片 紅點地址 ',
`client_seq` int(14) unsigned NOT NULL DEFAULT '0' COMMENT '客戶端 時間戳 ',
`unread_num` int(5) unsigned NOT NULL DEFAULT '1' COMMENT '未讀消息數 ',
`is_del` tinyint(1) unsigned NOT NULL DEFAULT '0',
`ext_field1` varchar(255) NOT NULL DEFAULT '',
`ext_field2` varchar(255) NOT NULL DEFAULT '',
`create_time` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `msg_to` (`msg_to`,`msg_from`),
UNIQUE KEY `idx_cid` (`conversation_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '會話表';
-- ----------------------------
-- Table structure for msg_record
-- ----------------------------
DROP TABLE IF EXISTS `msg_record`;
CREATE TABLE `msg_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` bigint(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'msg id',
`msg_to` varchar(32) NOT NULL DEFAULT '' COMMENT '接受者userid',
`msg_from` varchar(32) NOT NULL DEFAULT '' COMMENT '發送者userid',
`msg_content` text NOT NULL COMMENT '消息內容',
`client_seq` int(14) unsigned NOT NULL DEFAULT '0' COMMENT '客戶端序列號',
`msg_type` int(5) unsigned NOT NULL DEFAULT '0' COMMENT '消息類型',
`msg_code` int(5) unsigned NOT NULL DEFAULT '0' COMMENT '消息碼,映射他趣消息類型',
`is_del` tinyint(1) unsigned NOT NULL DEFAULT '0',
`create_time` bigint(20) NOT NULL DEFAULT '0',
`push_time` bigint(20) NOT NULL DEFAULT '0',
`ext_field1` varchar(255) NOT NULL DEFAULT '',
`ext_field2` varchar(255) NOT NULL DEFAULT '',
`ext_field3` varchar(255) NOT NULL DEFAULT '',
`ext_field4` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_cid` (`conversation_id`,`msg_id`) USING BTREE,
KEY `idx_msg_to` (`msg_to`),
KEY `idx_msg_from` (`msg_from`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '消息記錄表';
-- ----------------------------
-- Table structure for system_msg
-- ----------------------------
DROP TABLE IF EXISTS `system_msg`;
CREATE TABLE `system_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` bigint(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'msgid',
`action` varchar(255) NOT NULL DEFAULT '' COMMENT '紅包點擊跳轉 ',
`file` varchar(255) NOT NULL DEFAULT '' COMMENT '紅包圖片地址 or 文件地址 or 語音地址 or 視頻 ',
`content` varchar(255) NOT NULL DEFAULT '' COMMENT '保存title 或者富文本消息 ',
`is_del` tinyint(1) unsigned NOT NULL DEFAULT '0',
`create_time` bigint(11) NOT NULL DEFAULT '0',
`ext_field1` varchar(255) NOT NULL DEFAULT '',
`ext_field2` varchar(255) NOT NULL DEFAULT '',
`ext_field3` varchar(255) NOT NULL DEFAULT '',
`ext_field4` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_cmid` (`conversation_id`,`msg_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '系統消息表';
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- Table structure for attention_msg
-- ----------------------------
CREATE TABLE `attention_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '流水號',
`msg_to` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '接受者userid',
`msg_from` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '發送者userid',
`msg_content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息內容',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創建時間',
`conversation_id` int(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '消息id',
PRIMARY KEY (`id`),
KEY `idx_msg_to` (`msg_to`),
KEY `idx_msg_from` (`msg_from`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=186 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='關注消息表';
-- ----------------------------
-- Table structure for attention_conversation
-- ----------------------------
CREATE TABLE `attention_conversation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` int(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`uuid` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '被關注用戶id',
`un_read_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '未讀數量',
`read_msg_id` int(11) NOT NULL DEFAULT '0' COMMENT '最新已讀的msg_id',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創建時間',
`update_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新時間',
PRIMARY KEY (`id`),
KEY `idx_uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='關注會話表';
-- ----------------------------
-- Table structure for system_conversation
-- ----------------------------
CREATE TABLE `system_conversation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conversation_id` int(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`uuid` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '用戶id',
`un_read_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '未讀數量',
`msg_content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '最新消息內容',
`type` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT '類型:0:點贊,1:回覆,....',
`read_msg_id` int(11) NOT NULL DEFAULT '0' COMMENT '最新已讀的msg_id',
`last_msg_id` int(11) NOT NULL DEFAULT '0' COMMENT '最新的msg_id',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創建時間',
`update_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新時間',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_uuid` (`uuid`,`type`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系統會話表';
-- ----------------------------
-- Table structure for system_conversation_msg
-- ----------------------------
CREATE TABLE `system_conversation_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '流水號',
`msg_to` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '接受者userid',
`msg_from` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '發送者userid',
`msg_content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息內容',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創建時間',
`conversation_id` int(15) unsigned NOT NULL DEFAULT '0' COMMENT '會話id',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '消息id',
PRIMARY KEY (`id`),
KEY `idx_msg_to` (`msg_to`),
KEY `idx_msg_from` (`msg_from`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=212 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系統會話對應的消息表';
舊設計待刪除
不會出現在會話列表中數據,應該是不需要插入會話列表的 。但如果哪天這些消息需要出現在會話列表呢。
這時候 ,我們在插入也不遲 。因爲會話表是保持單一會話一條消息。
系統消息表和私信 是否需要區分呢。個人建議的話 ,還是區分
私信和 系統消息,讀寫訪問量是不同的。
最好能根據不同場景定義不同消息。而不是一張表care 所有消息。
目前關注消息,紅點消息 , 回執流程 。是否可以多次關注 。
1 . 分庫uuid 研究 (暫時不開發)0.5天
2 . 分表從etcd 獲取配置 tablename , num (num 用於取模),分表路由 2天
3 . 定時任務創建歷史消息表 ,其他分表腳本 1天
4 . 私信歷史消息表 對應 發送私信接口 ,歷史消息接口 變更 (2天)
5 . 系統消息 關注 attention_msg ,回覆,點贊 只是日誌表可以不用管 ,會話表需要處理 (1天)。