最近工作中遇到乙個情況,需要將使用mysql儲存海量的資料,需要使用mysql分割槽的技術,那麼下面是乙個按照日期分表的乙個事例:
create table `big_table` (`id`
int(11
) not null auto_increment,
`thedate` date default null comment '日期
',`shop_id` bigint(
20) default null comment 'id'
, `auction_name` varchar(
128) default null comment '名稱'
, primary key (`id`,`thedate`),
key `seller_id` (`seller_id`,`thedate`)
) engine=innodb default charset=utf8
partition by hash (dayofyear(thedate)) partitions
366;
一、建立分割槽表事例:
create table bigtable(id int,
snptime datetime not null,
value varchar(
20),
primary key (snptime, id)
) engine=innodb
partition by range (to_days(snptime))
(partition p1 values less than (to_days(
'2009-1-31
')),
partition p2 values less than (to_days(
'2009-2-28
')),
partition p3 values less than (to_days(
'2008-3-31
')),
partition p4 values less than (to_days(
'2008-4-30
')),
partition p5 values less than (to_days(
'2008-5-31
')),
partition p6 values less than (to_days(
'2008-6-30
')),
partition p7 values less than (to_days(
'2008-7-31
')),
partition p8 values less than (to_days(
'2008-8-31
')),
partition p9 values less than (to_days(
'2008-9-30
')),
partition p10 values less than (to_days(
'2008-10-31
')),
partition p11 values less than (to_days(
'2008-11-30
')),
partition p12 values less than (to_days(
'2008-12-31
')),
partition p13 values less than maxvalue
) ;
注意一點:一定要有主鍵,並且主鍵要包括分割槽鍵。
二、給已存在的表加分割槽
alter table 表名partition by range (to_days(collecttime))
(partition pmin values less than (to_days(
'2010-01-01
')),
partition p201001 values less than (to_days(
'2010-02-01
')) ,
......
partition pmax values less than maxvalue );
如果表中已有資料,分割槽時會自動進行分割槽儲存,不必擔心資料丟失或者手動分類資料.
三、刪除表中的指定分割槽
alter table 表名 drop partition 分割槽名;
四、追加表分割槽
alter table 表名 drop partition pmax;alter table 表名
add partition (
partition p201201 values less than (to_days(
'2012-2-1
')),
partition pmax values less than maxvalue);
五、檢視標分割槽資訊
selectpartition_name part,
partition_expression expr,
partition_description descr,
table_rows
from
information_schema.partitions
where
table_schema =schema()
and table_name='
表名';
六、檢視查詢語句涉及分割槽資訊
explain partitionsselect … from 表名 where …;
mysql表分割槽全文搜尋 Mysql表分割槽
什麼時候使用分割槽 海量資料 資料表索引大於伺服器有效記憶體 分割槽的限制 大部分只能對資料表的整型列進行分割槽,或者資料列可以通過分割槽函式轉化成整型列 其中columns支援 integer string date datetime型別 最大分割槽數目不能超過1024 如果含有唯一索引或者主鍵,...
mysql表分割槽
表分割槽的優點 查詢優化 缺點 除了資料庫管理方面複雜了點,其它的還沒有發現 只有5.1及之後的版本才支付分割槽,同時5.1中分割槽的一些維護的工具還不完善 mysql目前四種分割槽 1range 根據某個列的某種運算進行分割槽,分割槽的標誌都是該列的某種運算後的連續區間 create table ...
mysql 表分割槽
修改表的主鍵 alter table tb channel pv drop primary key,add primary key id channel 測試新增分割槽和刪除分割槽 新增刪除range分割槽 1 建立乙個分割槽 create table titles emp no int not n...