建表語句以下:mysql
create table comment_partition( id int not null auto_increment, commenterIpv4 char(30), blog int not null, content text, commentDate varchar(255), primary key(id,blog)) partition by linear hash(blog) partitions 10;
注意:用blog字段來分區,它必須是主鍵的一部分。sql
以下爲插入一些數據:code
INSERT INTO `comment_partition` VALUES (1,'127.0.0.1',2,'66666666666666666','2016-06-07 11-19'),(2,'127.0.0.1',2,'比較受用,謝謝博主。。。我也寫了份c3p0的使用實例,在個人OSChina上,歡迎你們閱讀並提出建議。。。。','2016-06-07 11-56'),(3,'127.0.0.1',2,'謝博主分享。。。','2016-06-07 12-00'),(4,'127.0.0.1',2,'多謝博主分享。。。。','2016-06-07 12-29'),(5,'127.0.0.1',3,'看了博主的分享後,終於解決了個人問題了。。。。','2016-06-07 12-30'),(6,'127.0.0.1',7,'6666666666666 3333333333333333333','2016-06-07 21-07'),(7,'127.0.0.1',7,'22223333333','2016-06-07 21-08'),(8,'127.0.0.1',7,'感謝博主分享。。。多謝了。。。','2016-06-07 21-08'),(9,'127.0.0.1',2,'233333332222222222222233333333333333','2016-06-07 21-33'),(10,'127.0.0.1',9,'6666666666666666','2016-06-07 21-35'),(11,'127.0.0.1',7,'多謝博主分享','2016-06-07 21-36'),(12,'127.0.0.1',8,'這種用apt-get install方式安裝的話不少文件都找不到了。。。。','2016-06-07 21-36'),(13,'127.0.0.1',4,'看了看了','2016-06-08 19-38');blog
插入數據後mysql會根據不一樣的blog的值將記錄放到不一樣的區中,以下爲查看咱們查詢時mysql從哪一個分區查詢咱們的數據(在mysql的命令窗口中輸入以下命令):rem
explain partitions select id,blog from comment_partition where blog=6;get
結果以下圖(從p6分區中查詢到):hash