最近参与了一个股市量化项目,我主要负责行情数据接入——将交易中心的数据拉取到本地进行持久化,供后续分析与统计。在对比开发、运维与性能成本后,持久化选型定为「MySQL + 分表」。以往分库分表多由业务层硬编码路由,这次尝试引入 ShardingSphere(Sharding-JDBC),记录一次踩坑复盘。
背景与容量评估
系统主要持久化逐笔交易分钟线与快照行情,每日增量预估如下:
1
2
3
4
5
6
| - 逐笔交易:60 * 24 * 3000 = 4.32M 条
- 快照数据:20 * 60 * 24 * 3000 = 8.64M 条
日均存储空间估算:
4.32M * 400B + 8.64M * 50B ≈ 2.5GB
结合索引与压缩,日增量约在 2GB ~ 5GB。
|
早期也曾评估 MongoDB、ElasticSearch、HBase 等方案,但综合查询方式与运维成本最终仍选择了 MySQL:按股票/期货代码分表后,单表年数据量控制在千万级,配合定期归档即可满足 MySQL 单表性能边界,ORM 层沿用 MyBatis。
ShardingSphere 接入实践
按照官方文档配置数据源与分片规则非常直观:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| @Bean
@ConfigurationProperties(prefix = "spring.datasource.main")
public HikariConfig hikariConfig() {
return new HikariConfig();
}
@Bean
public DataSource hikariDataSource() {
return new HikariDataSource(hikariConfig());
}
@Bean
public TableRuleConfiguration barTableRuleConfig() {
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration("stock_bar_stockcode_granularity");
tableRuleConfig.setCheckActualDataNode(false);
tableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setTableShardingStrategyConfig(
new ComplexShardingStrategyConfiguration("stock_code,granularity", (availableTargetNames, shardingValue) -> {
// 自定义复合分片路由
return Arrays.asList(...);
})
);
return tableRuleConfig;
}
@Bean
public TableRuleConfiguration snapshotRuleConfig() {
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration("tick_stockcode");
tableRuleConfig.setCheckActualDataNode(false);
tableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration("stock_code", "tick_${stock_code}")
);
return tableRuleConfig;
}
@Bean
@Primary
public DataSource shardingDatasource(TableRuleConfiguration[] tableRuleConfigurations) throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("main", hikariDataSource());
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().addAll(Arrays.asList(tableRuleConfigurations));
Properties properties = new Properties();
properties.put(ShardingPropertiesConstant.CHECK_TABLE_METADATA_ENABLED, false);
return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, properties);
}
|
遇到的两个关键限制
配置中特别关闭了元数据检查:
1
| properties.put(ShardingPropertiesConstant.CHECK_TABLE_METADATA_ENABLED, false);
|
原因在于 ShardingSphere 为了支持跨表分页、排序等 SQL 改写(区分 logic table 与 actual table),会在启动阶段预加载子表元数据并做结构校验(参考 TableMetaDataInitializer#load):
- 启动耗时随子表数量线性膨胀:子表越多,初始化阶段加载元数据越慢;即便通过修改源码跳过元数据加载,问题仍未彻底解决。
- 无法动态感知运行时新增子表:ShardingSphere 在初始化阶段建立逻辑表与物理表的映射关系,并在路由时校验表是否存在(参考
TableMetaDataLoader#isTableExist)。而行情接入场景需要在运行时动态创建新股票对应的物理子表,这直接导致路由失效。
折衷尝试与最终方案
在放弃 DataSource 层分表后,我们一度尝试将路由下沉到 MyBatis 拦截器(Interceptor / Plugin)阶段处理,但很快遇到了另一个深坑:
- MyBatis 拦截器拿到的是预编译 SQL 与按位置绑定的参数,并未做完整 SQL 语法解析,难以通用地提取逻辑表名与分片键值;
- 而如果单独抽取 ShardingSphere 的 SQL 解析引擎嫁接到 MyBatis,改造成本极高(ShardingSphere 过半复杂度都在 SQL 解析与改写上)。
最终权衡下来,对于物理子表需运行时动态扩容的场景,还是在业务 DAO / 路由层直接计算物理表名并显式传入最简单、可控、零黑盒成本。
选型总结
ShardingSphere 能极大简化标准分库分表场景的开发,但引入前建议先核对两条前置条件:
- 表拓扑是否静态可控:若存在频繁运行时动态建表,需确认版本是否支持动态刷新节点映射;
- 单次逻辑查询跨表数量是否可控:例如跨 1000 张分表做
LIMIT 1000 分页,汇总到应用内存就是百万级中间结果,极易引发 OOM。