シャーディング-JDBC:単一のライブラリパーティションテーブルを達成するために

物語を想起

以前、我々は完全に分離、垂直分割、垂直分割+読み取りおよび書き込み分離を読み書きすることを学びます。次のように記事対応しています。

シャーディング-JDBC:どのように大規模なクエリの最適化?

シャーディング-JDBC:どのように垂直分割を行いますか?

上記の最適化を通じて、我々はほとんどのニーズを満たすことができました。我々は再び最適化する必要がある唯一の場合は、その1つのテーブルの数の急激な増加で、100万人以上の以上、今回はそれが水平にテーブルを分割する必要があります。

レベル分割表とは何ですか?

テーブルには大きな石が移動しないと同じように、Nにテーブルを分割し、その後、このような転送を移動することができ、10カットされます。原理は同じです。

圧力バランスの数に加えて、だけでなく、圧力の読み取りおよび書き込み要求を広めるために、もちろん、これはあなたのスライスアルゴリズムに依存し、このアルゴリズムは、データの合理的に均一な分布を作成し、パフォーマンスを向上させることが可能です。

今日、私たちは主につまり、関係なく、ライブラリーの、唯一のテーブルを指して、単一のデータベース内のテーブルを分割について話しています。

戻る操作は、テーブルを繰り返し、最初のサブテーブルが絵を感じることはないサブライブラリーに分けることができます。

分類されていないテーブル

その後、再び次の図は、経験値テーブルを持っています:

サブテーブルされています

我々は、図から見ることができ、元の表から、ユーザが4つに分割され、データが均等にすなわち元のユーザー=のUser0 + USER1 + USER2 + USER3、3つのテーブルに分散されます。

ポイントテーブルの設定

まず、次のように、4つのユーザ・テーブルを作成する必要があります。

CREATE TABLE `user_0`(
    id bigint(64) not null,
    city varchar(20) not null,
    name varchar(20) not null,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_1`(
    id bigint(64) not null,
    city varchar(20) not null,
    name varchar(20) not null,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_2`(
    id bigint(64) not null,
    city varchar(20) not null,
    name varchar(20) not null,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_3`(
    id bigint(64) not null,
    city varchar(20) not null,
    name varchar(20) not null,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

サブテーブルの数は、あなたが成長し、今後数年間で持っているデータの量に基づいて評価する必要があります。

ルールサブテーブルの構成:

spring.shardingsphere.datasource.names=master

# 数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456

# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=master.user_${0..3}

# inline 表达式
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}
  • 実際のデータ・ノード
    master.user_0に変換設定ポイントテーブル情報、ここで使用されるインライン式、master.user_1、master.user_2、master.user_3
  • カラムinline.sharding
    側部テーブルIDと、部品表のフィールドを
  • inline.algorithm発現
    部品表の行演算式文法グルービー満たす必要があり、上記構成は、モジュロフラグメントIDを使用することです

私たちは、断片化のより複雑なニーズを持っている場合は、断片化が達成するために、アルゴリズムをカスタマイズすることができます。

# 自定义分表算法
spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm

アルゴリズムのカテゴリ:

public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        for (String tableName : availableTargetNames) {
            if (tableName.endsWith(shardingValue.getValue() % 4 + "")) {
                return tableName;
            }
        }
        throw new IllegalArgumentException();
    }

}

doSharding方法では、最終的には、このデータテーブル名が断片化することができる返す、パラメータshardingValueに基づいて何らかの処理を行うことができます。

単一のフィールドの断片化に加えて、断片化はまた、あなたが自分でどのような文書操作のために見ることができ、複数列をサポートしています。

パーツテーブル構成、何の構成データベースの操作は、コード行を変更しませんがないパーツテーブルする必要があります。

我々は、サブテーブルに基づいて単一のライブラリをしたい場合は、同じことが次のように限り、ソースから構成データを構成し、することができますように、非常に簡単です、別の読み取りを行うと、書き込み:

spring.shardingsphere.datasource.names=master,slave

# 主数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456

# 从数据源
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456

# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_${0..3}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}

# 读写分离配置
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave

単一のリザーバ別々の読み取りと書き込みのサブテーブル

遂に

あなたは最終的にフレームワークと、この複雑なサブテーブルのシーンは非常に簡単な解決することがわかります。少なくとも、このフォームのクエリを集約するために、テーブルよりも、フィールドを独自のルートを計算するためにはるかに優れています。

ソース参照:https://github.com/yinjihuan/sharding-jdbc

次のああを集中することを忘れないように良い感じ、それにスターを与えます!

技術の交流と一緒に、私の知識の惑星へようこそ、無料の学習コースの猿の世界(http://cxytiandi.com/course)

PS:現在のチームのリーダーシップの下であなたを待ってああ、Xingzhuている春の雲の惑星を学ぶために!

マイクロチャンネルスキャンコードは、知識惑星の猿の世界に参加します

猿の世界

おすすめ

転載: www.cnblogs.com/yinjihuan/p/11262559.html