{"id":18701046,"url":"https://github.com/toquery/example-spring-sharding-sphere","last_synced_at":"2026-03-05T00:36:37.721Z","repository":{"id":60262958,"uuid":"536032547","full_name":"ToQuery/example-spring-sharding-sphere","owner":"ToQuery","description":"Spring Boot Jpa 读写分离、分库分表最佳实践","archived":false,"fork":false,"pushed_at":"2025-12-28T11:44:03.000Z","size":220,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-30T22:19:49.170Z","etag":null,"topics":["shardingsphere","shardingsphere-jdbc","spring-boot","spring-boot-jpa"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ToQuery.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-09-13T08:35:27.000Z","updated_at":"2025-12-28T11:44:00.000Z","dependencies_parsed_at":"2024-02-13T02:28:13.901Z","dependency_job_id":"872c7e98-bcd3-47e6-b4a2-0f412ebc63cf","html_url":"https://github.com/ToQuery/example-spring-sharding-sphere","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ToQuery/example-spring-sharding-sphere","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ToQuery%2Fexample-spring-sharding-sphere","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ToQuery%2Fexample-spring-sharding-sphere/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ToQuery%2Fexample-spring-sharding-sphere/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ToQuery%2Fexample-spring-sharding-sphere/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ToQuery","download_url":"https://codeload.github.com/ToQuery/example-spring-sharding-sphere/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ToQuery%2Fexample-spring-sharding-sphere/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30102482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T23:59:36.199Z","status":"ssl_error","status_checked_at":"2026-03-04T23:56:48.556Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["shardingsphere","shardingsphere-jdbc","spring-boot","spring-boot-jpa"],"created_at":"2024-11-07T11:40:23.413Z","updated_at":"2026-03-05T00:36:37.703Z","avatar_url":"https://github.com/ToQuery.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# example-spring-sharding-sphere\n\n[TOC]\n\n## 业务场景准备：\n\n共有两个数据库（es3_write_ds_0、es3_write_ds_1），并且数据库都实现一个读库，总共存在4个数据库。存在6张逻辑表（如下）：\n\n- tb_account 账号表\n- tb_address 地址表\n- tb_order 主订单表\n- tb_order_item 子订单表\n- tb_user 用户表\n- tb_statistics_order统计订单表\n\ntb_user 为广播表，所有库表都一致，tb_account、tb_address 为分库不分表，tb_order、tb_order_item为分库分表，tb_statistics_order多字段分库分表。\n\n### 逻辑数据\n\n- tb_user\n\n| id   | username | pwd    |\n| ---- | -------- | ------ |\n| 1    | user_1   | user_1 |\n| 2    | user_2   | user_2 |\n| 3    | user_3   | user_3 |\n| 4    | user_4   | user_4 |\n| 5    | user_5   | user_5 |\n| 6    | user_6   | user_6 |\n\n------\n\n- tb_account\n\n| 数据库（物理） | id   | user_id | account_status |\n| -------------- | ---- | ------- | -------------- |\n| es3_write_ds_0 | 2    | 2       | enable         |\n|                | 4    | 4       | enable         |\n|                | 6    | 6       | enable         |\n| es3_write_ds_1 | 1    | 1       | enable         |\n|                | 3    | 3       | enable         |\n|                | 5    | 5       | enable         |\n\n------\n\n- tb_address\n\n| 数据库（物理） | id   | user_id | address_name    |\n| -------------- | ---- | ------- | --------------- |\n| es3_write_ds_0 | 1    | 2       | user_2_address_ |\n|                | 2    | 4       | user_4_address_ |\n|                | 3    | 6       | user_6_address_ |\n| es3_write_ds_1 | 4    | 1       | user_1_address_ |\n|                | 5    | 3       | user_3_address_ |\n|                | 6    | 5       | user_5_address_ |\n\n------\n\n- tb_order\n\n| 数据库（物理） | 数据表（物理） | id   | user_id | address_id | order_status | create_date_time    |\n| -------------- | -------------- | ---- | ------- | ---------- | ------------ | ------------------- |\n| es3_write_ds_0 | tb_order_0     | 2    | 2       | 2          | 5            | 2022-11-02 11:11:11 |\n|                |                | 4    | 4       | 4          | 5            | 2022-11-04 11:11:11 |\n|                |                | 6    | 6       | 6          | 1            | 2022-11-06 11:11:11 |\n|                | tb_order_1     | 1    | 2       | 2          | 4            | 2022-11-01 11:11:11 |\n|                |                | 3    | 4       | 4          | 1            | 2022-11-03 11:11:11 |\n|                |                | 5    | 6       | 6          | 1            | 2022-11-05 11:11:11 |\n| es3_write_ds_1 | tb_order_0     | 8    | 1       | 1          | 3            | 2022-11-02 11:11:11 |\n|                |                | 10   | 3       | 3          | 1            | 2022-11-04 11:11:11 |\n|                |                | 12   | 5       | 5          | 1            | 2022-11-06 11:11:11 |\n|                | tb_order_1     | 7    | 1       | 1          | 2            | 2022-11-01 11:11:11 |\n|                |                | 9    | 3       | 3          | 1            | 2022-11-03 11:11:11 |\n|                |                | 11   | 5       | 5          | 1            | 2022-11-05 11:11:11 |\n\n------\n\n- tb_order_item\n\n| 数据库（物理） | 数据表（物理）  | id   | user_id | order_id | order_item_status |\n| -------------- | --------------- | ---- | ------- | -------- | ----------------- |\n| es3_write_ds_0 | tb_order_item_0 | 1   | 2          | 2       | PAY          |\n| | | 2 | 2 | 2 | PAY |\n|                |                | 3    | 4          | 4       | PAY          |\n| | | 4 | 4 | 4 | PAY |\n|                |                | 5    | 6          | 6       | PAY          |\n| | | 6 | 6 | 6 | PAY |\n|                | tb_order_item_1 | 7    | 2          | 1       | PAY          |\n| | | 8 | 2 | 1 | PAY |\n|                |                | 9    | 4          | 3       | PAY          |\n| | | 10 | 4 | 3 | PAY |\n|                |                | 11   | 6          | 5       | PAY          |\n| | | 12 | 6 | 5 | PAY |\n| es3_write_ds_1 | tb_order_item_0 | 13   | 1          | 8       | PAY          |\n| | | 14 | 1 | 8 | PAY |\n|                |                | 15  | 3          | 10      | PAY          |\n| | | 16 | 3 | 10 | PAY |\n|                |                | 17   | 5          | 12      | PAY          |\n| | | 18 | 5 | 12 | PAY |\n|                | tb_order_item_1 | 19   | 1          | 7       | PAY          |\n| | | 20 | 1 | 7 | PAY |\n|                |                | 21   | 3          | 9       | PAY          |\n| | | 22 | 3 | 9 | PAY |\n|                |                | 23  | 5          | 11     | PAY          |\n| | | 24 | 5 | 11 | PAY |\n\n- tb_statistics_order 统计订单表\n\n| 数据库（物理） | 数据表（物理）                | id   | store_id | user_id | order_id | pay_date_time       |\n| -------------- | ----------------------------- | ---- | -------- | ------- | -------- | ------------------- |\n| es3_write_ds_0 | tb_statistics_order_0_2022_01 | 1    | 1        | 4       | 3        | 2022-01-03 22:22:22 |\n|                |                               | 2    | 2        | 6       | 6        | 2022-01-06 22:22:22 |\n|                | tb_statistics_order_0_2022_02 |      |          |         |          |                     |\n|                | tb_statistics_order_0_2022_03 |      |          |         |          |                     |\n|                |                               |      |          |         |          |                     |\n|                | tb_statistics_order_1_2022_01 |      |          |         |          |                     |\n|                | tb_statistics_order_1_2022_02 | 3    | 3        | 2       | 1        | 2022-02-01 22:22:22 |\n|                |                               | 4    | 1        | 4       | 4        | 2022-02-04 22:22:22 |\n|                | tb_statistics_order_1_2022_03 |      |          |         |          |                     |\n|                |                               |      |          |         |          |                     |\n|                | tb_statistics_order_2_2022_01 |      |          |         |          |                     |\n|                | tb_statistics_order_2_2022_02 |      |          |         |          |                     |\n|                | tb_statistics_order_2_2022_03 | 5    | 2        | 2       | 2        | 2022-03-02 22:22:22 |\n|                |                               | 6    | 3        | 6       | 5        | 2022-03-05 22:22:22 |\n|                |                               |      |          |         |          |                     |\n| es3_write_ds_1 | tb_statistics_order_0_2022_01 | 7    | 1        | 3       | 9        | 2022-01-09 22:22:22 |\n|                |                               | 8    | 2        | 5       | 12       | 2022-01-12 22:22:22 |\n|                | tb_statistics_order_0_2022_02 |      |          |         |          |                     |\n|                | tb_statistics_order_0_2022_03 |      |          |         |          |                     |\n|                |                               |      |          |         |          |                     |\n|                | tb_statistics_order_0_2022_01 |      |          |         |          |                     |\n|                | tb_statistics_order_1_2022_02 | 9    | 3        | 1       | 7        | 2022-02-07 22:22:22 |\n|                |                               | 10   | 1        | 3       | 10       | 2022-02-10 22:22:22 |\n|                | tb_statistics_order_0_2022_03 |      |          |         |          |                     |\n|                |                               |      |          |         |          |                     |\n|                | tb_statistics_order_2_2022_01 |      |          |         |          |                     |\n|                | tb_statistics_order_2_2022_02 |      |          |         |          |                     |\n|                | tb_statistics_order_2_2022_03 | 11   | 2        | 1       | 8        | 2022-03-08 22:22:22 |\n|                |                               | 12   | 3        | 5       | 11       | 2022-03-11 22:22:22 |\n\n\n\n\n------\n\n### 物理结构\n\n数据库物理结构如下，不体现两个读库（与主库一致）\n\n| 数据库         | 数据表                | 分库 | 分表 | 分表规则                      |\n| -------------- | --------------------- | ---- | ---- | ----------------------------- |\n| es3_write_ds_0 | tb_user               | -    | -    |                               |\n|                | tb_account            | 是   | -    |                               |\n|                | tb_address            | 是   | -    |                               |\n|                | tb_order_0            | 是   | 是   | id%2                          |\n|                | tb_order_1            | 是   | 是   | id%2                          |\n|                | tb_order_item_0       | 是   | 是   | 自定义：order_id%2            |\n|                | tb_order_item_1       | 是   | 是   | 自定义：order_id%2            |\n|                | tb_statistics_order_0 | 是   | 是   | 自定义：order_id%2_yyyy_MM_dd |\n|                | tb_statistics_order_1 | 是   | 是   | 自定义：order_id%2_yyyy_MM_dd |\n| es3_write_ds_1 | tb_user               | -    | -    |                               |\n|                | tb_account            | 是   | -    |                               |\n|                | tb_address            | 是   | -    |                               |\n|                | tb_order_0            | 是   | 是   | id%2                          |\n|                | tb_order_1            | 是   | 是   | id%2                          |\n|                | tb_order_item_0       | 是   | 是   | 自定义：order_id%2            |\n|                | tb_order_item_1       | 是   | 是   | 自定义：order_id%2            |\n|                | tb_statistics_order_0 | 是   | 是   | 自定义：order_id%2_yyyy_MM_dd |\n|                | tb_statistics_order_1 | 是   | 是   | 自定义：order_id%2_yyyy_MM_dd |\n\n### 分库、分表规则\n\n1. 首先根据 user_id 除2取余分配数据库\n2. tb_order、tb_order_item 分库后，根据 order_id 除2取余分表，tb_order_item为自定义分表策略\n3. tb_statistics_order 多字段分表，根据 order_id 除2取余分表_yyyy_MM_dd\n\n\n\n## 场景模拟调用\n\n- 跨库、跨表查询用户和订单信息 /open/order/user/1\n- 保存（批量）用户订单等信息 /open/order/save\n- 跨库、跨表查询订单列表 /open/order/list\n- 跨表查询订单列表 /open/order/list?userId=1\n- 查询订单列表 /open/order/list?userId=1\u0026orderId=1\n- 分片表tb_order为主表，inner join关联查询用户表  /open/order/list-user?userId=1\u0026orderId=1\n- 跨库、跨表分页查询订单数据 /open/order/page\n- 跨库、跨表统计订单数据 /open/order/statistics\n- 跨库统计订单数据 /open/order/statistics?storeId=1\u0026orderId=2\n- 跨表统计订单数据 /open/order/statistics?userId=2\n- 单分片表查询 /open/order/statistics?storeId=1\u0026orderId=2\u0026userId=1\n- 跨库、跨表订单统计 /open/order/statistics\n- \n\n\n\n## Q\u0026A\n\n\n\nQ：分库、分表场景中事务回滚问题\n\nA：\n\n------\n\nQ：在where条件中没有分表语句时，ShardingSphere是如何做的\n\nA：首先在分库场景下，每个库都会执行相同操作也就会汇总到最终的分表场景；分表时会 UNION ALL **所有** 分片表，也就是有多个分片表就需要查询多少个，因此也杜绝这种查询业务！！！\n\n------\n\nQ：如上面tb_order数据，进行分页 size=2 并排序 order by order_status,create_date_time desc  查询，是否会丢失ID为1、7、8的数据？\n\nA: 分析日志为四个物理表进行了分页 limit 0,2 查询，第一页查询排序后拿到所有信息正常分页获取，第二页查询时分页为 0,4查询，获取到之前所有分页的数据，程序进行拼装分页，所以不会丢失查询第一页未使用的数据场景。但会引入一个问题，越往后分页在程序里拼接的数据会越来越多？这个场景其实并不正确，因为没有分表条件无法定位到具体表信息，从而无法进行单表的分页操作。\n\n------\n\nQ：大量数据场景中，（如上一个问题）页码越往后分页在程序里拼接的数据会越来越多？\n\nA：如上，这个场景其实并不正确，因为没有分表条件无法定位到具体表信息，从而无法进行单表的分页操作。\n\n------\n\nQ：分表条件下 where in 查询分库字段是否会分表规则\n\nA：会查询到具体表\n\n------\n\nQ：能否更新分表规则字段？数据是否会自动迁移\n\nA：不能更新分表字段，提示 Can not update sharding value for table 错误。解决方式如下，方法一： update 语句 where 条件增加字段值并且和原值一致，方法二：update 语句删除修改分表字段。JPA通过修改注解属性`@Column(name = \"xxx\", updatable = false)`解决。\n\n------\n\nQ：\n\nA：\n\n\n\n## TODO\n\n- [x] 分页查询时，order by 非分表条件、实际会查询所有分表的前（page*size）行记录，但越往后数据越多？？？\n- [x] 更换查询维度，原表先删除，新表再新增？\n- [x] 跨表wherein是否能直接到表\n- [ ] 多字段跨表，where in 和时间范围综合影响\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoquery%2Fexample-spring-sharding-sphere","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoquery%2Fexample-spring-sharding-sphere","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoquery%2Fexample-spring-sharding-sphere/lists"}