Home > Database > Mysql Tutorial > body text

Code examples that explain in detail the limitations and limitations of MySQL partition tables

黄舟
Release: 2017-03-17 14:02:58
Original
1691 people have browsed it

This article summarizes the limitations of Mysql partition tables. Due to personal abilities and test environment, there may be errors. Please point them out in time. Of course you are interested. Friends can check it out on the official website.

Building prohibited

Partition expressions do not support the following types of constructions:

Stored procedures, stored functions , UDFS or plug-in

Declare variables or user variables

You can refer to SQL functions not supported by partitions

Arithmetic and logical operators

Partition expression supports +, -, * arithmetic operations, but does not support p and / operations (it still exists, please check Bug #30188, Bug #33182). However, the result must be an integer or NULL (except for linear partition keys, for more information, see Partition Types).

Partition expressions do not support bit operations: |, &, ^, <<, >>, ~ .

HANDLER statement

Partitioned tables before MySQL 5.7.1 do not support the HANDLER statement. This restriction is removed in later versions.

Server SQL mode

If you want to use a user-defined partitioned table, it should be noted that the SQL mode when creating a partitioned table is not reserved. As discussed in the Server SQL Mode chapter, the results of most MySQL functions and operators may change depending on the server SQL mode. Therefore, once the SQL schema is changed after creating partitioned tables, it may cause significant changes in the behavior of these tables, which can easily lead to data loss or damage. For the above reasons, it is strongly recommended that you never modify the server's SQL mode after creating a partition table.

Give an example to illustrate the above situation:

1.Error handling

mysql> CREATE TABLE tn (c1 INT)
  ->    PARTITION BY LIST(1 p c1) (
  ->    PARTITION p0 VALUES IN (NULL),
  ->    PARTITION p1 VALUES IN (1)
  -> );
  Query OK, 0 rows affected (0.05 sec)
Copy after login

The default result of MySQL divided by 0 is NULL, not Error:

mysql> SELECT @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|      |
+------------+
1 row in set (0.00 sec)
 
mysql> INSERT INTO tn VALUES (NULL), (0), (1);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
Copy after login

However, if we modify the SQL schema, an error will be reported:

mysql> SET sql_mode=&#39;STRICT_ALL_TABLES,ERROR_FOR_pISION_BY_ZERO&#39;;
Query OK, 0 rows affected (0.00 sec)
 
mysql> INSERT INTO tn VALUES (NULL), (0), (1);
ERROR 1365 (22012): pision by 0
Copy after login

2. Table auxiliary function

Sometimes the SQL schema is modified May cause the partition table to become unavailable. For example, some tables only work when the SQL mode is NO_UNSIGNED_SUBTRACTION, such as:

mysql> SELECT @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|      |
+------------+
1 row in set (0.00 sec)
 
mysql> CREATE TABLE tu (c1 BIGINT UNSIGNED)
  ->   PARTITION BY RANGE(c1 - 10) (
  ->   PARTITION p0 VALUES LESS THAN (-5),
  ->   PARTITION p1 VALUES LESS THAN (0),
  ->   PARTITION p2 VALUES LESS THAN (5),
  ->   PARTITION p3 VALUES LESS THAN (10),
  ->   PARTITION p4 VALUES LESS THAN (MAXVALUE)
  -> );
ERROR 1563 (HY000): Partition constant is out of partition function domain
   
mysql> SET sql_mode=&#39;NO_UNSIGNED_SUBTRACTION&#39;;
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT @@sql_mode;
+-------------------------+
| @@sql_mode       |
+-------------------------+
| NO_UNSIGNED_SUBTRACTION |
+-------------------------+
1 row in set (0.00 sec)
 
mysql> CREATE TABLE tu (c1 BIGINT UNSIGNED)
  ->   PARTITION BY RANGE(c1 - 10) (
  ->   PARTITION p0 VALUES LESS THAN (-5),
  ->   PARTITION p1 VALUES LESS THAN (0),
  ->   PARTITION p2 VALUES LESS THAN (5),
  ->   PARTITION p3 VALUES LESS THAN (10),
  ->   PARTITION p4 VALUES LESS THAN (MAXVALUE)
  -> );
   
Query OK, 0 rows affected (0.05 sec)
Copy after login

If you modify the SQL mode after creating the tu, you may no longer be able to access the table:

mysql> SET sql_mode=&#39;&#39;;Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT * FROM tu;
ERROR 1563 (HY000): Partition constant is out of partition function domain
 
mysql> INSERT INTO tu VALUES (20);
ERROR 1563 (HY000): Partition constant is out of partition function domain
Copy after login

Server-side SQL mode also affects the replication of partitioned tables. Using different SQL modes between the primary and secondary servers may result in different execution results of partition expressions on the primary and secondary servers (and switching between primary and secondary servers in Alibaba is a normal operation); this will also lead to different partition expressions during the primary and secondary replication processes. The data distribution in the interval is different; it may also cause the partition table insert on the primary database to succeed but fail on the standby database. Based on the above situation, the best solution is to ensure that the SQL modes between the primary and secondary servers are consistent (this is what the DBA needs to pay attention to during the operation and maintenance process).

PerformanceNotes

The following are some factors that will affect the performance of partition operations:

File system Operations
Partitioning or repartitioning (such as ALTER TABLE ...PARTITION BY ..., REORGANIZE PARTITION, or REMOVE PARTITIONING) operations depend on the file system implementation. This means that the above operations will be affected by the operating system, such as: the type and characteristics of the file system, disk speed, swap space, file processingefficiency on the operating system, and file handle-related issues on the MySQL server. Options, variables and other factors affect. It should be noted that you need to ensure that large_files_support is enabled and the open_files_limit setting is reasonable. For the partitioned table of the MyISAM engine, you need to increase myisam_max_sort_file_size to improve performance; for the InnoDB table, the partitioning or repartitioning operation will be faster through enabled innodb_file_per_table.

You can also refer to the maximum number of partitions.

MyISAM and partition file descriptors

For MyISAM partitioned tables, MySQL uses two file descriptors for each open table and partition. This means that performing operations on a MyISAM partitioned table (especially ALTER TABLE operations) requires more file descriptors than the same table without partitions.

Suppose we want to create a MyISAM table with 100 partitions. The statement is as follows:

CREATE TABLE t (c1 VARCHAR(50))
PARTITION BY KEY (c1) PARTITIONS 100
ENGINE=MYISAM;
Copy after login

To put it simply, in this example, although we use the KEY partition, there is a problem with the file descriptor , will be encountered in all partitions using Table EngineMyISAM, regardless of the partition type. But partitioned tables using other storage engines (such as InnoDB) do not have this problem.

Suppose you want to repartition t and want it to have 101 partitions, use the following statement:

ALTER TABLE t PARTITION BY KEY (c1) PARTITIONS 101;

如果要处理ALTER TABLE语句需要402个文件描述符,原来100个分区*2个+101个新分区*2。这是因为在重新组织表数据时,必须打开所有的(新旧)分区。所以建议在执行这些操作时,要确保--open-files-limit要设置的大些。

表锁

对表执行分区操作的进程会占用表的写锁,不影响读,例如在这些分区上的INSERT和UPDATE操作只有在分区操作完成后才能执行。

存储引擎

分区操作,比如查询,和更新操作通常情况下用MyISAM引擎要比InnoDB和NDB快。

索引;分区修剪

分区表和非分区表一样,合理的利用索引可以显著地提升查询速度。另外,设计分区表以及在这些表上的查询,可以利用分区修剪来显著提升性能。

在MySQL 5.7.3版本之前,分区表不支持索引条件下推,之后的版本可以支持了。

load data性能

在MySQL 5.7,load data 使用buffer提高性能。你需要知道的是buffer会占用每个分区的130KB来达到这个目的。

分区的最大个数

如果不是用NDB作为存储引擎的分区表,支持分区(这里子分区也包含在内)最大个数是8192。

如果使用NDB作为存储引擎的用户自定义分区的最大分区个数,取决于MySQL Cluster的版本, 数据节点和其他因素。

如果你创建一个非常多(比最大分区数要少)的分区时,遇到诸如Got error ... from storage engine: Out of resources when opening file类的错误,你可能需要增加open_files_limit。但是open_files_limit其实也依赖操作系统,可能不是所有的平台都可以建议调整。还有一些其他情况,不建议使用巨大或者成百上千个分区,所以使用越来越多的分区并不见得能带来好结果。

不支持Query cache

分区表不支持query cache,在分区表的查询中自动避开了query cache。也就是说在分区表的查询语句中query cache是不起作用的。

每个分区一个key caches

在MySQL 5.7版本中,可以通过CACHE INDEX和LOAD INDEX INTO CACHE来使用MyISAM分区表的key cache。可以为一个,几个或者所有分区都定义key cache,这样可以把一个,几个或者所有分区的索引预加载到key cache中。

不支持InnoDB分区表的外键

使用InnoDB引擎的分区表不支持外键。下面的两种具体情况来阐述:

在InnoDB表不能使用包含有外键的自定义分区;如果已经使用了外键的InnoDB表,则不能被分区。

InnoDB表不能包含一个和用户自定义分区表相关的外键;使用了用户自定义分区的InnoDB表,不能包含和外键相关的列。

刚刚列出的限制的范围包括使用InnoDB存储引擎的所有表。违反这些限制的CREATE TABLE和ALTER TABLE语句是不被允许的。

ALTER TABLE ... ORDER BY

如果在分区表上执行ALTER TABLE ... ORDER BY的话,会导致每个分区的行排序。

REPLACE语句在修改primary key上的效率

在某些情况下是需要修改表的primary key的,如果你的应用程序使用了REPLACE语句,这些语句的结果可能会被大幅度修改。

全文索引

分区表不支持全文索引或者搜索,即使分区表的存储引擎是InnoDB或者MyISAM也不行。

空间列

分区表不支持空间列,比如点或者几何。

临时表

不能对临时表进行分区(Bug #17497)。

日志表

不能对日志表进行分区,如果强制执行ALTER TABLE ... PARTITION BY ... 语句会报错。

分区键的数据类型

分区键必须是整形或者结果是整形的表达式。不能用结果为ENUM类型的表达式。因为这种类型的表达式可能是NULL。

下面两种情况是例外的:

当用LINER分区时,可以使用除TEXT或者BLOBS以外的数据类型作为分区键,因为MySQL内部的 hash函数会从这些列中产生正确的数据类型。例如,下面的创建语句是合法的:

CREATE TABLE tkc (c1 CHAR)
PARTITION BY KEY(c1)
PARTITIONS 4;

CREATE TABLE tke
  ( c1 ENUM(&#39;red&#39;, &#39;orange&#39;, &#39;yellow&#39;, &#39;green&#39;, &#39;blue&#39;, &#39;indigo&#39;, &#39;violet&#39;) )
PARTITION BY LINEAR KEY(c1)
PARTITIONS 6;
Copy after login

当用RANGE,LIST,DATE或者DATETIME列分区的话,可能会用string。例如,下面的创建语句是合法的:

CREATE TABLE rc (c1 INT, c2 DATE)
PARTITION BY RANGE COLUMNS(c2) (
  PARTITION p0 VALUES LESS THAN(&#39;1990-01-01&#39;),
  PARTITION p1 VALUES LESS THAN(&#39;1995-01-01&#39;),
  PARTITION p2 VALUES LESS THAN(&#39;2000-01-01&#39;),
  PARTITION p3 VALUES LESS THAN(&#39;2005-01-01&#39;),
  PARTITION p4 VALUES LESS THAN(MAXVALUE)
);

CREATE TABLE lc (c1 INT, c2 CHAR(1))
PARTITION BY LIST COLUMNS(c2) (
  PARTITION p0 VALUES IN(&#39;a&#39;, &#39;d&#39;, &#39;g&#39;, &#39;j&#39;, &#39;m&#39;, &#39;p&#39;, &#39;s&#39;, &#39;v&#39;, &#39;y&#39;),
  PARTITION p1 VALUES IN(&#39;b&#39;, &#39;e&#39;, &#39;h&#39;, &#39;k&#39;, &#39;n&#39;, &#39;q&#39;, &#39;t&#39;, &#39;w&#39;, &#39;z&#39;),
  PARTITION p2 VALUES IN(&#39;c&#39;, &#39;f&#39;, &#39;i&#39;, &#39;l&#39;, &#39;o&#39;, &#39;r&#39;, &#39;u&#39;, &#39;x&#39;, NULL)
);
Copy after login

上述异常都不适用于BLOB或TEXT列类型。

子查询

即使子查询避开整形值或者NULL值,分区键不能子查询。

子分区的问题

子分区必须使用HASH或者KEY分区。只有RANGE和LIST分区支持被子分区;HASH和KEY不支持被子分区。

SUBPARTITION BY KEY要求显示指定子分区列,不像PARTITION BY KEY可以省略(这种情况下会默认使用表的primary key)。例如,如果是这样创建表:

CREATE TABLE ts (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(30)
);
Copy after login

你也可以使用相同的列的创建分区表(以KEY分区),使用下面语句:

CREATE TABLE ts (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(30)
)
PARTITION BY KEY()
PARTITIONS 4;
Copy after login

前面的语句其实和下面的语句是一样的:

CREATE TABLE ts (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(30)
)
PARTITION BY KEY(id)
PARTITIONS 4;
Copy after login

但是,如果尝试使用缺省列作为子分区列,创建子分区表的话,以下语句将失败,必须指定该语句才能执行成功,如下所示:(bug已知 Bug #51470)。

mysql> CREATE TABLE ts (
  ->   id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  ->   name VARCHAR(30)
  -> )
  -> PARTITION BY RANGE(id)
  -> SUBPARTITION BY KEY()
  -> SUBPARTITIONS 4
  -> (
  ->   PARTITION p0 VALUES LESS THAN (100),
  ->   PARTITION p1 VALUES LESS THAN (MAXVALUE)
  -> );
   
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near &#39;)
 
mysql> CREATE TABLE ts (
  ->   id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  ->   name VARCHAR(30)
  -> )
  -> PARTITION BY RANGE(id)
  -> SUBPARTITION BY KEY(id)
  -> SUBPARTITIONS 4
  -> (
  ->   PARTITION p0 VALUES LESS THAN (100),
  ->   PARTITION p1 VALUES LESS THAN (MAXVALUE)
  -> );
   
Query OK, 0 rows affected (0.07 sec)
Copy after login

数据字典和索引字典选项

分区表的数据字典和索引字典受以下因素制约:

表级的数据字典和索引字典被忽略(Bug #32091)

在Windows系统上,MyISAM分区表不支持独立分区或子分区的数据字典和索引字典选项。但是支持InnoDB分区表的独立分区或者子分区的数据字典。

修复和重建分区表

分区表支持CHECK TABLE, OPTIMIZE TABLE, ANALYZE TABLE, 和 REPAIR TABLE语句。

另外,你也可以用ALTER TABLE ... REBUILD PARTITION在一个分区表上重建一个或多个分区;用ALTER TABLE ... REORGANIZE PARTITION同样可以重建分区。

从MySQL 5.7.2开始,子分区支持ANALYZE, CHECK, OPTIMIZE, REPAIR, 和 TRUNCATE操作。而在MySQL5.7.5之前的版本就已经引入REBUILD语法,只是不起作用(可以参考Bug #19075411, Bug #73130)。

分区表不支持mysqlcheck, myisamchk, 和 myisampack操作。

导出选项

在MySQL 5.7.4以前的版本,不支持InnoDB分区表的FLUSH TABLES语句的导出选项(Bug #16943907)。

The above is the detailed content of Code examples that explain in detail the limitations and limitations of MySQL partition tables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template