forked from docs/doc-exports
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>
76 lines
6.4 KiB
HTML
76 lines
6.4 KiB
HTML
<a name="ddm_10_0006"></a><a name="ddm_10_0006"></a>
|
|
|
|
<h1 class="topictitle1">YYYYMM</h1>
|
|
<div id="body1589181841219"><div class="section" id="ddm_10_0006__section15760711132611"><h4 class="sectiontitle">Application Scenarios</h4><p id="ddm_10_0006__p18393233102515">This algorithm applies when data is routed to shards by year and month. You are advised to use this algorithm together with tbpartition YYYYMM(ShardKey).</p>
|
|
</div>
|
|
<div class="section" id="ddm_10_0006__section1877171510217"><h4 class="sectiontitle">Instructions</h4><p id="ddm_10_0006__p18486174419615">The sharding key must be DATE, DATETIME, or TIMESTAMP.</p>
|
|
</div>
|
|
<div class="section" id="ddm_10_0006__section149331275411"><h4 class="sectiontitle">Data Routing</h4><p id="ddm_10_0006__p13746248153618">The data route depends on the remainder of the sharding key hash value divided by database shards. Enter the year and month into the hash function to obtain the hash value.</p>
|
|
<p id="ddm_10_0006__p8916269170">For example, YYYYMM ('2012-12-31 12:12:12') is equivalent to (2012 x 12 + 12) % D. D is the number of database or table shards.</p>
|
|
</div>
|
|
<div class="section" id="ddm_10_0006__section19685419351"><h4 class="sectiontitle">Calculation Method</h4>
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="ddm_10_0006__table39571721711" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Required calculation methods</caption><thead align="left"><tr id="ddm_10_0006__row209571741719"><th align="left" class="cellrowborder" valign="top" width="18.81188118811881%" id="mcps1.3.4.2.2.4.1.1"><p id="ddm_10_0006__p1627712131471"><strong id="ddm_10_0006__b699219494544">Condition</strong></p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="34.65346534653465%" id="mcps1.3.4.2.2.4.1.2"><p id="ddm_10_0006__p1322019321215"><strong id="ddm_10_0006__b113441757171913">Calculation Method</strong></p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="46.53465346534653%" id="mcps1.3.4.2.2.4.1.3"><p id="ddm_10_0006__p1277913194714"><strong id="ddm_10_0006__b1498165295411">Example</strong></p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="ddm_10_0006__row49585712177"><td class="cellrowborder" valign="top" width="18.81188118811881%" headers="mcps1.3.4.2.2.4.1.1 "><p id="ddm_10_0006__p22771713124712">Database sharding key ≠ Table sharding key</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="34.65346534653465%" headers="mcps1.3.4.2.2.4.1.2 "><p id="ddm_10_0006__p1280101374720">Sharding key: yyyy-MM-dd</p>
|
|
<p id="ddm_10_0006__p72801313144716">Database routing result = (yyyy x 12 + MM) % Database shards</p>
|
|
<p id="ddm_10_0006__p1228081344711">Table routing result = (yyyy x 12 + MM) % Table shards</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="46.53465346534653%" headers="mcps1.3.4.2.2.4.1.3 "><p id="ddm_10_0006__p1228041311479">Sharding key: 2012-11-20</p>
|
|
<p id="ddm_10_0006__p1428111138476">Database shard: (2012 x 12 + 11) % 8 = 3</p>
|
|
<p id="ddm_10_0006__p328161318476">Table shard: (2012 x 12 + 11) % 3 = 2</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="ddm_10_0006__row921516171548"><td class="cellrowborder" valign="top" width="18.81188118811881%" headers="mcps1.3.4.2.2.4.1.1 "><p id="ddm_10_0006__p8281181324710">Database sharding key = Table sharding key</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="34.65346534653465%" headers="mcps1.3.4.2.2.4.1.2 "><p id="ddm_10_0006__p928181394716">Sharding key: yyyy-MM-dd</p>
|
|
<p id="ddm_10_0006__p1328131311477">Table routing result = (yyyy x 12 + MM) % (Database shards x Table shards)</p>
|
|
<p id="ddm_10_0006__p228161315475">Database routing result = Table routing result/Table shards</p>
|
|
<div class="note" id="ddm_10_0006__note653241214114"><span class="notetitle"> NOTE: </span><div class="notebody"><p id="ddm_10_0006__p1696019424594">Database routing result is rounded off to the nearest integer.</p>
|
|
</div></div>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="46.53465346534653%" headers="mcps1.3.4.2.2.4.1.3 "><p id="ddm_10_0006__p428118137477">Sharding key: 2012-11-20</p>
|
|
<p id="ddm_10_0006__p1281141334715">Table shard: (2012 x 12 + 11) % (8 x 3) = 11</p>
|
|
<p id="ddm_10_0006__p202811913124713">Database shard: 11 % 3 = 3</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="ddm_10_0006__section83236412181"><h4 class="sectiontitle">Syntax for Creating Tables</h4><p id="ddm_10_0006__p9417545002">Assume that there are already 8 physical databases in your database instance. Now you want to shard data by year and month and require that data of the same month be stored in one table and each month within two years should correspond to an independent table, so that you can query data from a physical table in a physical database by the sharding key.</p>
|
|
<p id="ddm_10_0006__p6653157357">In this scenario, you can select the YYYYMM algorithm. Then create 24 physical tables for 24 months of the two years, each month corresponding to one table. Since you already have 8 physical databases, three physical tables should be created in each of them. The following is an example SQL statement for creating a table:</p>
|
|
<pre class="screen" id="ddm_10_0006__screen550211349108">create table test_yyyymm_tb(
|
|
id int,
|
|
name varchar(30) DEFAULT NULL,
|
|
create_time datetime DEFAULT NULL,
|
|
primary key(id)
|
|
) ENGINE = InnoDB DEFAULT CHARSET = utf8
|
|
dbpartition by YYYYMM(create_time)
|
|
tbpartition by YYYYMM(create_time) tbpartitions 3;</pre>
|
|
<p id="ddm_10_0006__p38851556161118">Syntax for creating tables when only database sharding is required:</p>
|
|
<pre class="screen" id="ddm_10_0006__screen788884611114">create table YYYYMM(
|
|
id int,
|
|
name varchar(30) DEFAULT NULL,
|
|
create_time datetime DEFAULT NULL,
|
|
primary key(id)
|
|
) ENGINE = InnoDB DEFAULT CHARSET = utf8
|
|
dbpartition by YYYYMM(create_time);</pre>
|
|
</div>
|
|
<div class="section" id="ddm_10_0006__section121681236151812"><h4 class="sectiontitle">Precautions</h4><ul id="ddm_10_0006__ul9723738240"><li id="ddm_10_0006__li13723731246">This YYYYMM algorithm does not apply if each month of a year corresponds to one database shard. The number of tables must be fixed if database and table sharding is both required.</li><li id="ddm_10_0006__li1822101592519">Data of the same month in different years may be routed to the same database or table. The result depends on the number of tables.</li></ul>
|
|
</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>
|
|
|