欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL數(shù)據(jù)庫優(yōu)化之分表分庫操作實例詳解

 更新時間:2020年01月19日 11:30:07   作者:小飛俠v科比  
這篇文章主要介紹了MySQL數(shù)據(jù)庫優(yōu)化之分表分庫操作,結(jié)合實例形式詳細(xì)分析了mysql數(shù)據(jù)庫分表分庫垂直拆分、水平拆分相關(guān)原理以及應(yīng)用案例,需要的朋友可以參考下

本文實例講述了MySQL數(shù)據(jù)庫優(yōu)化之分表分庫操作。分享給大家供大家參考,具體如下:

分表分庫

垂直拆分

垂直拆分就是要把表按模塊劃分到不同數(shù)據(jù)庫表中(當(dāng)然原則還是不破壞第三范式),這種拆分在大型網(wǎng)站的演變過程中是很常見的。當(dāng)一個網(wǎng)站還在很小的時候,只有小量的人來開發(fā)和維護(hù),各模塊和表都在一起,當(dāng)網(wǎng)站不斷豐富和壯大的時候,也會變成多個子系統(tǒng)來支撐,這時就有按模塊和功能把表劃分出來的需求。其實,相對于垂直切分更進(jìn)一步的是服務(wù)化改造,說得簡單就是要把原來強(qiáng)耦合的系統(tǒng)拆分成多個弱耦合的服務(wù),通過服務(wù)間的調(diào)用來滿足業(yè)務(wù)需求看,因此表拆出來后要通過服務(wù)的形式暴露出去,而不是直接調(diào)用不同模塊的表,淘寶在架構(gòu)不斷演變過程,最重要的一環(huán)就是服務(wù)化改造,把用戶、交易、店鋪、寶貝這些核心的概念抽取成獨立的服務(wù),也非常有利于進(jìn)行局部的優(yōu)化和治理,保障核心模塊的穩(wěn)定性
垂直拆分用于分布式場景。

水平拆分

上面談到垂直切分只是把表按模塊劃分到不同數(shù)據(jù)庫,但沒有解決單表大數(shù)據(jù)量的問題,而水平切分就是要把一個表按照某種規(guī)則把數(shù)據(jù)劃分到不同表或數(shù)據(jù)庫里。例如像計費系統(tǒng),通過按時間來劃分表就比較合適,因為系統(tǒng)都是處理某一時間段的數(shù)據(jù)。而像SaaS應(yīng)用,通過按用戶維度來劃分?jǐn)?shù)據(jù)比較合適,因為用戶與用戶之間的隔離的,一般不存在處理多個用戶數(shù)據(jù)的情況,簡單的按user_id范圍來水平切分
通俗理解:水平拆分行,行數(shù)據(jù)拆分到不同表中, 垂直拆分列,表數(shù)據(jù)拆分到不同表中

水平分割案例

思路:在大型電商系統(tǒng)中,每天的會員人數(shù)不斷的增加。達(dá)到一定瓶頸后如何優(yōu)化查詢。
可能大家會想到索引,萬一用戶量達(dá)到上億級別,如何進(jìn)行優(yōu)化呢?
使用水平分割拆分?jǐn)?shù)據(jù)庫表。

如何使用水平拆分?jǐn)?shù)據(jù)庫

使用水平分割拆分表,具體根據(jù)業(yè)務(wù)需求,有的按照注冊時間、取摸、賬號規(guī)則、年份等。

使用取摸方式分表

首先我創(chuàng)建三張表 user0 / user1 /user2 , 然后我再創(chuàng)建 uuid表,該表的作用就是提供自增的id。

create table user0(
id int unsigned primary key ,
name varchar(32) not null default '',
pwd varchar(32) not null default '')
engine=myisam charset utf8;
create table user1(
id int unsigned primary key ,
name varchar(32) not null default '',
pwd varchar(32) not null default '')
engine=myisam charset utf8;
create table user2(
id int unsigned primary key ,
name varchar(32) not null default '',
pwd varchar(32) not null default '')
engine=myisam charset utf8;
create table uuid(
id int unsigned primary key auto_increment)engine=myisam charset utf8;

創(chuàng)建一個demo項目

POM文件

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

Service代碼

@Service
public class UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public String regit(String name, String pwd) {
        // 1.先獲取到 自定增長ID
        String idInsertSQL = "INSERT INTO uuid VALUES (NULL);";
        jdbcTemplate.update(idInsertSQL);
        Long insertId = jdbcTemplate.queryForObject("select last_insert_id()", Long.class);
        // 2.判斷存儲表名稱
        String tableName = "user" + insertId % 3;
        // 3.注冊數(shù)據(jù)
        String insertUserSql = "INSERT INTO " + tableName + " VALUES ('" + insertId + "','" + name + "','" + pwd
                + "');";
        System.out.println("insertUserSql:" + insertUserSql);
        jdbcTemplate.update(insertUserSql);
        return "success";
    }
    public String get(Long id) {
        String tableName = "user" + id % 3;
        String sql = "select name from " + tableName + " where id="+id;
        System.out.println("SQL:" + sql);
        String name = jdbcTemplate.queryForObject(sql, String.class);
        return name;
    }
}

Controller

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/regit")
    public String regit(String name, String pwd) {
        return userService.regit(name, pwd);
    }
    @RequestMapping("/get")
    public String get(Long id) {
        String name = userService.get(id);
        return name;
    }
}

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總

希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。

相關(guān)文章

最新評論