文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

MySQL中怎么实现表分区功能

2024-04-02 19:55

关注

今天就跟大家聊聊有关MySQL中怎么实现表分区功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

 MySQL表分区功能基础
  创建分区表
  CREATE TABLE `表名` (
  `EQUIPMENTID` char(17) NOT NULL,
  `ATTRIBUTEID` char(4) NOT NULL,
  `VALUE` varchar(20) NOT NULL,
  `COLLECTTIME` datetime NOT NULL
  )
  ENGINE=InnoDB (适用大部分引擎,可根据需要调整)
  DEFAULT CHARSET=gbk(编码可根据需要修改)
  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 p201002 VALUES LESS THAN (to_days('2010-03-01')) ,
  PARTITION p201003 VALUES LESS THAN (to_days('2010-04-01')) ,
  PARTITION p201004 VALUES LESS THAN (to_days('2010-05-01')) ,
  PARTITION p201005 VALUES LESS THAN (to_days('2010-06-01')) ,
  PARTITION p201006 VALUES LESS THAN (to_days('2010-07-01')) ,
  PARTITION p201007 VALUES LESS THAN (to_days('2010-08-01')) ,
  PARTITION p201008 VALUES LESS THAN (to_days('2010-09-01')) ,
  PARTITION p201009 VALUES LESS THAN (to_days('2010-10-01')) ,
  PARTITION p201010 VALUES LESS THAN (to_days('2010-11-01')),
  PARTITION p201011 VALUES LESS THAN (to_days('2010-12-01')),
  PARTITION p201012 VALUES LESS THAN (to_days('2011-01-01')),
  PARTITION p201101 VALUES LESS THAN (to_days('2011-02-01')),
  PARTITION p201102 VALUES LESS THAN (to_days('2011-03-01')),
  PARTITION p201103 VALUES LESS THAN (to_days('2011-04-01')),
  PARTITION p201104 VALUES LESS THAN (to_days('2011-05-01')),
  PARTITION p201105 VALUES LESS THAN (to_days('2011-06-01')),
  PARTITION p201106 VALUES LESS THAN (to_days('2011-07-01')),
  PARTITION p201107 VALUES LESS THAN (to_days('2011-08-01')),
  PARTITION p201108 VALUES LESS THAN (to_days('2011-09-01')),
  PARTITION p201109 VALUES LESS THAN (to_days('2011-10-01')),
  PARTITION p201110 VALUES LESS THAN (to_days('2011-11-01')),
  PARTITION p201111 VALUES LESS THAN (to_days('2011-12-01')),
  PARTITION p201112 VALUES LESS THAN (to_days('2012-01-01')),
  PARTITION pmax 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 p201002 VALUES LESS THAN (to_days('2010-03-01')) ,
  PARTITION p201003 VALUES LESS THAN (to_days('2010-04-01')) ,
  PARTITION p201004 VALUES LESS THAN (to_days('2010-05-01')) ,
  PARTITION p201005 VALUES LESS THAN (to_days('2010-06-01')) ,
  PARTITION p201006 VALUES LESS THAN (to_days('2010-07-01')) ,
  PARTITION p201007 VALUES LESS THAN (to_days('2010-08-01')) ,
  PARTITION p201008 VALUES LESS THAN (to_days('2010-09-01')) ,
  PARTITION p201009 VALUES LESS THAN (to_days('2010-10-01')) ,
  PARTITION p201010 VALUES LESS THAN (to_days('2010-11-01')),
  PARTITION p201011 VALUES LESS THAN (to_days('2010-12-01')),
  PARTITION p201012 VALUES LESS THAN (to_days('2011-01-01')),
  PARTITION p201101 VALUES LESS THAN (to_days('2011-02-01')),
  PARTITION p201102 VALUES LESS THAN (to_days('2011-03-01')),
  PARTITION p201103 VALUES LESS THAN (to_days('2011-04-01')),
  PARTITION p201104 VALUES LESS THAN (to_days('2011-05-01')),
  PARTITION p201105 VALUES LESS THAN (to_days('2011-06-01')),
  PARTITION p201106 VALUES LESS THAN (to_days('2011-07-01')),
  PARTITION p201107 VALUES LESS THAN (to_days('2011-08-01')),
  PARTITION p201108 VALUES LESS THAN (to_days('2011-09-01')),
  PARTITION p201109 VALUES LESS THAN (to_days('2011-10-01')),
  PARTITION p201110 VALUES LESS THAN (to_days('2011-11-01')),
  PARTITION p201111 VALUES LESS THAN (to_days('2011-12-01')),
  PARTITION p201112 VALUES LESS THAN (to_days('2012-01-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);
  查看标分区信息
  SELECT
  partition_name part,
  partition_expression expr,
  partition_description descr,
  table_rows
  FROM
  INFORMATION_SCHEMA.partitions
  WHERE
  TABLE_SCHEMA = schema()
  AND TABLE_NAME='表名';
  查看查询语句涉及分区信息
  explain partitions
  select … from 表名 where …;
  该操作只查看查询语句相关的分区信息,不会返回查询结果.

看完上述内容,你们对MySQL中怎么实现表分区功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯