如何通过mysql表分为两层?

时间:2013-03-26 19:48:36

标签: mysql database indexing database-partitioning database-tuning

我想对表格进行分区,此代码将向您显示表格的结构。该表目前有大约500万条记录。

我需要像这样的MySql分区语法

主分区是提交的trigger_on分区类型范围“按年” 然后通过名为status的字段进行子分区。

如何在现有表上创建分区/子分区?

CREATE TABLE `phone_calls` (
`phone_call_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(11) unsigned NOT NULL,
`team_id` int(11) unsigned NOT NULL,
`call_code_id` int(11) unsigned NOT NULL,
`result_code_id` int(11) unsigned NOT NULL DEFAULT '0',
`trigger_on` datetime NOT NULL,
 `created_on` datetime NOT NULL,
`first_attempt_on` datetime DEFAULT NULL, 
`first_attempt_by` int(11) unsigned DEFAULT NULL,
`call_subject` char(100) NOT NULL,
`status` tinyint(1) NOT NULL COMMENT '0= purge, 1=active, 2 = completed', `workflow_generated` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1= generated by system flow, 0 created by a user',
`last_attempt_on` datetime DEFAULT NULL,
`last_attempt_by` int(11) unsigned DEFAULT NULL,
`total_attempts` tinyint(3) unsigned NOT NULL DEFAULT '0',
`modified_by` int(11) unsigned DEFAULT NULL,
`last_call_id` int(11) unsigned NOT NULL,
`call_direction` enum('INBOUND','OUTBOUND') NOT NULL COMMENT 'INBOUND or OUTBOUND', `call_notes` text NOT NULL,
`owner_id` int(11) NOT NULL,
`call_duration` smallint(5) unsigned NOT NULL DEFAULT '0',
`modified_on` datetime DEFAULT NULL,
PRIMARY KEY (`phone_call_id`),
KEY `owner_id` (`owner_id`),
KEY `call_code_id` (`call_code_id`),
KEY `result_code_id` (`result_code_id`),
KEY `account_id` (`account_id`),
KEY `trigger_on` (`trigger_on`),
KEY `created_on` (`created_on`),
KEY `status` (`status`),
KEY `pcto` (`trigger_on`,`status`,`owner_id`))
ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4

1 个答案:

答案 0 :(得分:0)

试试这个sql

CREATE TABLE `phone_calls` (
`phone_call_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(11) unsigned NOT NULL,
`team_id` int(11) unsigned NOT NULL,
`call_code_id` int(11) unsigned NOT NULL,
`result_code_id` int(11) unsigned NOT NULL DEFAULT '0',
`trigger_on` datetime NOT NULL,
 `created_on` datetime NOT NULL,
`first_attempt_on` datetime DEFAULT NULL, 
`first_attempt_by` int(11) unsigned DEFAULT NULL,
`call_subject` char(100) NOT NULL,
`status` tinyint(1) NOT NULL COMMENT '0= purge, 1=active, 2 = completed', `workflow_generated` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1= generated by system flow, 0 created by a user',
`last_attempt_on` datetime DEFAULT NULL,
`last_attempt_by` int(11) unsigned DEFAULT NULL,
`total_attempts` tinyint(3) unsigned NOT NULL DEFAULT '0',
`modified_by` int(11) unsigned DEFAULT NULL,
`last_call_id` int(11) unsigned NOT NULL,
`call_direction` enum('INBOUND','OUTBOUND') NOT NULL COMMENT 'INBOUND or OUTBOUND', `call_notes` text NOT NULL,
`owner_id` int(11) NOT NULL,
`call_duration` smallint(5) unsigned NOT NULL DEFAULT '0',
`modified_on` datetime DEFAULT NULL,
PRIMARY KEY (`phone_call_id`,`trigger_on`,`status`),
KEY `owner_id` (`owner_id`),
KEY `call_code_id` (`call_code_id`),
KEY `result_code_id` (`result_code_id`),
KEY `account_id` (`account_id`),
KEY `trigger_on` (`trigger_on`),
KEY `created_on` (`created_on`),
KEY `status` (`status`),
KEY `pcto` (`trigger_on`,`status`,`owner_id`))
PARTITION BY RANGE( YEAR(`trigger_on`) )
SUBPARTITION BY HASH( `status` )
SUBPARTITIONS 2(
  PARTITION p0 VALUES LESS THAN (1990),
  PARTITION p1 VALUES LESS THAN MAXVALUE
)

SQL FIDDLE