公司的數據量半年達到了一千萬條數據,現在的數據量在三千萬條左右,之前加了索引,現在數據量多了又變得很慢了,網上說建表分區可以解決問題,但是不知道怎麼在已建的表上增加表分區,請求大神的幫助!先謝謝了
這個是我總結的可以看看。
2、表分區
1) 查詢是否支持表分區:
mysql5.6以前的: SHOW VARIABLES LIKE '%partition%';
mysql> SHOW VARIABLES LIKE '%partition%';
+-----------------------+-------+
|Variable_name | Value |
+-----------------------+-------+
| have_partition_engine | YES |
+-----------------------+-------+
如果是yes表示你當前的配置支持分區
5.6以後的版本: SHOW PLUGINS;
最後一行,可以看到partition是ACTIVE的,表示支持分區
2)查詢某個表的分區信息
select
partition_name,
partition_expression,
partition_description,
table_rows
from
INFORMATION_SCHEMA.partitions
where
-- table_schema='accountrecord_copy'
table_name = '表名';
3)時間分區
alter table member
PARTITION BY RANGE ((year(createTime)*100+month(createTime))*100+day(createTime)) (
PARTITION s20100401 VALUES LESS THAN (20160401),
PARTITION s20100402 VALUES LESS THAN (20160701),
PARTITION s20100403 VALUES LESS THAN (20161001),
PARTITION s20100404 VALUES LESS THAN (20170101),
PARTITION s20100405 VALUES LESS THAN (20170401),
PARTITION s20100406 VALUES LESS THAN (20170701),
PARTITION p1 VALUES LESS THAN (MAXVALUE)
);
或
alter table member
partition by range COLUMNS (createTime)
(
PARTITION p0 VALUES LESS THAN ('2016-03-01'),
PARTITION p1 VALUES LESS THAN ('2016-06-01'),
PARTITION p2 VALUES LESS THAN ('2016-09-01'),
PARTITION p3 VALUES LESS THAN ('2016-12-01'),
PARTITION p21 VALUES LESS THAN MAXVALUE
);
4) key 分區
alter table accountrecord_copy
partition by key(id) partitions 4;
id可以不指定 默認為主鍵,