简体   繁体   中英

how to partition a table in non primary key column (MYSQL)

I have a table which contains 50GB data now I am trying to partition on the datetime column below are the details.

CREATE TABLE EDR2 ( id varchar(255),appKey varchar(255),clickedTime datetime ,ettId bigint(20),primary key(id),Key `ettId` (ettId),Key `clickedTime_index` (`clickedTime`) )
PARTITION BY RANGE (day(clickedTime))
( PARTITION p01 VALUES LESS THAN (2) ,
PARTITION p02 VALUES LESS THAN (3) ,
PARTITION p03 VALUES LESS THAN (4) ,
PARTITION p04 VALUES LESS THAN (5) ,
PARTITION p05 VALUES LESS THAN (6) ,
PARTITION p06 VALUES LESS THAN (7) ,
PARTITION p07 VALUES LESS THAN (8) ,
PARTITION p08 VALUES LESS THAN (9) ,
PARTITION p09 VALUES LESS THAN (10) ,
PARTITION p10 VALUES LESS THAN (11) ,
PARTITION p11 VALUES LESS THAN (12) ,
PARTITION p12 VALUES LESS THAN (13) ,
PARTITION p13 VALUES LESS THAN (14) ,
PARTITION p14 VALUES LESS THAN (15) ,
PARTITION p15 VALUES LESS THAN (16) ,
PARTITION p16 VALUES LESS THAN (17) ,
PARTITION p17 VALUES LESS THAN (18) ,
PARTITION p18 VALUES LESS THAN (19) ,
PARTITION p19 VALUES LESS THAN (20) ,
PARTITION p20 VALUES LESS THAN (21) ,
PARTITION p21 VALUES LESS THAN (22) ,
PARTITION p22 VALUES LESS THAN (23) ,
PARTITION p23 VALUES LESS THAN (24) ,
PARTITION p24 VALUES LESS THAN (25) ,
PARTITION p25 VALUES LESS THAN (26) ,
PARTITION p26 VALUES LESS THAN (27) ,
PARTITION p27 VALUES LESS THAN (28) ,
PARTITION p28 VALUES LESS THAN (29) ,
PARTITION p29 VALUES LESS THAN (30) ,
PARTITION p30 VALUES LESS THAN (31) ,
PARTITION p31 VALUES LESS THAN MAXVALUE);*

and I am getting the error:-

***ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function***

I can't create a composite primary key in (id,clickedTime) and can not remove the primary key, because it will allow the duplicate records, I don't want to change anything in my code. so please suggest.

Any UNIQUE key on a partitioned table must include the partition key because otherwise the UNIQUEness constraint would be painfully costly. Each INSERT would have to check all partitions to verify that the constraint is not violated.

Considering that PARTITIONing is normally useful only on very large tables, this overhead would be deadly.

You are partitioning on day of month - Why? What do you hope to gain? Checking for a "range" on clickedTime will not do "partition pruning" (I think). See EXPLAIN PARTITIONS SELECT ...

What is the ID like, for it to be VARCHAR(255)? Big PRIMARY KEYs are generally not practical.

Is this table MyISAM or InnoDB?

I recommend getting rid of the partitioning.

What are some of the SELECTs? I see no compound INDEXes currently, usually it is prudent to have a compound index ending with a DATETIME.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM