|
|
|
|
<!doctype html>
|
|
|
|
|
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-split-db" data-has-hydrated="false">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="generator" content="Docusaurus v3.0.0">
|
|
|
|
|
<title data-rh="true">9.29 分表分库 | Furion</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="http://furion.baiqian.ltd/docs/split-db"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="9.29 分表分库 | Furion"><meta data-rh="true" name="description" content="本章 9.28.2 至 9.28.5 小节挪用博主:雨点的名字 的 分库分表 - 理论 博客内容。特此声明。"><meta data-rh="true" property="og:description" content="本章 9.28.2 至 9.28.5 小节挪用博主:雨点的名字 的 分库分表 - 理论 博客内容。特此声明。"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="http://furion.baiqian.ltd/docs/split-db"><link data-rh="true" rel="alternate" href="http://furion.baiqian.ltd/docs/split-db" hreflang="en"><link data-rh="true" rel="alternate" href="http://furion.baiqian.ltd/docs/split-db" hreflang="x-default"><link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="Furion RSS Feed">
|
|
|
|
|
<link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="Furion Atom Feed"><link rel="stylesheet" href="/assets/css/styles.3f87a095.css">
|
|
|
|
|
<script src="/assets/js/runtime~main.89709a83.js" defer="defer"></script>
|
|
|
|
|
<script src="/assets/js/main.10e5dc01.js" defer="defer"></script>
|
|
|
|
|
</head>
|
|
|
|
|
<body class="navigation-with-keyboard">
|
|
|
|
|
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const a=new URLSearchParams(window.location.search).entries();for(var[t,e]of a)if(t.startsWith("docusaurus-data-")){var n=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(n,e)}}catch(t){}}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())</script><div id="__docusaurus"><div class="floatbar_hUWl"><div class="qrcode_sWwE"><img title="微信扫码关注 Furion 官方公众号" src="/img/weixin_qrcode.jpg" style="display:block"><div>❤️ 关注 Furion 微信公众号有惊喜哦!</div></div><div style="display:flex;flex-direction:row-reverse;align-items:flex-start"><div class="title_SD0k">🫠 遇到问题了</div><div class="extend__5i1"><a class="item_Y35a" href="/docs/subscribe"><div style="flex:1"><div class="itemTitle_If_T">⭐️ VIP 服务 ⭐️</div><div class="itemDesc_dp7r">仅需 499 元/年,尊享 365 天项目无忧</div></div><div class="jiantou_mV_o"></div></a><div class="item_Y35a"><div style="flex:1"><div class="itemTitle_If_T">问题反馈</div><div class="itemDesc_dp7r">到 Furion 开源仓库反馈</div></div><div class="jiantou_mV_o"></div></div></div></div></div><div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><div class="announcementBar_mb4j" style="background-color:#4623d9;color:yellow" role="banner"><div class="content_knG7 announcementBarContent_xLdY">⭐️ 开通 VIP 服务仅需 499 元/年,尊享 365 天项目无忧 <a href="/docs/subscribe" style="background-color:rgb(199, 29, 36);color:#ffffff;padding:1px 10px;border-radius:3px;text-decoration:none;display:inline-block;margin:0 5px;font-size:12px;cursor:pointer;">立即开通</a>⭐️</div></div><nav aria-label="Main" class="navbar navbar--fixed-top navbarHideable_m1mJ"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/furionlogo.png" alt="Furion Logo" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src="/img/furionlogo.png" alt="Furion Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><b class="navbar__title text--truncate">Furion</b></a><a aria-current="page" class="navbar__item navbar__link navbar__link--active" href="/docs/category/appendix">文档</a><a class="navbar__item navbar__link" href="/docs/global/app">静态类</a><a class="navbar__item navbar__link" href="/docs/settings/appsettings">配置</a><a class="navbar__item navbar__link" href="/blog">博客</a><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">更新日志</a><ul class="dropdown__menu"><li><a class="dropdown__link" href="/docs/upgrade">📝 查看日志(v4.9.1.7)</a></li><li><a class="dropdown__link" href="/docs/target">🚀 路线图</a></li></ul></div><a href="http://furion.baiqian.ltd/api/api" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">API<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></div><div class="navbar__items navbar__it
|
|
|
|
|
<h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9291-应用场景">9.29.1 应用场景<a href="#9291-应用场景" class="hash-link" aria-label="Direct link to 9.29.1 应用场景" title="Direct link to 9.29.1 应用场景"></a></h2>
|
|
|
|
|
<p>数据库中的数据量不一定是可控的,在未进行分库分表的情况下,随着时间和业务的发展,库中的表会越来越多,表中的数据量也会越来越大,相应地,数据操作增删改查的开销也会越来越大。</p>
|
|
|
|
|
<p>另外,由于无法进行分布式部署,而一台服务器的资源(CPU、磁盘、内存、IO 等)是有限的,最终数据库所能承载的数据量、数据处理能力都将遭遇瓶颈。</p>
|
|
|
|
|
<p>这个时候就需要对数据库或数据表进行拆分。</p>
|
|
|
|
|
<p>数据切分可以分为:<code>垂直切分</code> 和 <code>水平切分</code>。</p>
|
|
|
|
|
<h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9292-垂直切分">9.29.2 垂直切分<a href="#9292-垂直切分" class="hash-link" aria-label="Direct link to 9.29.2 垂直切分" title="Direct link to 9.29.2 垂直切分"></a></h2>
|
|
|
|
|
<p>垂直切分又可以分为: 垂直分库和垂直分表。</p>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="92921-垂直分库">9.29.2.1 垂直分库<a href="#92921-垂直分库" class="hash-link" aria-label="Direct link to 9.29.2.1 垂直分库" title="Direct link to 9.29.2.1 垂直分库"></a></h3>
|
|
|
|
|
<p>根据业务耦合性,将关联度低的不同表存储在不同的数据库。做法与大系统拆分为多个小系统类似,按业务分类进行独立划分。与"微服务治理"的做法相似,每个微服务使用单独的一个数据库。</p>
|
|
|
|
|
<img src="/img/dbsplit1.jpg">
|
|
|
|
|
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>说明</div><div class="admonitionContent_BuS1"><p>一开始我们是单体服务,所以只有一个数据库,所有的表都在这个库里。</p><p>后来因为业务需求,单体服务变成微服务治理。所以将之前的一个商品库,拆分成多个数据库。每个微服务对于一个数据库。</p></div></div>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="92922-垂直分表">9.29.2.2 垂直分表<a href="#92922-垂直分表" class="hash-link" aria-label="Direct link to 9.29.2.2 垂直分表" title="Direct link to 9.29.2.2 垂直分表"></a></h3>
|
|
|
|
|
<p>把一个表的多个字段分别拆成多个表,一般按字段的冷热拆分,热字段一个表,冷字段一个表。从而提升了数据库性能。</p>
|
|
|
|
|
<img src="/img/dbsplit2.jpg">
|
|
|
|
|
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>说明</div><div class="admonitionContent_BuS1"><p>一开始商品表中包含商品的所有字段,但是我们发现:</p><p>1.商品详情和商品属性字段较长。2.商品列表的时候我们是不需要显示商品详情和商品属性信息,只有在点进商品商品的时候才会展示商品详情信息。</p><p>所以可以考虑把商品详情和商品属性单独切分一张表,提高查询效率。</p></div></div>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="92923-优缺点">9.29.2.3 优缺点<a href="#92923-优缺点" class="hash-link" aria-label="Direct link to 9.29.2.3 优缺点" title="Direct link to 9.29.2.3 优缺点"></a></h3>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
|
|
|
|
<p>优点</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>解决业务系统层面的耦合,业务清晰</li>
|
|
|
|
|
<li>与微服务的治理类似,也能对不同业务的数据进行分级管理、维护、监控、扩展等</li>
|
|
|
|
|
<li>高并发场景下,垂直切分一定程度的提升 IO、数据库连接数、单机硬件资源的瓶颈</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
<p>缺点</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>分库后无法 Join,只能通过接口聚合方式解决,提升了开发的复杂度</li>
|
|
|
|
|
<li>分库后分布式事务处理复杂</li>
|
|
|
|
|
<li>依然存在单表数据量过大的问题(需要水平切分)</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9293-水平切分">9.29.3 水平切分<a href="#9293-水平切分" class="hash-link" aria-label="Direct link to 9.29.3 水平切分" title="Direct link to 9.29.3 水平切分"></a></h2>
|
|
|
|
|
<p>当一个应用难以再细粒度的垂直切分或切分后数据量行数巨大,存在单库读写、存储性能瓶颈,这时候就需要进行水平切分了。</p>
|
|
|
|
|
<p>水平 |