MySQL在5.1之後才支持分區功能,但查詢是否支持分區的方法不一樣。express
MySQL5.1中使用:less
SHOW VARIABLES LIKE '%partition%';
顯示結果爲:code
+-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | have_partition_engine | YES | +-----------------------+-------+
表示支持分區表。索引
MySQL5.6中使用:SHOW PLUGINS; 來查詢。ip
SHOW PLUGINS;
顯示結果爲:it
+------------+----------+----------------+---------+---------+ | Name | Status | Type | Library | License | +------------+----------+----------------+---------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL | | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | ndbcluster | DISABLED | STORAGE ENGINE | NULL | GPL | +------------+----------+----------------+---------+---------+ 11 rows in set (0.00 sec)
partion狀態爲ACTIVE表示支持分區。 或者用一條查詢語句來檢查INFORMATION_SCHEMA.PLUGINS 表:io
SELECT PLUGIN_NAME as Name, PLUGIN_VERSION as Version, PLUGIN_STATUS as Status FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_TYPE='STORAGE ENGINE';
若是顯示結果爲table
+--------------------+---------+--------+ | Name | Version | Status | +--------------------+---------+--------+ | binlog | 1.0 | ACTIVE | | CSV | 1.0 | ACTIVE | | MEMORY | 1.0 | ACTIVE | | MRG_MYISAM | 1.0 | ACTIVE | | MyISAM | 1.0 | ACTIVE | | PERFORMANCE_SCHEMA | 0.1 | ACTIVE | | BLACKHOLE | 1.0 | ACTIVE | | ARCHIVE | 3.0 | ACTIVE | | InnoDB | 5.6 | ACTIVE | | partition | 1.0 | ACTIVE | +--------------------+---------+--------+ 10 rows in set (0.00 sec)
partion爲ACTIVE表示支持分區。date
MySQL查看和新增表分區 一、查看錶分區方法
SELECT partition_name part, partition_expression expr, partition_description descr, FROM_DAYS(partition_description) lessthan_sendtime, table_rows FROM INFORMATION_SCHEMA.partitions WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME='td_sendmessagelog'; ---這裏是表名
二、添加表分區
ALTER TABLE td_sendmessagelog ADD PARTITION ( PARTITION p20150210 VALUES LESS THAN (TO_DAYS('2015-02-10')), PARTITION p20150220 VALUES LESS THAN (TO_DAYS('2015-02-20')), PARTITION p20150301 VALUES LESS THAN (TO_DAYS('2015-03-01')), PARTITION p20150310 VALUES LESS THAN (TO_DAYS('2015-03-10')), PARTITION p20150320 VALUES LESS THAN (TO_DAYS('2015-03-20')), PARTITION p20150401 VALUES LESS THAN (TO_DAYS('2015-04-01')), PARTITION p20150410 VALUES LESS THAN (TO_DAYS('2015-04-10')), PARTITION p20150420 VALUES LESS THAN (TO_DAYS('2015-04-20')), PARTITION p20150501 VALUES LESS THAN (TO_DAYS('2015-05-01')), PARTITION pmax VALUES LESS THAN (maxvalue) );
注意:建立表分區,要指定對應的列。上面例子,用的是一個時間列(sendtime) 建立表過程以下(注意指定的primary key):
create table td_sendmessagelog ( id int not null, sendtime datetime not null primary key (id,sendtime) ) 建立索引: create index Index_sid_sendtime on td_sendmessagelog ( sid, sendtime );