Practice under the partition table RDS - MySQL

  Practical background
  
  projects and some table space is too big, and too many lines, it was decided to table a number of sub-library sub-table. Further Study Selection program, we noticed some common sub-library sub-table solutions for business more code changes, it was decided to adopt MySQL partitioning scheme.
  
  In fact, in my personal opinion, the partition table is MySQL help us to achieve the underlying sub-library sub-table, need not involve modification of business codes, distributed transaction need not be concerned. Because in terms of access to the database, logic, or only a table, but in fact does have multiple physical partitions objects, queries the specific partitions based on specific zoning rules.
  
  Introduce the practice table, table space 172G, 1 120 million records.
  
  Database version: RDS MySQL 5.6
  
  tool: Ali cloud DTS
  
  First, why partition?
  
  Advantages:
  
  for expired or no saved data, you can quickly delete data by deleting data related to these partitions, it is more efficient than DELETE high
  
  when the partition containing the conditions necessary to scan only one in the where clause or a plurality of partitions to improve query efficiency
  
  , for example, the following statement:
  
  the sELECT * the pARTITION the FROM T (p0, p1) WHERE C <. 5 selects only the records that match the criteria WHERE partition p0 and p1 are
  
  directed to the aggregate function SUM (), COUNT () of when the query will be processed in parallel on each partition
  
  partition originally a data table is stored on multiple physical disks to achieve higher IOPS
  
  Cons:
  
  can not use foreign keys, do not support full-text indexing (now no company should also with a foreign key, right)
  
  partition key design is not flexible, if you do not take the partition key, it is prone to the whole table lock
  
  developers to write a SQL, mysql is not clear how to play
  
  Two, RANGE partitioning
  
  currently supports MySQL range partitioning (RANGE), list partitioning (LIST), hash partitioning (HASH) and KEY partitions four.
  
  This article is based on range partitioning (RANGE) partition of time, so I will briefly explain RANGE partitions. See more partition type partition type of official documents MySQL 5.6
  
  on a given column value continuation interval, according to the interval assigned partition. The most common is based on the time field. In fact, based on the partition of the integer column it is best if you can use the date type function converted to an integer. MySQL 5.6 Supported partition function
  
  used in this example TO_DAYS function of
  
  the CREATE TABLE Members (
  
  ID VARCHAR (25) the NOT NULL,
  
  FirstName VARCHAR (25) the NOT NULL,
  
  LastName VARCHAR (25) the NOT NULL,
  
  username VARCHAR (16) the NOT NULL,
  
  In Email VARCHAR (35),
  
  joindate DATETIME the NOT NULL the DEFAULT '0000-00-00 00:00:00',
  
  a PRIMARY KEY (ID, joindate) the USING BTREE,
  
  KEY idx_joindate (joindate) the USING BTREE
  
  ) ENGINE = the InnoDB the DEFAULT the CHARSET = UTF8 the ROW_FORMAT the COMPACT =
  
  PARTITION BY RANGE (TO_DAYS (joindate) ) (
  
  P0 the VALUES LESS THAN the PARTITION (TO_DAYS ( '1960-01-01')),
  
  the PARTITION P1 the VALUES LESS THAN (TO_DAYS ( '1970-01-01')),
  
  the PARTITION P2 the VALUES LESS THAN (TO_DAYS ( '1980-01-01 ')),
  
  the pARTITION P3 the VALUES LESS THAN (TO_DAYS (' 1990-01-01 ')),
  
  the pARTITION the VALUES LESS THAN MAXVALUE P4
  
  );
  
  the PS: If you have a primary key or a unique index, you have to put your partitions like the examples also add key, which is joindate partitioning key, or create will fail!
  
  PS: as above plus LESS THAN MAXVALUE, you can not later add a new partition! ! !
  
  Example: The
  
  following query will fall on the index partition p2 defined. Therefore, when the query key will bring down your partition corresponding to partition the data query, if your condition across a plurality of partitions aggregate function SUM (), COUNT () queries, it will be processed in parallel on each partition . If you did not take full table partition key query will query.
  
  <Border BorderBrush = "{TemplateBinding BorderBrush }" BorderThickness = "{TemplateBinding BorderThickness}" Background = "{TemplateBinding Background}" CornerRadius = "3"
  
  <VisualStateManager.VisualStateGroups>
  
  <VisualStateGroup x:Name="ExpansionStates">
  
  <VisualStateGroup.Transitions>
  
  <VisualTransition GeneratedDuration="0:0:0.3">
  
  <VisualTransition.GeneratedEasingFunction>
  
  <QuarticEase EasingMode="EaseOut"/>
  
  </VisualTransition.GeneratedEasingFunction>
  
  </VisualTransition>
  
  </VisualStateGroup.Transitions>
  
  <VisualState x:Name="Expanded"/>
  
  <VisualState x:Name="Collapsed">
  
  <Storyboard>
  
  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ExpandableContentControl">
  
  <EasingDoubleKeyFrame KeyTime="www.chenghylpt.com" Value="0"/>
  
  </DoubleAnimationUsingKeyFrames>
  
  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Percentage" Storyboard.TargetName="ExpandableContentControl">
  
  <EasingDoubleKeyFrame KeyTime="www.renheyL.com" Value="0"/>
  
  </DoubleAnimationUsingKeyFrames>
  
  </Storyboard>
  
  </VisualState>
  
  </VisualStateGroup>
  
  </VisualStateManager.VisualStateGroups>
  
  <DockPanel>
  
  <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment=www.feironggw.cn"{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}www.ztylegw.cn" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" Style="{StaticResource ExpanderDownHeaderStyle}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
  
  <Primitives:ExpandableContentControl x:Name="ExpandableContentControl" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"www.yifa5yl.com VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
  
  Margin="{TemplateBinding Padding}" ClipToBounds="True"www.zbzxyL12.com>
  
  <ContentPresenter x:Name=www.chengmingdl.com"ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
  
  </Primitives:ExpandableContentControl>
  
  </DockPanel>
  
  </Border>
  
  <ControlTemplate.Triggers>
  
  <Trigger Property="IsExpanded" Value="false">
  
  <Setter Property = "IsHitTestVisible" TargetName = "ExpandableContentControl" Value = "False" />
  
  </ the Trigger>
  
  ...
  
  </ControlTemplate.Triggers>
  
  When I finished migrating data query found a particularly interesting phenomenon, with a If the SQL time interval partitioning key is not the same, it will be less to go a different range of indices based on the number of rows rows. As I have not studied it to the bottom of how to achieve
  
  Third, partition management
  
  brief introduction to the scope of partitions, and then say something about the partition of common operations.
  
  Partition Manager includes an increase for the partition, delete, and query. More detailed partition management MySQL official documentation
  
  1. Increase the partition
  
  to RANGE and LIST partitions:
  
  the ALTER the Table table_name the Add Partition (Partition P0 values ... (exp))
  
  # example
  
  ALTER TABLE members ADD PARTITION (TO_DAYS ( www.hengxyul.com '2021-03-01'));
  
  2. delete partition
  
  deleted partition, it will also delete all the data in the partition. If you delete a partition lead to a partition can not cover all the value, then insert the data when error.
  
  Partition P0 the Table table_name drop the ALTER;
  
  3. Discover how many partitions
  
  SELECT * FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = ' members';
  
  Four, Data Migration
  
  said in front of so many concepts, I talk about this large table to migrate data into the partition table.
  
  DTS Why did you choose it? Because it can be non-stop migration of data to support the full amount of migration and incremental migration, it has little effect on the original table.
  
  The migration process is as follows:
  
  First, in the same instance of RDS built inside with a partition table structure
  
  to create a migration task using DTS, do not choose when to migrate structure Qian, select only the full amount + incremental migration
  
  then need to edit the next target library table name, which is done under the migration mapping A-> B from
  
  turning off the task of writing data, when the task queue is empty, wait a few minutes to pause and end of the migration task
  
  last modified table name, data migration and switching (I in the test environment to modify the partition table for some time, but RDS modify the table name is changed in seconds)
  
  refer to the official document: MySQL 5.6 partitions
  
  than purely personal point of view, if not please correct me.

Guess you like

Origin www.cnblogs.com/qwangxiao/p/11241627.html