SET @@session.default_storage_engine = 'InnoDB';
drop table if exists t1;
# Case1. Partitioning by RANGE based on a non-stored virtual column.
CREATE TABLE t1 (
a DATE NOT NULL,
b int as (year(a))
)
PARTITION BY RANGE( b ) (
PARTITION p0 VALUES LESS THAN (2006),
PARTITION p2 VALUES LESS THAN (2008)
);
insert into t1 values ('2006-01-01',default);
insert into t1 values ('2007-01-01',default);
insert into t1 values ('2005-01-01',default);
insert into t1 (a) values ('2007-01-02');
select * from t1;
a b 2005-01-012005 2006-01-012006 2007-01-012007 2007-01-022007
select partition_name,table_rows,data_length from information_schema.partitions where table_name = 't1';
partition_name table_rows data_length
p0 116384
p2 316384
# Modify the expression of virtual column b
ALTER TABLE t1 modify b int as (year(a)-1);
select * from t1;
a b 2005-01-012004 2006-01-012005 2007-01-012006 2007-01-022006
select partition_name,table_rows,data_length from information_schema.partitions where table_name = 't1';
partition_name table_rows data_length
p0 216384
p2 216384
drop table t1;
# Case2. Partitioning by LIST based on a stored virtual column.
CREATE TABLE t1 (a int, b int as (a % 3 ) persistent)
PARTITION BY LIST (a+1)
(PARTITION p1 VALUES IN (1), PARTITION p2 VALUES IN (2));
insert into t1 values (1,default);
select partition_name,table_rows,data_length from information_schema.partitions where table_name = 't1';
partition_name table_rows data_length
p1 016384
p2 116384
select * from t1;
a b 11
select * from t1;
a b 11
drop table t1;
# Case3. Partitioning by HASH based on a non-stored virtual column.
CREATE TABLE t1 (
a DATE NOT NULL,
b int as (year(a))
)
PARTITION BY HASH( b % 3 ) PARTITIONS 3;
insert into t1 values ('2005-01-01',default);
insert into t1 values ('2006-01-01',default);
select * from t1;
a b 2005-01-012005 2006-01-012006
select partition_name,table_rows,data_length from information_schema.partitions where table_name = 't1';
partition_name table_rows data_length
p0 016384
p1 116384
p2 116384
# Modify the expression of virtual column b
ALTER TABLE t1 modify b int as (year(a)-1);
select * from t1;
a b 2005-01-012004 2006-01-012005
select partition_name,table_rows,data_length from information_schema.partitions where table_name = 't1';
partition_name table_rows data_length
p0 116384
p1 116384
p2 016384
drop table t1;
create table t1 (a int, b datetime as (now())) partition by hash(b+1) partitions 3; ERROR HY000: Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed
create table t1 (a int, b varchar(100) as (user())) partition by hash(b+1) partitions 3; ERROR HY000: Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed
create table t1 (a int, b double as (rand())) partition by hash(b+1) partitions 3; ERROR HY000: Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.