在 mySQL 中对表进行分区

发布于 2025-01-01 03:18:05 字数 70 浏览 1 评论 0原文

有一个包含 300 万行的表,想要对其进行分区。有 5 列,其中一列是日期列。我想按日期拆分表格。任何简单的帮助都会很棒。谢谢

Have a table with 3 million rows and would like to partition it. There are 5 columns, one of them which is a date column. I would like to split up the table by dates. Any easy help would be great. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方圜几里 2025-01-08 03:18:05

如果我正确理解您的问题,有几种方法可以做到这一点:

您可以使用 Between 关键字按特定日期范围进行细分。

drop table if exists TableA, TableB;

create table TableA
select * from YourTable
where DateColumn between '2011-07-01' and '2012-02-01';

create table TableB
select * from YourTable
where DateColumn between '2011-01-01' and '2011-06-30';

或者,如果您的细分更简单(例如您想按年份细分),您可以使用日期函数。

drop table if exists TableA, TableB;

create table TableA
select * from YourTable
where year(DateColumn) = '2011';

create table TableB
select * from YourTable
where year(DateColumn) = '2012';

可能还有更动态的方法来做到这一点,但我需要听到有关您的环境和目标的更多详细信息。

If I'm understanding your question correctly, there are a couple of ways to do this:

You could do it using the Between keyword to break down by specific date ranges.

drop table if exists TableA, TableB;

create table TableA
select * from YourTable
where DateColumn between '2011-07-01' and '2012-02-01';

create table TableB
select * from YourTable
where DateColumn between '2011-01-01' and '2011-06-30';

Or if your breakdown is more simple (e.g. you want to break down by year), you can use the date functions.

drop table if exists TableA, TableB;

create table TableA
select * from YourTable
where year(DateColumn) = '2011';

create table TableB
select * from YourTable
where year(DateColumn) = '2012';

There are probably more dynamic ways to do this as well, but I would need to hear some more details of your environment and goals.

抱着落日 2025-01-08 03:18:05

你说的把桌子分开是什么意思?

您可以通过执行插入选择并选择现有数据在日期列上执行自 JOIN GROUP BY 来插入到其他表中。

What do you mean by split up the table?

You can insert into other tables by performing an insert-select and selecting your existing data doing a self JOIN GROUP BY on the date column.

粉红×色少女 2025-01-08 03:18:05

您可以在 MySQL 中使用表分区。步骤如下:

  1. 备份您的表。

  2. 从数据库中删除表

  3. 运行创建表查询和分区语句。

    示例:http://dev.mysql.com/tech-resources/文章/partitioning.html

    创建表part_tab
    (  
        c1 int 默认为 NULL,
        c2 varchar(30) 默认为 NULL,
        c3 日期默认为 NULL
    ) 引擎=myisam 
    按范围划分(年份(c3))(分区 p0 值小于(1995 年),
    分区 p1 值小于 (1996) 、分区 p2 值小于 (1997) 、
    分区 p3 值小于 (1998) 、分区 p4 值小于 (1999) 、
    分区 p5 值小于 (2000) 、分区 p6 值小于 (2001) 、
    分区 p7 值小于 (2002) 、分区 p8 值小于 (2003) 、
    分区 p9 值小于 (2004),分区 p10 值小于 (2010),
    分区 p11 值小于 MAXVALUE );
    
  4. 现在导入表的数据。

You can use Table Partitioning in MySQL. Steps Are:

  1. Take a backup of your table.

  2. Drop Table from your Database

  3. Run the create table query along with partition statement.

    example : http://dev.mysql.com/tech-resources/articles/partitioning.html

    CREATE TABLE part_tab
    (  
        c1 int default NULL,
        c2 varchar(30) default NULL,
        c3 date default NULL
    ) engine=myisam 
    PARTITION BY RANGE (year(c3)) (PARTITION p0 VALUES LESS THAN (1995),
    PARTITION p1 VALUES LESS THAN (1996) , PARTITION p2 VALUES LESS THAN (1997) ,
    PARTITION p3 VALUES LESS THAN (1998) , PARTITION p4 VALUES LESS THAN (1999) ,
    PARTITION p5 VALUES LESS THAN (2000) , PARTITION p6 VALUES LESS THAN (2001) ,
    PARTITION p7 VALUES LESS THAN (2002) , PARTITION p8 VALUES LESS THAN (2003) ,
    PARTITION p9 VALUES LESS THAN (2004) , PARTITION p10 VALUES LESS THAN (2010),
    PARTITION p11 VALUES LESS THAN MAXVALUE );
    
  4. Now Import data of your table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文