对于RANGE和LIST分区,从网上的一些例子中了解到,可以为每个分区指定相应的数据库服务器,如下所示:
drop database if exists myspider;
create database myspider;
use myspider;
Create table tbl_b(
col_a int,
col_b int,
primary key(col_a)
) engine = Spider
Connection ' table "tbl_a", user "msandbox", password "msandbox" '
partition by range ( col_a ) (
partition pt1 values less than (1000) comment 'host "rdb1", port "3306"',
partition pt2 values less than (2000) comment 'host "rdb2", port "3306"',
partition pt3 values less than (MAXVALUE) comment 'host "rdb3", port "3306"'
);
但是对于KEY和HASH分区,似乎并没有这样为每个分区指定数据库服务器的实例,大多的例子如下所示:
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
body TEXT,
FULLTEXT (title,body)
)
PARTITION BY HASH (id)
PARTITIONS 4;
或者
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
body TEXT,
FULLTEXT (title,body)
)
PARTITION BY KEY (id)
PARTITIONS 4;
而且在MySQL四大分区类型中(RANGE、LIST、HASH、KEY),分区键必须是INT类型,或者通过表达式返回INT类型,可以为NULL。唯 一的例外是当分区类型为KEY分区的时候,可以使用其他类型的列作为分区键( BLOB or TEXT 列除外)。
因此,今天特意实践了一下KEY分区,需求如下:
①KEY分区的分区类型不是 INT类型;
②为每个分区指定对应的数据库服务器;
结果成功,创建满足该需求的代码如下:
DROP TABLE IF EXISTS tkc;
CREATE TABLE tkc (cr CHAR)
ENGINE=SPIDER DEFAULT CHARSET=latin1 CONNECTION=' table "tkc", user "msandbox", password "msandbox" '
PARTITION BY KEY (cr)
PARTITIONS 3
(PARTITION pt1 COMMENT = 'host "127.0.0.1", port "6001"' ENGINE = SPIDER,
PARTITION pt2 COMMENT = 'host "127.0.0.1", port "6002"' ENGINE = SPIDER,
PARTITION pt3 COMMENT = 'host "127.0.0.1", port "6003"' ENGINE = SPIDER);
或者这样也可以
DROP TABLE IF EXISTS tkc;
CREATE TABLE `tkc` (
`cr` CHAR(1) DEFAULT NULL
) ENGINE=SPIDER DEFAULT CHARSET=latin1 CONNECTION=' table "tkc", user "msandbox", password "msandbox" '
PARTITION BY KEY (cr)
(PARTITION pt1 COMMENT = 'host "127.0.0.1", port "6001"' ENGINE = SPIDER,
PARTITION pt2 COMMENT = 'host "127.0.0.1", port "6002"' ENGINE = SPIDER,
PARTITION pt3 COMMENT = 'host "127.0.0.1", port "6003"' ENGINE = SPIDER)