1、在master2上创建users表
2、配置rule.xml
3、插入测试数据
在amoeba中插入1条记录,却查到了2条一样的记录,在master1和master2上分别都插入记录,且记录相同
然后truncate这条记录,发现master1上的记录删除了,而master2的记录然后存在。
继续插入测试数据
查看master1和master2的数据分布情况
master1:
master2:
4、测试
4.1 排序
很乱哦,仔细查看,其实是先读取master2,然后读取master1的数据,而且是分别排序的
4.2 分页
分页与排序的情况差不多,但是不同的是分页需要在2个分库中同时读取相同的分页数
4.3 join
当表groups在master2上的时候,报错表不存在。
在master1上创建groups表,并插入与master1一样的数据
删除master2上的groups表,在次进行join查询
仍然报错表不存在,最后在master1和master2上同时创建groups表,并数据相同
也就是说没有分片的join表,必须在分片表的每个分库中都存在,也就是说amoeba不支持分库的join查询。
- [root@localhost:mydb][09:32:49am] MySQL-> CREATE TABLE `users` (
- -> `user_id` int(11) NOT NULL,
- -> `group_id` int(11) NOT NULL,
- -> `name` varchar(20) DEFAULT NULL,
- -> `address` varchar(50) DEFAULT NULL,
- -> `email` varchar(50) DEFAULT NULL,
- -> PRIMARY KEY (`user_id`)
- -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Query OK, 0 rows affected (0.02 sec)
- <tableRule name="users" schema="mydb" defaultPools="master1,master2">
-
- <rule name="rule1">
- <parameters>user_id</parameters>
- <expression><![CDATA[ user_id %2 == 0 ]]></expression>
- <defaultPools>master1</defaultPools>
- <readPools>slave1</readPools>
- <writePools>master1</writePools>
- </rule>
-
- <rule name="rule1">
- <parameters>user_id</parameters>
- <expression><![CDATA[ user_id %2 == 1 ]]></expression>
- <defaultPools>master2</defaultPools>
- <readPools>slave2</readPools>
- <writePools>master2</writePools>
- </rule>
-
- </tableRule>
-
- <tableRule name="groups" schema="mydb" defaultPools="master2" />
- [root@10.10.10.39:mydb][07:59:03pm] MySQL-> insert into users values (1,1,'john','zhejiang','a@163.com');
- Query OK, 2 rows affected (0.05 sec)
- ullnull
-
- [root@10.10.10.39:mydb][07:59:30pm] MySQL-> select * from users;
- +---------+----------+------+----------+-----------+
- | user_id | group_id | name | address | email |
- +---------+----------+------+----------+-----------+
- | 1 | 1 | john | zhejiang | a@163.com |
- | 1 | 1 | john | zhejiang | a@163.com |
- +---------+----------+------+----------+-----------+
- 2 rows in set (0.01 sec)
-
- [root@10.10.10.39:mydb][08:01:27pm] MySQL-> truncate table users;
- Query OK, 0 rows affected (0.06 sec)
然后truncate这条记录,发现master1上的记录删除了,而master2的记录然后存在。
继续插入测试数据
- insert into users(user_id,group_id,name,address,email) values (1,1,'john','zhejiang','a@163.com');
- insert into users(user_id,group_id,name,address,email) values (2,1,'james','bejing','b@163.com');
- insert into users(user_id,group_id,name,address,email) values (3,2,'kitty','shanghai','c@163.com');
- insert into users(user_id,group_id,name,address,email) values (4,4,'dave','hubei','d@163.com');
- insert into users(user_id,group_id,name,address,email) values (5,4,'mica','jiangsu','e@163.com');
- insert into users(user_id,group_id,name,address,email) values (6,3,'chole','zhejiang','f@163.com');
- insert into users(user_id,group_id,name,address,email) values (7,4,'ice','shanghai','g@163.com')
master1:
- [root@localhost:mydb][12:28:08pm] MySQL-> select * from users;
- +---------+----------+-------+----------+-----------+
- | user_id | group_id | name | address | email |
- +---------+----------+-------+----------+-----------+
- | 2 | 1 | james | bejing | b@163.com |
- | 4 | 4 | dave | hubei | d@163.com |
- | 6 | 3 | chole | zhejiang | f@163.com |
- +---------+----------+-------+----------+-----------+
- 3 rows in set (0.00 sec):
- [root@localhost:mydb][12:31:10pm] MySQL-> select * from users;
- +---------+----------+-------+----------+-----------+
- | user_id | group_id | name | address | email |
- +---------+----------+-------+----------+-----------+
- | 1 | 1 | john | zhejiang | a@163.com |
- | 3 | 2 | kitty | shanghai | c@163.com |
- | 5 | 4 | mica | jiangsu | e@163.com |
- | 7 | 4 | ice | shanghai | g@163.com |
- +---------+----------+-------+----------+-----------+
- 4 rows in set (0.00 sec)
4.1 排序
- [root@10.10.10.39:mydb][08:14:19pm] MySQL-> select * from users order by user_id;
- +---------+----------+-------+----------+-----------+
- | user_id | group_id | name | address | email |
- +---------+----------+-------+----------+-----------+
- | 1 | 1 | john | zhejiang | a@163.com |
- | 3 | 2 | kitty | shanghai | c@163.com |
- | 5 | 4 | mica | jiangsu | e@163.com |
- | 7 | 4 | ice | shanghai | g@163.com |
- | 2 | 1 | james | bejing | b@163.com |
- | 4 | 4 | dave | hubei | d@163.com |
- | 6 | 3 | chole | zhejiang | f@163.com |
- +---------+----------+-------+----------+-----------+
- 7 rows in set (0.01 sec)
4.2 分页
- [root@10.10.10.39:mydb][08:17:32pm] MySQL-> select * from users limit 0,3;
- +---------+----------+-------+----------+-----------+
- | user_id | group_id | name | address | email |
- +---------+----------+-------+----------+-----------+
- | 1 | 1 | john | zhejiang | a@163.com |
- | 3 | 2 | kitty | shanghai | c@163.com |
- | 5 | 4 | mica | jiangsu | e@163.com |
- | 2 | 1 | james | bejing | b@163.com |
- | 4 | 4 | dave | hubei | d@163.com |
- | 6 | 3 | chole | zhejiang | f@163.com |
- +---------+----------+-------+----------+-----------+
- 6 rows in set (0.01 sec)
4.3 join
- [root@10.10.10.39:mydb][08:20:10pm] MySQL-> select user_id,a.group_id,name,address,email,group_name,privilege_id
- -> from mydb.users a,mydb.groups b
- -> where a.group_id=b.group_id;
- ERROR 1146 (42S02): Table 'mydb.groups' doesn
在master1上创建groups表,并插入与master1一样的数据
- [root@localhost:mydb][12:32:42pm] MySQL-> CREATE TABLE `groups` (
- -> `group_id` int(11) NOT NULL,
- -> `group_name` varchar(20) DEFAULT NULL,
- -> `privilege_id` int(11) NOT NULL,
- -> PRIMARY KEY (`group_id`)
- -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Query OK, 0 rows affected (0.04 sec)
-
- [root@localhost:mydb][12:43:52pm] MySQL-> insert into groups values (1,'admin',1);
- Query OK, 1 row affected (0.01 sec)
-
- [root@localhost:mydb][12:44:08pm] MySQL-> insert into groups values (2,'leder',2);
- Query OK, 1 row affected (0.01 sec)
-
- [root@localhost:mydb][12:44:08pm] MySQL-> insert into groups values (3,'engineer',4);
- Query OK, 1 row affected (0.00 sec)
-
- [root@localhost:mydb][12:44:08pm] MySQL-> insert into groups values (4,'sale',3)
- ->
- -> ;
- Query OK, 1 row affected (0.02 sec)
- [root@10.10.10.39:mydb][08:27:37pm] MySQL-> select user_id,a.group_id,name,address,email,group_name,privilege_id
- -> from mydb.users a,mydb.groups b
- -> where a.group_id=b.group_id;
- ERROR 1146 (42S02): Table 'mydb.groups' doesn
- [root@10.10.10.39:mydb][08:21:36pm] MySQL-> select user_id,a.group_id,name,address,email,group_name,privilege_id
- -> from mydb.users a,mydb.groups b
- -> where a.group_id=b.group_id;
- +---------+----------+-------+----------+-----------+------------+--------------+
- | user_id | group_id | name | address | email | group_name | privilege_id |
- +---------+----------+-------+----------+-----------+------------+--------------+
- | 1 | 1 | john | zhejiang | a@163.com | admin | 1 |
- | 3 | 2 | kitty | shanghai | c@163.com | leder | 2 |
- | 5 | 4 | mica | jiangsu | e@163.com | sale | 3 |
- | 7 | 4 | ice | shanghai | g@163.com | sale | 3 |
- | 2 | 1 | james | bejing | b@163.com | admin | 1 |
- | 6 | 3 | chole | zhejiang | f@163.com | engineer | 4 |
- | 4 | 4 | dave | hubei | d@163.com | sale | 3 |
- +---------+----------+-------+----------+-----------+------------+--------------+
- 7 rows in set (0.01 sec)
也就是说没有分片的join表,必须在分片表的每个分库中都存在,也就是说amoeba不支持分库的join查询。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12219480/viewspace-1788852/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12219480/viewspace-1788852/