graph TD
A[交易系统] -->|实时同步| B[业务数据库]
C[会员系统] -->|实时同步| B
D[库存系统] -->|定时同步| B
B -->|ETL抽取| E[数据仓库]
E -->|定时计算| F[客户标签库]
E -->|定时计算| G[商品关联分析]
F --> H[精准营销系统]
G --> H
-- 传感器基础信息表 CREATE TABLE `sensors` ( `sensor_id` varchar(50) NOT NULL, `sensor_type` varchar(30) NOT NULL, `location` varchar(100) NOT NULL, `install_time` datetime NOT NULL, `status` tinyint NOT NULLDEFAULT'1', PRIMARY KEY (`sensor_id`), KEY `idx_type_location` (`sensor_type`,`location`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 传感器数据表(按月分区) CREATE TABLE `sensor_readings` ( `id` bigintNOT NULL AUTO_INCREMENT, `sensor_id` varchar(50) NOT NULL, `reading_time` datetime(3) NOT NULL, `temperature` decimal(5,2) DEFAULTNULL, `humidity` decimal(5,2) DEFAULTNULL, `pressure` decimal(8,2) DEFAULTNULL, `battery` decimal(5,2) DEFAULTNULL, PRIMARY KEY (`id`,`reading_time`), KEY `idx_sensor_time` (`sensor_id`,`reading_time`) ) ENGINE=InnoDB PARTITIONBYRANGE (MONTH(reading_time)) ( PARTITION p1 VALUES LESS THAN (2), PARTITION p2 VALUES LESS THAN (3), PARTITION p3 VALUES LESS THAN (4), PARTITION p4 VALUES LESS THAN (5), PARTITION p5 VALUES LESS THAN (6), PARTITION p6 VALUES LESS THAN (7), PARTITION p7 VALUES LESS THAN (8), PARTITION p8 VALUES LESS THAN (9), PARTITION p9 VALUES LESS THAN (10), PARTITION p10 VALUES LESS THAN (11), PARTITION p11 VALUES LESS THAN (12), PARTITION p12 VALUES LESS THAN (MAXVALUE) );
graph TD
A[设备传感器] -->|实时采集| B[边缘网关]
B -->|数据过滤| C[数据接入服务]
C -->|写入| D[时序数据库]
C -->|异常事件| E[MySQL主库]
E -->|同步| F[MySQL从库]
G[监控平台] -->|查询| F
H[管理系统] -->|操作| E
-- 账户表 CREATE TABLE `accounts` ( `account_id` varchar(32) NOT NULL, `user_id` bigintNOT NULL, `account_type` tinyint NOT NULL COMMENT '1:储蓄账户 2:信用账户', `currency` char(3) NOT NULLDEFAULT'CNY', `balance` decimal(18,2) NOT NULLDEFAULT'0.00', `status` tinyint NOT NULLDEFAULT'1', `create_time` datetime NOT NULLDEFAULTCURRENT_TIMESTAMP, `update_time` datetime NOT NULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP, PRIMARY KEY (`account_id`), KEY `idx_user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 交易记录表 CREATE TABLE `transactions` ( `transaction_id` varchar(64) NOT NULL, `from_account` varchar(32) DEFAULTNULL, `to_account` varchar(32) DEFAULTNULL, `amount` decimal(18,2) NOT NULL, `currency` char(3) NOT NULL, `transaction_type` tinyint NOT NULL COMMENT '1:转账 2:充值 3:提现', `status` tinyint NOT NULLDEFAULT'0' COMMENT '0:处理中 1:成功 2:失败', `create_time` datetime NOT NULLDEFAULTCURRENT_TIMESTAMP, `complete_time` datetime DEFAULTNULL, PRIMARY KEY (`transaction_id`), KEY `idx_from_account` (`from_account`,`create_time`), KEY `idx_to_account` (`to_account`,`create_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
金融系统的MySQL优化重点:
使用InnoDB事务确保数据一致性
实施分布式事务处理复杂业务场景
严格的数据备份和灾备策略
完善的审计日志系统
2. 实际案例:支付系统的MySQL架构
某支付公司构建了高可用的MySQL架构,支撑每天数千万笔交易:
graph TD
A[支付网关] --> B[交易处理服务]
B --> C[数据库代理层]
C --> D[主库集群A区域]
C --> E[主库集群B区域]
D --> F[从库集群A区域]
E --> G[从库集群B区域]
H[监控系统] --> D
H --> E
H --> F
H --> G