如果写的不对的地方,欢迎各位提意见。 可以在数据非常大的时候起到分发表或者库到不同的服务器。减少每个服务器的IO。
首先看看有没有federated 引擎。
mysql> show engines;
+------------+----------+----------------------------------------------------------------+
| Engine | Support | Comment |
+------------+----------+----------------------------------------------------------------+
| MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys |
| BerkeleyDB | NO | Supports transactions and page-level locking |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) |
| EXAMPLE | YES | Example storage engine |
| ARCHIVE | YES | Archive storage engine |
| CSV | YES | CSV storage engine |
| ndbcluster | DISABLED | Clustered, fault-tolerant, memory-based tables |
| FEDERATED | YES | Federated MySQL storage engine |
| MRG_MYISAM | YES | Collection of> | ISAM | NO | Obsolete storage engine |
+------------+----------+----------------------------------------------------------------+
12 rows in set (0.00 sec)
A(192.168.0.233:3306)
B(192.168.0.233:3307)
C(192.168.0.233:3308)
D(192.168.0.234:3306)
在A、B、C、D 分别创建数据库。
mysql> create database t_boy;
Query OK, 1 row affected (0.00 sec)
mysql> use t_boy;
Database changed
在B、C上分别创建授权用户。
mysql> grant all privileges on t_boy.* to root@'%'> Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在B主机上:
mysql> create table t_tableB (id int not null auto_increment primary key, c_str char(20) not null) engine myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-----------------+
| Tables_in_t_boy |
+-----------------+
| t_table |
+-----------------+
1 row in set (0.00 sec)
在C主机上:
mysql> create table t_tableC (id int not null auto_increment primary key, c_str char(20) not null) engine myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-----------------+
| Tables_in_t_boy |
+-----------------+
| t_table |
+-----------------+
1 row in set (0.00 sec)
关于创建FEDERATED引擎的方法详细见手册。
在A主机上:
mysql> create table t_1 (id int not null auto_increment primary key, c_str char(20) not null) engine federated connection = 'mysql://root:123456@192.168.0.233:3307/t_boy/t_tableB';
Query OK, 0 rows affected (0.04 sec)
mysql> create table t_2 (id int not null auto_increment primary key, c_str char(20) not null) engine federated connection = 'mysql://root:123456@192.168.0.233:3308/t_boy/t_tableC';
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t_1(c_str) values (rand());
Query OK, 1 row affected (0.53 sec)
mysql> insert into t_2(c_str) values (rand());
Query OK, 1 row affected (0.03 sec)
可以像在一个主机上进行操作。
mysql> select t_1.*,t_2.* from t_1 inner join t_2 using(id);
+----+----------------+----+------------------+
|> +----+----------------+----+------------------+
| 1 | 0.304819039353 | 1 | 0.24238659184648 |
+----+----------------+----+------------------+
1 row in set (0.00 sec)
插入百万级别的数据后
mysql> select t_1.*,t_2.* from t_1 inner join t_2 using(id) limit 20;
+----+----------------------+----+----------------------+
|> +----+----------------------+----+----------------------+
| 1 | 0.304819039353 | 1 | 0.24238659184648 |
| 2 | 1.304819039353 | 2 | 1.2423865918465 |
| 3 | 1.304819039352999920 | 3 | 1.242386591846480037 |
| 4 | 2.304819039353000143 | 4 | 2.242386591846500021 |
| 5 | 1.304819039352999920 | 5 | 1.242386591846480037 |
| 6 | 2.304819039353000143 | 6 | 2.242386591846500021 |
| 7 | 2.304819039353000143 | 7 | 2.242386591846480037 |
| 8 | 3.304819039353000143 | 8 | 3.242386591846500021 |
| 9 | 1.304819039352999920 | 9 | 1.242386591846480037 |
| 10 | 2.304819039353000143 | 10 | 2.242386591846500021 |
| 11 | 2.304819039353000143 | 11 | 2.242386591846480037 |
| 12 | 3.304819039353000143 | 12 | 3.242386591846500021 |
| 13 | 2.304819039353000143 | 13 | 2.242386591846480037 |
| 14 | 3.304819039353000143 | 14 | 3.242386591846500021 |
| 15 | 3.304819039353000143 | 15 | 3.242386591846480037 |
| 16 | 4.304819039353000143 | 16 | 4.242386591846500465 |
| 17 | 1.304819039352999920 | 17 | 1.242386591846480037 |
| 18 | 2.304819039353000143 | 18 | 2.242386591846500021 |
| 19 | 2.304819039353000143 | 19 | 2.242386591846480037 |
| 20 | 3.304819039353000143 | 20 | 3.242386591846500021 |
+----+----------------------+----+----------------------+
20 rows in set (0.73 sec)
mysql> select max(id),min(id) from t_1;
+---------+---------+
| max(id) | min(id) |
+---------+---------+
| 1048576 | 1 |
+---------+---------+
1 row in set (1.40 sec)
mysql> select max(id),min(id) from t_2;
+---------+---------+
| max(id) | min(id) |
+---------+---------+
| 1048576 | 1 |
+---------+---------+
1 row in set (1.40 sec)
mysql> update t_2 set> Query OK, 1048576 rows affected (2 min 10.38 sec)
Rows matched: 1048576 Changed: 1048576 Warnings: 0
mysql> select max(id),min(id) from t_2;
+---------+---------+
| max(id) | min(id) |
+---------+---------+
| 2097152 | 1048577 |
+---------+---------+
1 row in set (1.63 sec)
mysql> explain select> +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|> +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | t_1 | range | PRIMARY | PRIMARY | 4 | NULL | 2 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)
在D主机上
mysql> create table t(id int not null auto_increment primary key, c_str char(20));
Query OK, 0 rows affected (0.00 sec)
插入两百万级别数据后。
mysql> select count(*) from t;
+----------+
| count(*) |
+----------+
| 2097152 |
+----------+
1 row in set (0.00 sec)
mysql> analyze table t;
+---------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+---------+----------+----------+
| t_boy.t | analyze | status | OK |
+---------+---------+----------+----------+
1 row in set (0.37 sec)
mysql> explain select> +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+
|> +----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+
| 1 | SIMPLE | t | range | PRIMARY | PRIMARY | 4 | NULL | 1132065 | Using where; Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+
1 row in set (0.00 sec)
和上面A主机上的查询相比,扫描的行数大减。
插入的速度主要靠网络。
看看A主机上的文件
[root@localhost t_boy]# cd /usr/local/mysql/data/t_boy/
[root@localhost t_boy]# ls -sihl
total 40K
15892559 8.0K -rw-rw---- 1 mysql mysql 61 Feb 28 11:03 db.opt
15892560 16K -rw-rw---- 1 mysql mysql 8.4K Feb 28 11:11 t_1.frm
15892561 16K -rw-rw---- 1 mysql mysql 8.4K Feb 28 11:14 t_2.frm
[root@localhost t_boy]#
这里只有表的定义而没有数据。
手册中关于FEDERATED引擎的介绍:
http://dev.mysql.com/doc/refman/ ... ated-storage-engine
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com