doc-exports/docs/ddm/umn/ddm_10_0002.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.9 KiB
HTML

<a name="ddm_10_0002"></a><a name="ddm_10_0002"></a>
<h1 class="topictitle1">MOD_HASH</h1>
<div id="body1589181841215"><div class="section" id="ddm_10_0002__section15760711132611"><h4 class="sectiontitle">Application Scenarios</h4><p id="ddm_10_0002__p18393233102515">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_10_0002__section1877171510217"><h4 class="sectiontitle">Instructions</h4><p id="ddm_10_0002__p82853617353">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_10_0002__section149331275411"><h4 class="sectiontitle">Data Routing</h4><p id="ddm_10_0002__p289717314402">The data route depends on the remainder of the sharding key value divided by database or table shards. If the value is a string, convert the string into a hashed value and calculate the data route based on the value.</p>
<p id="ddm_10_0002__p999715133917">For example, if the sharding key value is <strong id="ddm_10_0002__b26524455119">8</strong>, MOD_HASH('8') is equivalent to 8 % D. D is the number of database or table shards.</p>
</div>
<div class="section" id="ddm_10_0002__section1022216812166"><h4 class="sectiontitle">Calculation Method</h4><p id="ddm_10_0002__p0783998210"><strong id="ddm_10_0002__b131721113606">Method 1: Use an Integer as the Sharding Key</strong></p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="ddm_10_0002__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_10_0002__row209571741719"><th align="left" class="cellrowborder" valign="top" width="16.071607160716074%" id="mcps1.3.4.3.2.4.1.1"><p id="ddm_10_0002__p1173121415175"><strong id="ddm_10_0002__b61601917707">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_10_0002__p151739144179"><strong id="ddm_10_0002__b897316685714">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_10_0002__p317314149177"><strong id="ddm_10_0002__b679526201">Example</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="ddm_10_0002__row49585712177"><td class="cellrowborder" valign="top" width="16.071607160716074%" headers="mcps1.3.4.3.2.4.1.1 "><p id="ddm_10_0002__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_10_0002__p665825216312">Database routing result = Database sharding key value % Database shards</p>
<p id="ddm_10_0002__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_10_0002__p1351617911412">Database shard: 16 % 8 = 0</p>
<p id="ddm_10_0002__p35163918414">Table shard: 16 % 3 = 1</p>
</td>
</tr>
<tr id="ddm_10_0002__row921516171548"><td class="cellrowborder" valign="top" width="16.071607160716074%" headers="mcps1.3.4.3.2.4.1.1 "><p id="ddm_10_0002__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_10_0002__p166805252412">Table routing result = Sharding key value % (Database shards x Table shards)</p>
<p id="ddm_10_0002__p166802251140">Database routing result = Table routing result/Table shards</p>
<div class="note" id="ddm_10_0002__note653241214114"><span class="notetitle"> NOTE: </span><div class="notebody"><p id="ddm_10_0002__p1696019424594">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_10_0002__p14680172514419">Table shard: 16 % (8 x 3) = 16</p>
<p id="ddm_10_0002__p12680122516413">Database shard: 16/3 = 5</p>
</td>
</tr>
</tbody>
</table>
</div>
<p id="ddm_10_0002__p470702910207"><strong id="ddm_10_0002__b77974342011">Method 2: Use a String as the Sharding Key</strong></p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="ddm_10_0002__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_10_0002__row728318263188"><th align="left" class="cellrowborder" valign="top" width="16.301630163016302%" id="mcps1.3.4.5.2.4.1.1"><p id="ddm_10_0002__p20543112161217"><strong id="ddm_10_0002__b123464834517">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_10_0002__p2515143314188"><strong id="ddm_10_0002__b182387202581">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_10_0002__p751513334185"><strong id="ddm_10_0002__b1939692493618">Example</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="ddm_10_0002__row3284172661819"><td class="cellrowborder" valign="top" width="16.301630163016302%" headers="mcps1.3.4.5.2.4.1.1 "><p id="ddm_10_0002__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_10_0002__p17169591557">Database routing result = hash(Database sharding key value) % Database shards</p>
<p id="ddm_10_0002__p81614591353">Table routing result = hash(Table sharding key value) % Table shards</p>
<p id="ddm_10_0002__p4167596518"></p>
</td>
<td class="cellrowborder" valign="top" width="38.753875387538756%" headers="mcps1.3.4.5.2.4.1.3 "><p id="ddm_10_0002__p31635910517">hash('abc') = 'abc'.hashCode()=96354</p>
<p id="ddm_10_0002__p7167595514">Database shard: 96354 % 8 = 2;</p>
<p id="ddm_10_0002__p181610591551">Table shard: 96354 % 3 = 0;</p>
</td>
</tr>
<tr id="ddm_10_0002__row228413268182"><td class="cellrowborder" valign="top" width="16.301630163016302%" headers="mcps1.3.4.5.2.4.1.1 "><p id="ddm_10_0002__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_10_0002__p18161859651">Table routing result = hash(Sharding key value) % (Database shards x Table shards)</p>
<p id="ddm_10_0002__p101615913514">Database routing result = Table routing result/Table shards</p>
<div class="note" id="ddm_10_0002__note178961728143913"><span class="notetitle"> NOTE: </span><div class="notebody"><p id="ddm_10_0002__p138961128183919">Database routing result is rounded off to the nearest integer.</p>
</div></div>
<p id="ddm_10_0002__p9165595513"></p>
</td>
<td class="cellrowborder" valign="top" width="38.753875387538756%" headers="mcps1.3.4.5.2.4.1.3 "><p id="ddm_10_0002__p1817125911517">hash('abc') = 'abc'.hashCode()=96354</p>
<p id="ddm_10_0002__p16178591518">Table shard: 96354 % (8 x 3) = 18</p>
<p id="ddm_10_0002__p017259658">Database shard: 18/3 = 6</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="ddm_10_0002__section83236412181"><h4 class="sectiontitle">Syntax for Creating Tables</h4><ul id="ddm_10_0002__ul68185813591"><li id="ddm_10_0002__li7818158185914">Assume that you use field <strong id="ddm_10_0002__b1236415165011">ID</strong> as the sharding key to shard databases based on MOD_HASH:<pre class="screen" id="ddm_10_0002__screen445814284018">create table mod_hash_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(ID);</pre>
</li></ul>
<ul id="ddm_10_0002__ul16204134333912"><li id="ddm_10_0002__li8205104318394">Assume that you use field <strong id="ddm_10_0002__b161091933185020">ID</strong> as the sharding key to shard databases and tables based on MOD_HASH: <pre class="screen" id="ddm_10_0002__screen45641414173914">create table mod_hash_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(ID) tbpartition by mod_hash(ID) tbpartitions 4;</pre>
</li></ul>
</div>
<div class="section" id="ddm_10_0002__section121681236151812"><h4 class="sectiontitle">Precautions</h4><p id="ddm_10_0002__p656819744817">The MOD_HASH 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>