分库分表第七篇之广播表
2020-05-28 本文已影响0人
小螺丝钉cici
存在这样的情况:表结构和表中的数据在每个数据库中完全一致,比如字典表。
那么这时候应该怎么办?广播表这时候就应运而生了。
定义:指所有的分片数据源中都存在的表,表结构和表中的数据在每个数据库中完全一致。
适用:数据量不大且需要与海量数据的表进行关联查询的场景,例如:字典表。
实战操作我们需要满足如下:
假如有3张表,t_order ,t_order_item ,t_dictionary
(1)在每个数据库表都存在该表以及表结构都一样。
(2)当保存的时候,每个数据库都会插入相同的数据。
#广播表规则列表:说明t_dictionary为广播表
spring.shardingsphere.sharding.broadcast-tables[0]=t_dictionary
@Autowired
private DictionaryService dictionaryService;
@Test
public void testBroadCast() {
Dictionary dictionary = new Dictionary();
dictionary.setMykey("systemName");
dictionary.setMyvalue("kfit");
dictionary.setUid(1000);
dictionaryService.save(dictionary);
System.out.println(dictionaryService.getAll());
System.out.println(dictionaryService.getAll());
}
测试说明:
在ds0和ds1存在相同的表结构。
在ds0和ds1都会保存1条相同的数据。
主键使用的是数据库默认的增长方式
当查询的时候,会随机的选择一个数据源进行查询。