字符編碼泼橘,字符集
后面 字符編碼涝动,字符集統(tǒng)一 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
開發(fā)數(shù)據(jù)庫
10.10.60.195 9701 ksuser ksuser
數(shù)據(jù)分庫分表思想
如果用戶id 有序自增 最好的方式以用戶id 分段模式 決定分庫 , 然后對(duì)用戶id 進(jìn)行取模決定分表
目前公司的用戶id是uuid 所以說不能做到分段分庫 炬灭。 但是我們可以根據(jù)用戶創(chuàng)建時(shí)間來做分段
比如 2021年 7 月 起 到 2023年 8 月為庫1 (可為邏輯庫)
2023年 8 月 起 到 2024年 8 月為庫2 (可為邏輯庫)
每個(gè)庫下面的表 醋粟,以用戶id uuid hash 取模 分32個(gè)分片 。保證用戶會(huì)路由到指定數(shù)據(jù)庫的指定table上
會(huì)話消息表 需要單獨(dú)設(shè)計(jì) 重归,每張表1000 w 米愿,128張表也就12億 會(huì)話消息 。保存會(huì)話未讀消息
歷史消息 提前,以月進(jìn)行一個(gè)劃分吗货,每個(gè)月32張表。 后面應(yīng)該會(huì)用hbase 狈网,歷史消息會(huì)存在大量數(shù)據(jù) 宙搬,而這個(gè)數(shù)據(jù)保存期限笨腥,需要和產(chǎn)品商量。目前保存一個(gè)月 勇垛。 一個(gè)月后清除 (時(shí)間可以配置)
數(shù)據(jù)庫分表設(shè)計(jì)
分庫目前沒做脖母,但是預(yù)留了分庫的代碼,可以后面接入闲孤。
分表目前是按照:用戶uuid的hash值模以32取絕對(duì)值來分谆级,Math.abs(uuid.hashCode()) % 32)。對(duì)應(yīng)32張表讼积。表命名格式:基礎(chǔ)表_取模值肥照,取模值為[0,31] 例如:conversation _ index _ 31
私信相關(guān)表:
conversation_index_31 // 會(huì)話索引表,分表
conversation_msg_31 // 會(huì)話表勤众,分表
msg_record_31 // 未讀消息表舆绎,分表
msg_record_history_202106_31 // 消息歷史表,按 月份 和 uuid取模 兩個(gè)維度分们颜,單獨(dú)說明
關(guān)注相關(guān)表:
attention_conversation_31 // 會(huì)話表吕朵,分表
attention_msg // 消息記錄表,未分表
點(diǎn)贊回復(fù)相關(guān)表:
system_conversation_31 // 系統(tǒng)會(huì)話表窥突,分表
system_conversation_msg // 系統(tǒng)會(huì)話消息記錄表努溃,未分表
msg_record和msg_record_history:
msg_record_history可以看成msg_record的備份表,私信聊天的消息會(huì)雙寫到這兩張表中阻问,已讀 回調(diào)的時(shí)候會(huì)刪掉msg_record中所有的消息梧税,msg_record中專門存放未讀消息,msg_record_history中存放所有的消息記錄则拷。
msg_record_history會(huì)在每個(gè)月的4號(hào)5號(hào)0點(diǎn)時(shí)贡蓖,定時(shí)任務(wù)創(chuàng)建下個(gè)月的分表。以年月為索引煌茬,創(chuàng)建32張分表斥铺。
數(shù)據(jù)庫原始表
/*
Navicat Premium Data Transfer
Source Server : 10.10.60.195 開發(fā)
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 '會(huì)話id',
`read_msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '已讀消息id',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創(chuàng)建時(shí)間',
`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 '會(huì)話索引表';
-- ----------------------------
-- 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 '會(huì)話id',
`msg_to` varchar(16) NOT NULL DEFAULT '' COMMENT '接受者userid',
`msg_from` varchar(16) NOT NULL DEFAULT '' COMMENT '發(fā)送者userid',
`sender_nickname` varchar(50) NOT NULL DEFAULT '' COMMENT '發(fā)送者nickname',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '會(huì)話的最新的msgid ',
`msg_content` text COMMENT '消息內(nèi)容',
`msg_img` varchar(255) NOT NULL DEFAULT '' COMMENT '會(huì)話展示圖片 紅點(diǎn)地址 ',
`client_seq` int(14) unsigned NOT NULL DEFAULT '0' COMMENT '客戶端 時(shí)間戳 ',
`unread_num` int(5) unsigned NOT NULL DEFAULT '1' COMMENT '未讀消息數(shù) ',
`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 '會(huì)話表';
-- ----------------------------
-- 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 '會(huì)話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 '發(fā)送者userid',
`msg_content` text NOT NULL COMMENT '消息內(nèi)容',
`client_seq` int(14) unsigned NOT NULL DEFAULT '0' COMMENT '客戶端序列號(hào)',
`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 '會(huì)話id',
`msg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'msgid',
`action` varchar(255) NOT NULL DEFAULT '' COMMENT '紅包點(diǎn)擊跳轉(zhuǎn) ',
`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 '系統(tǒng)消息表';
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- Table structure for attention_msg
-- ----------------------------
CREATE TABLE `attention_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '流水號(hào)',
`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 '發(fā)送者userid',
`msg_content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息內(nèi)容',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創(chuàng)建時(shí)間',
`conversation_id` int(15) unsigned NOT NULL DEFAULT '0' COMMENT '會(huì)話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='關(guān)注消息表';
-- ----------------------------
-- 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 '會(huì)話id',
`uuid` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '被關(guān)注用戶id',
`un_read_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '未讀數(shù)量',
`read_msg_id` int(11) NOT NULL DEFAULT '0' COMMENT '最新已讀的msg_id',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創(chuàng)建時(shí)間',
`update_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新時(shí)間',
PRIMARY KEY (`id`),
KEY `idx_uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='關(guān)注會(huì)話表';
-- ----------------------------
-- 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 '會(huì)話id',
`uuid` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '用戶id',
`un_read_num` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '未讀數(shù)量',
`msg_content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '最新消息內(nèi)容',
`type` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT '類型:0:點(diǎn)贊坛善,1:回復(fù)晾蜘,....',
`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 '創(chuàng)建時(shí)間',
`update_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新時(shí)間',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_uuid` (`uuid`,`type`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系統(tǒng)會(huì)話表';
-- ----------------------------
-- Table structure for system_conversation_msg
-- ----------------------------
CREATE TABLE `system_conversation_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '流水號(hào)',
`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 '發(fā)送者userid',
`msg_content` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息內(nèi)容',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '創(chuàng)建時(shí)間',
`conversation_id` int(15) unsigned NOT NULL DEFAULT '0' COMMENT '會(huì)話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='系統(tǒng)會(huì)話對(duì)應(yīng)的消息表';
舊設(shè)計(jì)待刪除
不會(huì)出現(xiàn)在會(huì)話列表中數(shù)據(jù),應(yīng)該是不需要插入會(huì)話列表的 眠屎。但如果哪天這些消息需要出現(xiàn)在會(huì)話列表呢剔交。
這時(shí)候 ,我們?cè)诓迦胍膊贿t 改衩。因?yàn)闀?huì)話表是保持單一會(huì)話一條消息岖常。
系統(tǒng)消息表和私信 是否需要區(qū)分呢。個(gè)人建議的話 葫督,還是區(qū)分
私信和 系統(tǒng)消息竭鞍,讀寫訪問量是不同的板惑。
最好能根據(jù)不同場(chǎng)景定義不同消息。而不是一張表care 所有消息偎快。
目前關(guān)注消息冯乘,紅點(diǎn)消息 , 回執(zhí)流程 晒夹。是否可以多次關(guān)注 裆馒。
1 . 分庫uuid 研究 (暫時(shí)不開發(fā))0.5天
2 . 分表從etcd 獲取配置 tablename , num (num 用于取模)丐怯,分表路由 2天
3 . 定時(shí)任務(wù)創(chuàng)建歷史消息表 喷好,其他分表腳本 1天
4 . 私信歷史消息表 對(duì)應(yīng) 發(fā)送私信接口 ,歷史消息接口 變更 (2天)
5 . 系統(tǒng)消息 關(guān)注 attention_msg 读跷,回復(fù)绒窑,點(diǎn)贊 只是日志表可以不用管 ,會(huì)話表需要處理 (1天)舔亭。