[原创]数据表的水平拆分(1) 初探分库分表
当一个数据表数据量非常大的时候,查询会变得非常慢。 一般来说MySQL达到千万条以后(视情况而定,查询较少的表可能会稍好一点), 所以就要尝试分库分表, 就是所谓的数据库水平拆分。 水平拆分最重要的一点是按照什么 分表 .先不说理论,先看下边实例 用户
当一个数据表数据量非常大的时候,查询会变得非常慢。 一般来说MySQL达到千万条以后(视情况而定,查询较少的表可能会稍好一点), 所以就要尝试分库分表, 就是所谓的数据库水平拆分。
水平拆分最重要的一点是按照什么分表. 先不说理论,先看下边实例
用户表 user( uname-用户名,唯一 upwd- 密码 unickname-昵称)
用户信息表 user_info (uname-用户名,外键 tel-电话 email-邮箱 ......)
假设我们有好多好多数据, 有可能上亿条,甚至十亿 百亿(绝对是史上最大的用户表了),我们怎么分这个数据表?
按照什么分表? 当然 uname 是最佳的选择。 不为什么,就因为我们查询的时候最有可能用到这个字段做为唯一查询条件: select * from user where uname='XXX';
方案1:
按照用户名的最后两位来分表。用户名 hello 的用户就存在 user_lo 里边 , 用户名是world 的就存在 user_ld 里, 这样做的好处是:
1. 按照用户名有这样一个散列规则,如果知道用户名的话, 经过我们的散列规则算法一下就确定了数据保存在那张数据表中, 不需要再搜索
2. 数据表中的数据也基本的平均, 理论上边每个组合的用户名后两位是平均分布的(事实上是有差别的, 如果是自增的数字ID的话是基本上平均的),达到了我们分库分表的初衷
但是这种算法也是有缺点的:
1. 数据表的组合从user_aa 到 user_zz (假如全由字母组成, 不区分大小写), 有 26*26张数据表, 数据表个数是固定的, 如果后期发现有些数据表的数据又比较多了, 要扩展的话就比较难了。
2. 统计起来比较困难。 要查询某一个时间段注册的用户, 这样的就比较困难了,这样的话就必须查询每一张数据表然后把结果合并起来。
再来看另外一个例子: 订单。 大家可以发现京东上边是这样做的, 京东上边默认查询的是当月的订单, 然后所有的订单是另外查询的(卓越亚马逊等其他网站也有好多这么做的)。他们的分表应该是这样的, 数据肯定是要按月分的,你所有的订单他就让你再点一下才可以查到。我们模拟一下:
因为用户只能查到自己的订单, 所以按照用户分表还是必要的, 对于京东来说,按照用户来分还不足以应付这么多的数据量, 所以我们就用一个更复杂一点的散列规则, 按照用户名和日期组合分表:
order_date_name date 表示日期, name 表示用户名的后两位, 比如我这个月订单的数据表就存在 order_201103_ng, 根据我的名字 查询我当月的订单就去这帐数据表中查询, 查询我的所有订单就去 show tables like 'order_%_ng'; 我把的名字后缀的表先取出来, 然后再去 查询这些数据表, 最后合并结果。
当然你可能会问, 后台统计的时候是不是太麻烦了, 我可以很负责地告诉你: 是! 这个没办法, 后台取统计数据的话可以延迟个几分钟, 老板不会说你, 但是如果让前台用户等级分钟老板绝对饶不了你!
总之数据拆分的精髓就在于,根据实际情况将数据按照最合适的规则存储在不同的数据表中,尽量避免多个数据表的数据合并!
更多数据拆分的东西我们以后再讨论, 敬请继续关注我的博客,谢谢!
声明: 本文采用 CC BY-NC-SA 3.0 协议进行授权
转载请注明来源:小景的博客
本文链接地址:http://www.phpv5.com/blog/archives/109/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Get started quickly: JSON array merging and splitting techniques in Java In modern software development, data format and transmission have become increasingly important. Among them, JSON (JavaScriptObjectNotation) is a commonly used data format, especially suitable for front-end and back-end interaction and data storage. In Java development, we often need to deal with JSON objects and JSON arrays. This article explains how to merge and split JSON arrays in Java, along with tips and examples for implementing these operations.

Many gamers are very curious about the graphics card 3060 and want to buy a 3060 display, but they don’t know what level the RTX3060 is at. In fact, this graphics card is also at a relatively mainstream level. What is the level of 3060 graphics card: Answer: Mainstream level. This RTX3060 graphics card is very popular among players and is also on sale. It has the unique advantages of the RTX30 series and is very cost-effective. Players with higher requirements for game graphics can choose this graphics card. 3060 graphics card expansion introduction: Frame rate: It is loved by the majority of players. It has a large video memory of 12GB and is very popular. With 12GB of large video memory, it can run AAA masterpieces smoothly without any lag, and the frame rate can even reach 60. Value for money: it

How to use PHPZipArchive to merge and split multiple compressed packages? Overview: During the development process, sometimes we need to merge multiple compressed packages into one, or split a compressed package into multiple ones. PHP provides the ZipArchive extension to easily complete these operations. This article will introduce how to use PHPZipArchive to merge and split multiple compressed packages. Merging multiple archives First, we need to create a new archive and open it. Then, the loop traversal needs to be

A preliminary study on Java crawlers: To understand its basic concepts and uses, specific code examples are required. With the rapid development of the Internet, obtaining and processing large amounts of data has become an indispensable task for enterprises and individuals. As an automated data acquisition method, crawler (WebScraping) can not only quickly collect data on the Internet, but also analyze and process large amounts of data. Crawlers have become a very important tool in many data mining and information retrieval projects. This article will introduce the basic overview of Java crawlers

MySQL is a common relational database that is a core component of many websites and applications. As the amount of data becomes larger and larger, how to optimize the performance of MySQL becomes particularly important. One of the key areas is the compression of data tables. In this article we will introduce the data table compression technology in MySQL. Compressed tables and non-compressed tables There are two types of data tables in MySQL: compressed tables and non-compressed tables. Uncompressed tables are MySQL's default table type, which use fixed-length row format to store data. This means data

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

PrimeFactor−Innumbertheory,theprimefactorsofapositiveintegeraretheprimenumbersthatdividethatintegerexactly.Theprocessoffindingthesenumbersiscalledintegerfactorization,orprimefactorization.Example−Primefactorsof288are:288=2x2x2x2x2

As a new star in the mobile phone market this year, Kirin 9000s has attracted much attention. As competition in the smartphone market becomes increasingly fierce, as Huawei's flagship processor, the Kirin series has always attracted much attention. As the latest processor, how does the Kirin 9000s perform? What is its performance and level? The following will evaluate the Kirin 9000s and analyze its advantages and disadvantages from various aspects. First of all, from the perspective of performance, Kirin 9000s is manufactured using an advanced 5nm process and integrates ARM’s latest Co
