Mysql 分区处理NULL值的方式

MySQL分区处理NULL值的方式

一般情况下,MySQL的分区把NULL当做零值,或者一个最小值进行处理

对于range分区

create table test_null(
    id int
)
partition by range(id)(
partition p0 values less than (-6),
partition p1 values less than (0),
partition p2 values less than (1),
partition p3 values less than MAXVALUE
);

mysql> insert into test_null values(null);
Query OK, 1 row affected (0.01 sec)

mysql> select 
    ->   partition_name part,  
    ->   partition_expression expr,  
    ->   partition_description descr,  
    ->   table_rows  
    -> from information_schema.partitions  where 
    ->   table_schema = schema()  
    ->   and table_name='test_null';
+------+------+----------+------------+
| part | expr | descr    | table_rows |
+------+------+----------+------------+
| p0   | id   | -6       |          1 |
| p1   | id   | 0        |          0 |
| p2   | id   | 1        |          0 |
| p3   | id   | MAXVALUE |          0 |
+------+------+----------+------------+
4 rows in set (0.00 sec)

对于list分区
当分区定义不包含NULL值是,会报错

create table list_null(
    id int
)
partition by list(id)(
partition p1 values in (0),
partition p2 values in (1)
);

mysql> insert into list_null values(null);
ERROR 1526 (HY000): Table has no partition for value NULL

当分区定义包含NULL时

create table list_null_1(
    id int
)
partition by list(id)(
partition p1 values in (0),
partition p2 values in (1),
partition p3 values in (null)
);

mysql> insert into list_null_1 values(null);
Query OK, 1 row affected (0.01 sec)

mysql> select 
    ->   partition_name part,  
    ->   partition_expression expr,  
    ->   partition_description descr,  
    ->   table_rows  
    -> from information_schema.partitions  where 
    ->   table_schema = schema()  
    ->   and table_name='list_null_1';
+------+------+-------+------------+
| part | expr | descr | table_rows |
+------+------+-------+------------+
| p1   | id   | 0     |          0 |
| p2   | id   | 1     |          0 |
| p3   | id   | NULL  |          1 |
+------+------+-------+------------+
3 rows in set (0.00 sec)

对于Hash分区和key分区

create table hash_null(
    id int
)
partition by hash(id)
partitions 2;

mysql> insert into hash_null values(null);
Query OK, 1 row affected (0.00 sec)

mysql> select 
    ->   partition_name part,  
    ->   partition_expression expr,  
    ->   partition_description descr,  
    ->   table_rows  
    -> from information_schema.partitions  where 
    ->   table_schema = schema()  
    ->   and table_name='hash_null';
+------+------+-------+------------+
| part | expr | descr | table_rows |
+------+------+-------+------------+
| p0   | id   | NULL  |          1 |
| p1   | id   | NULL  |          0 |
+------+------+-------+------------+
2 rows in set (0.00 sec)

对于Key分区

create table key_null(
    id int
)
partition by key(id)
partitions 2;

mysql> select 
    ->   partition_name part,  
    ->   partition_expression expr,  
    ->   partition_description descr,  
    ->   table_rows  
    -> from information_schema.partitions  where 
    ->   table_schema = schema()  
    ->   and table_name='key_null';
+------+------+-------+------------+
| part | expr | descr | table_rows |
+------+------+-------+------------+
| p0   | `id` | NULL  |          1 |
| p1   | `id` | NULL  |          0 |
+------+------+-------+------------+
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值