Get a table some days ago, nearly 4000w data, no index, primary key. (It's definitely a talent to build this watch.)
This is a log table, recorded in the game the output and consumption of items, there was a background to the table statistics .... (This is to use the super computer can be counted out), can only help the predecessors pits ....
The data is too large to be reconstructed by partitioning.
If you find it is empty, it means that your MySQL version is not enough, the partition must be at least 5.1
The following for business queries, decided to use time to do range partition (and List,hash and other types), one months a district.
Tables that are partitioned by range are partitioned in one of the following ways, with each partition containing rows whose values are in a given contiguous interval. These intervals are contiguous and cannot overlap each other, and are defined using the values less than operator.
Create a new table:
SQL code
- CREATE TABLE ' xxxxxxxx ' (
- ' Crttm ' int (one) is not NULL,
- ' Srvid ' int (one) is not NULL,
- ' EvtID ' int (one) is not NULL,
- ' Aid ' int (one) is not NULL,
- ' RID ' int (one) is not NULL,
- ' Itmid ' int (one) is not NULL,
- ' Itmnum ' int (one) is not NULL,
- ' Gdtype ' int (one) is not NULL,
- ' Gdnum ' int (one) is not NULL,
- ' ISLMT ' int (one) is not NULL,
- KEY ' Crttm ' (' Crttm '),
- KEY ' itemid ' (' itmid '),
- KEY ' srvid ' (' srvid '),
- KEY ' gdtype ' (' Gdtype ')
- ) Engine=myisam DEFAULT Charset=utf8
- PARTITION by RANGE (Crttm)
- (
- PARTITION p201303 VALUES less THAN (Unix_timestamp (' 2013-04-01 ')),
- PARTITION p201304 VALUES less THAN (Unix_timestamp (' 2013-05-01 ')),
- PARTITION p201305 VALUES less THAN (Unix_timestamp (' 2013-06-01 ')),
- PARTITION p201306 VALUES less THAN (Unix_timestamp (' 2013-07-01 ')),
- PARTITION p201307 VALUES less THAN (Unix_timestamp (' 2013-08-01 ')),
- PARTITION p201308 VALUES less THAN (Unix_timestamp (' 2013-09-01 ')),
- PARTITION p201309 VALUES less THAN (Unix_timestamp (' 2013-10-01 ')),
- PARTITION p201310 VALUES less THAN (Unix_timestamp (' 2013-11-01 ')),
- PARTITION p201311 VALUES less THAN (Unix_timestamp (' 2013-12-01 ')),
- PARTITION p201312 VALUES less THAN (Unix_timestamp (' 2014-01-01 ')),
- PARTITION p201401 VALUES less THAN (Unix_timestamp (' 2014-02-01 '))
- );
Note:
1. Primary key and unique key must be included as part of the partition key, or "ERROR 1503 (HY000)" will be reported when creating primary key and unique index
Mysql> Create unique index idx_employees1_job_code on employees1 (Job_code); ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table ' s partitioning function or mysql> ALTER table ' Skate '. ' Employees1 ' ADD PRIMARY KEY (' id '); ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table ' s partitioning function
2. Range partition Add partition can only append partition 3 after the maximum value. All partitions must have the same engine 4. Range partition partition field: integer, numeric expression, date column, date function expression (such as year (), To_days (), To_seconds (), Unix_timestamp ())
When you import old table data into a new table, you see that the data for the new table is distributed across different extents!
Maintenance Commands:
adding partitions
SQL code
- Alter table xxxxxxx Add Partition (partition P0 values less Than (1991)); Only partitions larger than the partition key can be added
Delete Partition
SQL code
- Alter table xxxxxxx Drop Partition P0;//can delete any partition
Delete partition data
SQL code
- Alter table XXXXXX truncate partition p1,p2;
- Alter table XXXXXX truncate partition all ;
- Or
- Delete from xxxxxx where separated < ' 2006-01-01 ' or (separated >= ' 2006-01-01 ' and Sep arated<' 2011-01-01 ');
Redefine partitions (including renaming partitions, accompanying moving data; merging partitions)
SQL code
- Alter table XXXXX REORGANIZE partition P1,P3,P4 into (partition PM1 values less Than (2006),
- Partition PM2 values less Than (2011));
Rebuild Rebuilding partitions
SQL code
- Alter table XXXXXX rebuild partition pm1/all;//equivalent to drop all records and then reinsert; can resolve disk fragmentation
Optimizing tables
SQL code
- Alter table TT2 optimize partition PM1;//After extensive delete table data, you can reclaim space and defragmentation. But after the 5.5.30 support. Before 5.5.30 can be replaced by Recreate+analyze, if with rebuild+analyze speed slow
Analzye table
SQL code
- Alter table XXXXXX analyze partition pm1/all;
Check table
SQL code
- Alter table xxxxxx Check partition pm1/all;
SQL code
- Show create table employees2; To view the definition of a partitioned table
- Show table status like ' Employees2 ' \g; When viewing a table, it is a partitioned table such as "create_options:partitioned"
- SELECT * FROM information_schema. Key_column_usage where table_name=' employees2 '; View Index
- SELECT * from information_schema.partitions WHERE table_name=' employees2 '//view partition table
- Explain partitions select * from employees2 where separated < ' 1990-01-01 ' or separated > ' 2 016-01-01 '; To see if a partition is being used by select
Actual MySQL partition