doc-exports/docs/ddm/umn/ddm_12_0007.html
Hasko, Vladimir a5cc543164 reverting ddm umn to state before last commit in #613
Reviewed-by: Wagner, Fabian <fabian.wagner@t-systems.com>
Co-authored-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-committed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
2024-01-17 13:06:49 +00:00

107 lines
8.7 KiB
HTML

<a name="ddm_12_0007"></a><a name="ddm_12_0007"></a>
<h1 class="topictitle1">MOD_HASH_CI</h1>
<div id="body0000001125387086"><div class="section" id="ddm_12_0007__section166484124619"><h4 class="sectiontitle">Application Scenarios</h4><p id="ddm_12_0007__p106641442466">This algorithm applies if you want to route data to different database shards by user ID or order ID.</p>
</div>
<div class="section" id="ddm_12_0007__section1877171510217"><h4 class="sectiontitle">Instructions</h4><p id="ddm_12_0007__p11800143215222">The sharding key must be CHAR, VARCHAR, INT, INTEGER, BIGINT, MEDIUMINT, SMALLINT, TINYINT, or DECIMAL (the precision can be 0).</p>
</div>
<div class="section" id="ddm_12_0007__section149331275411"><h4 class="sectiontitle">Data Routing</h4><p id="ddm_12_0007__p12780198124511">The data route depends on the remainder of the sharding key value divided by database or table shards. MOD_HASH is case-sensitive, but MOD_HASH_CI is not.</p>
</div>
<div class="section" id="ddm_12_0007__section1022216812166"><h4 class="sectiontitle">Calculation Method</h4><p id="ddm_12_0007__p0783998210"><strong id="ddm_12_0007__b532320229425">Method 1: Use an Integer as the Sharding Key</strong></p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="ddm_12_0007__table39571721711" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Required calculation methods when the sharding key is the integer data type</caption><thead align="left"><tr id="ddm_12_0007__row209571741719"><th align="left" class="cellrowborder" valign="top" width="16.071607160716074%" id="mcps1.3.4.3.2.4.1.1"><p id="ddm_12_0007__p1173121415175"><strong id="ddm_12_0007__b262412812420">Condition</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50.5950595059506%" id="mcps1.3.4.3.2.4.1.2"><p id="ddm_12_0007__p151739144179"><strong id="ddm_12_0007__b1565563016013">Calculation Method</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.3.4.3.2.4.1.3"><p id="ddm_12_0007__p317314149177"><strong id="ddm_12_0007__b152781432194216">Example</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="ddm_12_0007__row49585712177"><td class="cellrowborder" valign="top" width="16.071607160716074%" headers="mcps1.3.4.3.2.4.1.1 "><p id="ddm_12_0007__p2035318193178">Database sharding key ≠ Table sharding key</p>
</td>
<td class="cellrowborder" valign="top" width="50.5950595059506%" headers="mcps1.3.4.3.2.4.1.2 "><p id="ddm_12_0007__p665825216312">Database routing result = Database sharding key value % Database shards</p>
<p id="ddm_12_0007__p96583524319">Table routing result = Table sharding key value % Table shards</p>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.3.4.3.2.4.1.3 "><p id="ddm_12_0007__p1351617911412">Database shard: 16 % 8 = 0</p>
<p id="ddm_12_0007__p35163918414">Table shard: 16 % 3 = 1</p>
</td>
</tr>
<tr id="ddm_12_0007__row921516171548"><td class="cellrowborder" valign="top" width="16.071607160716074%" headers="mcps1.3.4.3.2.4.1.1 "><p id="ddm_12_0007__p668022519420">Database sharding key = Table sharding key</p>
</td>
<td class="cellrowborder" valign="top" width="50.5950595059506%" headers="mcps1.3.4.3.2.4.1.2 "><p id="ddm_12_0007__p166805252412">Table routing result = Sharding key value % (Database shards x Table shards)</p>
<p id="ddm_12_0007__p166802251140">Database routing result = Table routing result/Table shards</p>
<div class="note" id="ddm_12_0007__note1881017143810"><span class="notetitle"> NOTE: </span><div class="notebody"><p id="ddm_12_0007__p581017143816">Database routing result is rounded off to the nearest integer.</p>
</div></div>
</td>
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.3.4.3.2.4.1.3 "><p id="ddm_12_0007__p14680172514419">Table shard: 16 % (8 x 3) = 16</p>
<p id="ddm_12_0007__p12680122516413">Database shard: 16/3 = 5</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="ddm_12_0007__p470702910207"><strong id="ddm_12_0007__b1888202415437">Method 2: Use a String as the Sharding Key</strong></p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="ddm_12_0007__table1228313268181" frame="border" border="1" rules="all"><caption><b>Table 2 </b>Required calculation methods when the sharding key is the string data type</caption><thead align="left"><tr id="ddm_12_0007__row728318263188"><th align="left" class="cellrowborder" valign="top" width="16.301630163016302%" id="mcps1.3.4.5.2.4.1.1"><p id="ddm_12_0007__p20543112161217"><strong id="ddm_12_0007__b56911730164318">Condition</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="44.944494449444946%" id="mcps1.3.4.5.2.4.1.2"><p id="ddm_12_0007__p2515143314188"><strong id="ddm_12_0007__b948612522000">Calculation Method</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="38.753875387538756%" id="mcps1.3.4.5.2.4.1.3"><p id="ddm_12_0007__p751513334185"><strong id="ddm_12_0007__b58901332184317">Example</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="ddm_12_0007__row3284172661819"><td class="cellrowborder" valign="top" width="16.301630163016302%" headers="mcps1.3.4.5.2.4.1.1 "><p id="ddm_12_0007__p0161859050">Database sharding key ≠ Table sharding key</p>
</td>
<td class="cellrowborder" valign="top" width="44.944494449444946%" headers="mcps1.3.4.5.2.4.1.2 "><p id="ddm_12_0007__p17169591557">Database routing result = hash(Database sharding key value) % Database shards</p>
<p id="ddm_12_0007__p81614591353">Table routing result = hash(Table sharding key value) % Table shards</p>
<p id="ddm_12_0007__p4167596518"></p>
</td>
<td class="cellrowborder" valign="top" width="38.753875387538756%" headers="mcps1.3.4.5.2.4.1.3 "><p id="ddm_12_0007__p31635910517"></p>
<p id="ddm_12_0007__p861715231538">hash('abc') = 'abc'.toUpperCase().hashCode()=64578</p>
<p id="ddm_12_0007__p7167595514">Database shard: 64578 % 8 = 2;</p>
<p id="ddm_12_0007__p181610591551">Table shard: 64578 % 3 = 0;</p>
</td>
</tr>
<tr id="ddm_12_0007__row228413268182"><td class="cellrowborder" valign="top" width="16.301630163016302%" headers="mcps1.3.4.5.2.4.1.1 "><p id="ddm_12_0007__p111614592050">Database sharding key = Table sharding key</p>
</td>
<td class="cellrowborder" valign="top" width="44.944494449444946%" headers="mcps1.3.4.5.2.4.1.2 "><p id="ddm_12_0007__p18161859651">Table routing result = hash(Sharding key value) % (Database shards x Table shards)</p>
<p id="ddm_12_0007__p101615913514">Database routing result = Table routing result/Table shards</p>
<div class="note" id="ddm_12_0007__note1484111814400"><span class="notetitle"> NOTE: </span><div class="notebody"><p id="ddm_12_0007__p1696019424594">Database routing result is rounded off to the nearest integer.</p>
</div></div>
<p id="ddm_12_0007__p9165595513"></p>
</td>
<td class="cellrowborder" valign="top" width="38.753875387538756%" headers="mcps1.3.4.5.2.4.1.3 "><p id="ddm_12_0007__p1883731010547">hash('abc') = 'abc'.toUpperCase().hashCode()=64578</p>
<p id="ddm_12_0007__p16178591518">Table shard: 64578 % (8 x 3) = 18</p>
<p id="ddm_12_0007__p017259658">Database shard: 18/3 = 6</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="ddm_12_0007__section83236412181"><h4 class="sectiontitle">Syntax for Creating Tables</h4><ul id="ddm_12_0007__ul974373119551"><li id="ddm_12_0007__li47436311559">Assume that you use field <strong id="ddm_12_0007__b63809441622">ID</strong> as the sharding key to shard databases based on MOD_HASH_CI: <pre class="screen" id="ddm_12_0007__screen17391182006">create table mod_hash_ci_tb(
id int,
name varchar(30) DEFAULT NULL,
create_time datetime DEFAULT NULL,
primary key(id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 dbpartition by mod_hash_ci(id);</pre>
</li><li id="ddm_12_0007__li7726450115617">Assume that you use field <strong id="ddm_12_0007__b48911451351">ID</strong> as the sharding key to shard databases and tables based on MOD_HASH_CI: <pre class="screen" id="ddm_12_0007__screen1358911271506">create table mod_hash_ci_tb(
id int,
name varchar(30) DEFAULT NULL,
create_time datetime DEFAULT NULL,
primary key(id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8
dbpartition by mod_hash_ci(id)
tbpartition by mod_hash_ci(id) tbpartitions 4;</pre>
</li></ul>
</div>
<div class="section" id="ddm_12_0007__section121681236151812"><h4 class="sectiontitle">Precautions</h4><p id="ddm_12_0007__p1491563915505">The MOD_HASH_CI algorithm is a simple way to find the remainder of the sharding key value divided by shards. This algorithm features even distribution of sharding key values to ensure even results.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="ddm_10_0018.html">Sharding Algorithms</a></div>
</div>
</div>