MySQL 5.5儲存引擎介紹

feelpurple發表於2016-04-13
儲存引擎是MySQL元件,用於處理不同型別的表的SQL操作。

InnoDB儲存引擎

預設和應用最廣泛的儲存引擎。支援事務,具有crash-recovery特性;支援行級鎖;支援主鍵和外來鍵。
InnoDB是MySQL中具有可靠性和高效能的一種通用儲存引擎。

優點:
DML操作遵循ACID模型(原子性、一致性、隔離性和永續性),支援事務,支援crash-recovery特性(當MySQL發生故障重啟後,對於InnoDB表,沒有完成的事務將透過redo日誌重新進行,已經提交但是沒有寫到資料檔案中的資料,將從doublewrite buffer中重新構建)以保護資料。

InnoDB buffer pool 快取被訪問的表和索引資訊,經常使用的資料直接從記憶體中讀取。

inserts,update,deletes操作被一種稱為change buffering的機制所最佳化。InnoDB不僅允許多併發讀寫同一張表,它還會快取發生改變的資料,最佳化磁碟I/O。

當資料庫執行大表的長時間查詢且反覆訪問相同表的相同行時,一種叫做Adaptive Hash Index的特性使這些查詢更快,就像資料從雜湊表中查詢出來一樣。

可以壓縮表和相關的索引。

可以對效能和可用性造成很小影響地建立和刪除索引。

可以很快TRUNCATE掉一個file_per_table表空間,釋放出磁碟空間供作業系統使用,而不必釋放出僅能供InnoDB所重用的系統表空間。

支援行級鎖和一致性讀,提高多使用者的併發性和效能。

支援主鍵,提高查詢效能。

為了保持資料的完整性,InnoDB也支援外來鍵。

你可以將InnoDB表與MySQL其他儲存引擎的表自由組合在一起使用。例如,在一個SQL中,你可以關聯一張InnoDB表和一個記憶體表。

在處理大量資料的時候,InnoDB引擎可以有效的發揮CPU效率和提升效能。

MyISAM儲存引擎

表級鎖會限制讀寫的效能,所以這個儲存引擎通常用於只讀或以讀為主的網站資料和資料倉儲配置中。

MyISAM表有下面特點:

所有資料的值會先以低位元組儲存,這使得存放資料的機器和作業系統相互獨立。

所有數字鍵值會先以高位元組儲存,這樣會使索引更好地壓縮。

支援檔案系統和作業系統上面的大檔案(63位檔案長度)。

MyISAM表中行數的限制是(232)2 (1.844E+19)。

每張MyISAM表最多可以建立64個索引,聯合索引做多支援16個欄位。

最大鍵長度為1000位元組,這個可以透過原始碼重新編譯。如果想讓一個鍵的長度大於250位元組,需要使用大於預設1024位元組的鍵塊。

當行按順序插入到MyISAM表中的時候,例如你使用了AUTO_INCREMENT欄位,索引樹會被分割,高節點會只包含一個鍵值,這會提高索引樹空間的利用。

支援每個表AUTO INCREMENT欄位的內部處理。MyISAM會自動更新這個欄位的插入和更新操作。這使得AUTO INCREMENT欄位序列處理能力更快(至少10%)。當序列被刪除後,序列的最高值不會被重用。

當MYISAM表的delete操作和update和update操作同時存在時,會在表中產生碎片,動態分配大小的行可以有效減少碎片。這個是資料庫透過把已刪除的相鄰行合併在一起以及擴充套件刪除的塊自動實現的。

MyISAM表支援併發插入。如果一張表沒有空閒的資料塊了,你可以在其他執行緒正在讀這張表的同時,插入新的行到這張表中。

你可以把資料檔案和索引檔案放在不同的物理裝置上,這樣可以提高對錶的讀寫速度。

--在不同的路徑下指定分割槽表的不同分割槽位置
mysql> create table t_partition(id int,name varchar(30),adate date) engine=myisam
    -> partition by list(year(adate))
    -> (
    ->   PARTITION p1999 VALUES IN (1995, 1999, 2003)
    ->     DATA DIRECTORY = '/appdata/95/data'
    ->     INDEX DIRECTORY = '/appdata/95/idx',
    ->   PARTITION p2000 VALUES IN (1996, 2000, 2004)
    ->     DATA DIRECTORY = '/appdata/96/data'
    ->     INDEX DIRECTORY = '/appdata/96/idx',
    ->   PARTITION p2001 VALUES IN (1997, 2001, 2005)
    ->     DATA DIRECTORY = '/appdata/97/data'
    ->     INDEX DIRECTORY = '/appdata/97/idx',
    ->   PARTITION p2002 VALUES IN (1998, 2002, 2006)
    ->     DATA DIRECTORY = '/appdata/98/data'
    ->     INDEX DIRECTORY = '/appdata/98/idx'
    -> ) ;
Query OK, 0 rows affected (0.11 sec)

mysql> insert into t_partition values(100,'Neo',date'2016-04-12');
ERROR 1526 (HY000): Table has no partition for value 2016
mysql> insert into t_partition values(100,'Neo',date'1995-04-12');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_partition values(200,'Tom',date'1997-04-12');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t_partition;
+------+------+------------+
| id   | name | adate      |
+------+------+------------+
|  100 | Neo  | 1995-04-12 |
|  200 | Tom  | 1997-04-12 |
+------+------+------------+
2 rows in set (0.06 sec)

[root@localhost data]# ls /appdata/95/data/
t_partition#P#p1999.MYD
[root@localhost data]# ls /appdata/95/idx/
t_partition#P#p1999.MYI
[root@localhost data]# ls /appdata/97/data/
t_partition#P#p2001.MYD
[root@localhost data]# ls /appdata/97/idx/
t_partition#P#p2001.MYI
[root@localhost data]# ls /appdata/98/idx/
t_partition#P#p2002.MYI
[root@localhost data]# ls /appdata/98/data
t_partition#P#p2002.MYD

可以為BLOB和TEXT建立索引。

索引欄位可以包括空值,每個鍵佔據0到1位元組。

每個字元欄位可以使用不同的字符集。

在MyISAM索引檔案中有一個標識,這個標識可以判斷表是否正確關閉。如果mysqld啟動服務的時候帶上了--myisam-recover-options 引數,當資料庫開啟的時候,MyISAM表會自動檢查,當MyISAM表沒有正確關閉的時候會自動修復。

可以透過 myisamchk 工具來檢查MyISAM表。

可以透過myisampack工具來壓縮BLOB和VARCHAR欄位。

支援真VARCHAR型別,一個VARCHAR欄位可以儲存一個或兩個位元組。

帶有VARCHAR欄位的表可以有固定或動態的行長。

--建立測試表

mysql> create table t_myd5(id int,v1 char(10)) engine=myisam;
Query OK, 0 rows affected (0.83 sec)

mysql> show table status like 't_myd5'\G
*************************** 1. row ***************************
           Name: t_myd5
         Engine: MyISAM
        Version: 10
     Row_format: Fixed
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 9851624184872959
   Index_length: 1024
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-04-13 06:03:53
    Update_time: 2016-04-13 06:03:53
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.08 sec)

mysql> create table t_myd6(id int,v1 char(10)) row_format=fixed engine=myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> show table status like 't_myd6'\G
*************************** 1. row ***************************
           Name: t_myd6
         Engine: MyISAM
        Version: 10
     Row_format: Fixed
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 9851624184872959
   Index_length: 1024
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-04-13 06:05:47
    Update_time: 2016-04-13 06:05:47
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: row_format=FIXED
        Comment: 
1 row in set (0.02 sec)

mysql> create table t_myd7(id int,v1 char(10)) row_format=dynamic engine=myisam;
Query OK, 0 rows affected (0.06 sec)

mysql> show table status like 't_myd7'\G
*************************** 1. row ***************************
           Name: t_myd7
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 281474976710655
   Index_length: 1024
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-04-13 06:08:13
    Update_time: 2016-04-13 06:08:13
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: row_format=DYNAMIC
        Comment: 
1 row in set (0.00 sec)

在一張表中,VARCHAR或CHAR欄位總長度為64K。

--建立測試表
mysql> create table t_myd2(id int) engine=myisam;
Query OK, 0 rows affected (0.07 sec)

--檢視建立的資料檔案,.frm檔案存放表結構檔案,.MYI檔案存放索引檔案,.MYD檔案存放資料檔案
[root@localhost fire]# ls -trl
total 656
-rw-rw----. 1 mysql mysql   8556 Apr 12 00:28 t_myd2.frm
-rw-rw----. 1 mysql mysql   1024 Apr 12 00:28 t_myd2.MYI
-rw-rw----. 1 mysql mysql      0 Apr 12 00:28 t_myd2.MYD

MEMORY儲存引擎

將所有的資料存放在記憶體(RAM)中,應用的場景:非關鍵資料實現快速訪問。這個儲存引擎原來被稱為HEAP引擎。它的應用場合在減少,因為InnoDB可以透過buffer pool memory將大多數的資料保留在記憶體中,並且更加的可靠安全;同時NDBCLUSTER對大的資料集提供了基於鍵值的快速查詢。

這個儲存引擎適合存放短暫、非核心的資料,當MySQL伺服器停止或重啟時,記憶體中的資料將會丟失。

它可以實現資料的快速訪問和低延遲,可以將資料完全載入到記憶體中,而不會導致作業系統虛擬記憶體頁的交換。

它適合只讀或以讀為主的資料訪問模式(資料更新很少)。

它預設使用雜湊索引,而不是B+樹索引。

記憶體表最大的容量不能超過max_heap_table_size這個系統引數,預設值時16MB。

雖然Memory儲存引擎速度非常快,但在使用上還是有一定的限制。比如,其只支援表鎖,併發效能較差,並且不支援TEXT和BLOB列型別。最重要的是,儲存變長欄位(varchar)時是按照欄位(char)的方式進行的,因此會浪費記憶體。此外有一點常被忽視的是,MySQL資料庫使用Memory儲存引擎作為臨時表來存放查詢的中間結果集(intermediate result)。如果中間結果集大於Memory儲存引擎表的容量設定,又或者中間結果含有TEXT或BLOB列型別欄位,則MySQL資料庫會把其轉換到MyISAM儲存引擎表而存放到磁碟。MyISAM不快取資料檔案,因此這時產生的臨時表的效能對於查詢會有損失。

CSV儲存引擎

使用這個儲存引擎的表實際上是用逗號分隔的文字檔案。CSV表可以讓你以CSV格式匯入和匯出資料。CSV表不能建立索引,你可以在正常的操作時使用InnoDB表,只在匯出和匯出資料階段使用CSV表。

--建立測試表
mysql> create table t_csv1 (id int not null default 0,v1 varchar(20) not null default '') engine=csv;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t_csv1 values(1,'a');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t_csv1 values(2,'b');
Query OK, 1 row affected (0.00 sec)

--檢視資料檔案中生成的表結構檔案(.frm)、後設資料檔案(.CSM)和表資料檔案(.CSV)
[root@localhost firedb]# ls -trl
total 172
-rw-rw----. 1 mysql mysql  8582 Apr  9 19:09 t_csv1.frm
-rw-rw----. 1 mysql mysql    35 Apr  9 19:11 t_csv1.CSM
-rw-rw----. 1 mysql mysql    12 Apr  9 19:11 t_csv1.CSV

可以透過check語句來檢查CSV表中資料的有效性,檢查的時候會掃描欄位分隔符,判斷出正確的欄位以及格式不匹配的資料。

mysql> check table t_csv1;
+---------------+-------+----------+----------+
| Table         | Op    | Msg_type | Msg_text |
+---------------+-------+----------+----------+
| firedb.t_csv1 | check | status   | OK       |
+---------------+-------+----------+----------+
1 row in set (0.06 sec)

可以透過REPAIR語句來修復損壞的CSV表。這個操作會修復表中的有限資料,同時表中損壞的資料將會丟失。

mysql> repair table t_csv1;
+---------------+--------+----------+----------+
| Table         | Op     | Msg_type | Msg_text |
+---------------+--------+----------+----------+
| firedb.t_csv1 | repair | status   | OK       |
+---------------+--------+----------+----------+
1 row in set (0.01 sec)

Archive儲存引擎

使用這個儲存引擎的表,資料排列緊湊而不能建立索引,用於存放和查詢資料量大的歷史、歸檔或安全審計資訊。
Archive儲存引擎支援INSERT, REPLACE, and SELECT操作,不支援DELETE和UPDATE操作,也不支援排序、BLOB欄位。
Archive儲存引擎使用行級鎖。插入的Archive表中的資料會被壓縮,Archive儲存引擎使用zlib資料壓縮方法。

--建立測試表,兩個表中存放相同的資料,使用Myisam引擎的表使用了517696位元組,而使用Archive引擎的表使用了68904位元組

mysql> create table t_mi1 engine=myisam as select * from information_schema.columns;
Query OK, 509 rows affected (0.12 sec)
Records: 509  Duplicates: 0  Warnings: 0

mysql> insert into t_mi1 select * from t_mi1;
Query OK, 509 rows affected (0.00 sec)
Records: 509  Duplicates: 0  Warnings: 0

mysql> insert into t_mi1 select * from t_mi1;
Query OK, 1018 rows affected (0.01 sec)
Records: 1018  Duplicates: 0  Warnings: 0

mysql> insert into t_mi1 select * from t_mi1;
Query OK, 2036 rows affected (0.01 sec)
Records: 2036  Duplicates: 0  Warnings: 0

mysql> show table status like 't_mi1'\G
*************************** 1. row ***************************
           Name: t_mi1
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 4072
 Avg_row_length: 127
    Data_length: 517696
Max_data_length: 281474976710655
   Index_length: 1024
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2016-04-11 23:55:41
    Update_time: 2016-04-11 23:55:54
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.11 sec)

mysql> create table t_arc1 engine=archive as select * from t_mi1;
Query OK, 4072 rows affected (0.21 sec)
Records: 4072  Duplicates: 0  Warnings: 0

mysql> show table status like 't_arc1'\G
*************************** 1. row ***************************
           Name: t_arc1
         Engine: ARCHIVE
        Version: 10
     Row_format: Compressed
           Rows: 4072
 Avg_row_length: 16
    Data_length: 68904
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: NULL
    Update_time: 2016-04-12 00:05:26
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.20 sec)

--檢視建立的表檔案,表的定義檔案是.frm檔案,實際存放資料的檔案是.ARZ檔案
[root@localhost fire]# ls -trl
total 640
-rw-rw----. 1 mysql mysql  13552 Apr 12 00:05 t_arc1.frm
-rw-rw----. 1 mysql mysql  68904 Apr 12 00:05 t_arc1.ARZ

Blackhole儲存引擎

這個儲存引擎接受資料的插入但是並不儲存資料,有點類似Unix下的/dev/null裝置。對Blackhole表的查詢通常返回值為空。Blackhole表可以用在複製的配置中,當DML語句傳送到備用伺服器時,主伺服器不儲存它自己的資料複製。
Blackhole儲存引擎支援各種索引。插入到Blackhole表的資料將不會存在於此表中,但是如果資料庫開啟了二進位制日誌,相關的SQL語句會被記錄並複製到備用伺服器。這個特性在將某個MySQL資料庫當作中繼器或過濾器的時候很有用。

--建立測試表

mysql> create table t_bl1(i int,c char(10)) engine=blackhole;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t_bl1 values(1,'record one'),(2,'record two');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t_bl1;
Empty set (0.00 sec)

--資料檔案中只有表的定義檔案

[root@localhost firedb]# ls -trl
total 728
-rw-rw----. 1 mysql mysql   8578 Apr 10 06:01 t_bl1.frm

Merge儲存引擎

這個儲存引擎也被稱為MRG_MyISAM儲存引擎,可以將一系列具有相同列和索引的MyISAM表邏輯地組合成一個資料物件,對於資料倉儲環境很有用。當要組合的表中的列的順序不一致時,不能使用Merge儲存引擎。和Merge表相對應的是分割槽表,分割槽表將單個表中的資料存放到不同的檔案中。

--建立兩張結構相同的Mysiam表

mysql> create table t_mg1 (id int not null auto_increment primary key,v1 varchar(20)) engine=myisam;
Query OK, 0 rows affected (0.06 sec)

mysql> create table t_mg2 (id int not null auto_increment primary key,v1 varchar(20)) engine=myisam;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t_mg1(v1) values('This'),('ls'),('mysl');
Query OK, 3 rows affected (0.09 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into t_mg1(v1) values('This'),('ls'),('mys2');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t_mg1;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mysl |
|  4 | This |
|  5 | ls   |
|  6 | mys2 |
+----+------+
6 rows in set (0.00 sec)

mysql> insert into t_mg2(v1) values('This'),('ls'),('mys3');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into t_mg2(v1) values('This'),('ls'),('mys4');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t_mg1;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mysl |
|  4 | This |
|  5 | ls   |
|  6 | mys2 |
+----+------+
6 rows in set (0.00 sec)

mysql> select * from t_mg2;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mys3 |
|  4 | This |
|  5 | ls   |
|  6 | mys4 |
+----+------+
6 rows in set (0.00 sec)

--建立MERGE表,將之前建立的兩張表合併到一起

mysql> create table t_mer1(id int not null auto_increment primary key,v1 varchar(20)) engine=merge union=(t_mg1,t_mg2);
Query OK, 0 rows affected (0.06 sec)

mysql> select * from t_mer1;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mysl |
|  4 | This |
|  5 | ls   |
|  6 | mys2 |
|  1 | This |
|  2 | ls   |
|  3 | mys3 |
|  4 | This |
|  5 | ls   |
|  6 | mys4 |
+----+------+
12 rows in set (0.00 sec)

--檢視建立的生成檔案
.frm裡面存放的是表結構資訊,.MRG裡面存放的是資料來源於哪些表,實際上建立出來的MERGE表裡面使用的還是源表的資料

[root@localhost firedb]# ls -trl
total 804
-rw-rw----. 1 mysql mysql   8582 Apr 10 07:10 t_mg1.frm
-rw-rw----. 1 mysql mysql   8582 Apr 10 07:10 t_mg2.frm
-rw-rw----. 1 mysql mysql   2048 Apr 10 07:11 t_mg1.MYI
-rw-rw----. 1 mysql mysql    120 Apr 10 07:11 t_mg1.MYD
-rw-rw----. 1 mysql mysql   2048 Apr 10 07:13 t_mg2.MYI
-rw-rw----. 1 mysql mysql    120 Apr 10 07:13 t_mg2.MYD
-rw-rw----. 1 mysql mysql   8582 Apr 10 07:15 t_mer1.frm
-rw-rw----. 1 mysql mysql     12 Apr 10 07:15 t_mer1.MRG

[root@localhost firedb]# cat t_mer1.MRG 
t_mg1
t_mg2

--向源表t_mg1裡面插入兩條記錄,資料會直接出現在MERGE表t_mer1中

mysql> insert into t_mg1 values(8,'car');
Query OK, 1 row affected (0.02 sec)
mysql> insert into t_mg1(v1) values('car2');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t_mg1;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mysl |
|  4 | This |
|  5 | ls   |
|  6 | mys2 |
|  8 | car  |
|  9 | car2 |
+----+------+
8 rows in set (0.00 sec)

mysql> select * from t_mer1;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mysl |
|  4 | This |
|  5 | ls   |
|  6 | mys2 |
|  8 | car  |
|  9 | car2 |
|  1 | This |
|  2 | ls   |
|  3 | mys3 |
|  4 | This |
|  5 | ls   |
|  6 | mys4 |
+----+------+
14 rows in set (0.06 sec)

--可以向MERGE表插入資料,透過insert_method屬性決定向源表的哪張表插入資料,insert_method last代表的是最後一張源表

mysql> alter table t_mer1 insert_method last;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from t_mg2;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mys3 |
|  4 | This |
|  5 | ls   |
|  6 | mys4 |
+----+------+
6 rows in set (0.00 sec)

mysql> insert into t_mer1(v1) values('car5')
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from t_mg2;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mys3 |
|  4 | This |
|  5 | ls   |
|  6 | mys4 |
| 10 | car5 |
+----+------+
7 rows in set (0.00 sec)

mysql> select * from t_mer1;
+----+------+
| id | v1   |
+----+------+
|  1 | This |
|  2 | ls   |
|  3 | mysl |
|  4 | This |
|  5 | ls   |
|  6 | mys2 |
|  8 | car  |
|  9 | car2 |
|  1 | This |
|  2 | ls   |
|  3 | mys3 |
|  4 | This |
|  5 | ls   |
|  6 | mys4 |
| 10 | car5 |
+----+------+
15 rows in set (0.00 sec)


MariaDB [test]> create table payment_2006(
    -> country_id smallint,
    -> payment_date datetime,
    -> amount decimal(15,2),
    -> key idx_fk_country_id(country_id))
    -> engine=myisam;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> create table payment_2007(
    -> country_id smallint,
    -> payment_date datetime,
    -> amount decimal(15,2),
    -> key idx__fk_country_id(country_id))
    -> engine=myisam;
Query OK, 0 rows affected (0.01 sec)

MariaDB [test]> create table payment_all(
    -> country_id smallint,
    -> payment_date datetime,
    -> amount decimal(15,2),
    -> index(country_id))
    -> engine=merge union=(payment_2006,payment_2007) insert_method=last;
Query OK, 0 rows affected (0.09 sec)

MariaDB [test]> insert into payment_2006 values(1,'2006-05-01',100000),(2,'2006-08-15',150000);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [test]> insert into payment_2007 values(1,'2007-02-20',35000),(2,'2007-07-15',220000);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [test]> select * from payment_2006;
+------------+---------------------+-----------+
| country_id | payment_date        | amount    |
+------------+---------------------+-----------+
|          1 | 2006-05-01 00:00:00 | 100000.00 |
|          2 | 2006-08-15 00:00:00 | 150000.00 |
+------------+---------------------+-----------+
2 rows in set (0.00 sec)

MariaDB [test]> select * from payment_2007;
+------------+---------------------+-----------+
| country_id | payment_date        | amount    |
+------------+---------------------+-----------+
|          1 | 2007-02-20 00:00:00 |  35000.00 |
|          2 | 2007-07-15 00:00:00 | 220000.00 |
+------------+---------------------+-----------+
2 rows in set (0.00 sec)

MariaDB [test]> select * from payment_all;
+------------+---------------------+-----------+
| country_id | payment_date        | amount    |
+------------+---------------------+-----------+
|          1 | 2006-05-01 00:00:00 | 100000.00 |
|          2 | 2006-08-15 00:00:00 | 150000.00 |
|          1 | 2007-02-20 00:00:00 |  35000.00 |
|          2 | 2007-07-15 00:00:00 | 220000.00 |
+------------+---------------------+-----------+
4 rows in set (0.00 sec)

--由於使用的是LAST方法,向MERGE表中插入資料,會向建表時的最後一張表插入資料

MariaDB [test]> insert into payment_all values(3,'2006-03-31',112200);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from payment_all;
+------------+---------------------+-----------+
| country_id | payment_date        | amount    |
+------------+---------------------+-----------+
|          1 | 2006-05-01 00:00:00 | 100000.00 |
|          2 | 2006-08-15 00:00:00 | 150000.00 |
|          1 | 2007-02-20 00:00:00 |  35000.00 |
|          2 | 2007-07-15 00:00:00 | 220000.00 |
|          3 | 2006-03-31 00:00:00 | 112200.00 |
+------------+---------------------+-----------+
5 rows in set (0.00 sec)

MariaDB [test]> select * from payment_2006;
+------------+---------------------+-----------+
| country_id | payment_date        | amount    |
+------------+---------------------+-----------+
|          1 | 2006-05-01 00:00:00 | 100000.00 |
|          2 | 2006-08-15 00:00:00 | 150000.00 |
+------------+---------------------+-----------+
2 rows in set (0.00 sec)

MariaDB [test]> select * from payment_2007;
+------------+---------------------+-----------+
| country_id | payment_date        | amount    |
+------------+---------------------+-----------+
|          1 | 2007-02-20 00:00:00 |  35000.00 |
|          2 | 2007-07-15 00:00:00 | 220000.00 |
|          3 | 2006-03-31 00:00:00 | 112200.00 |
+------------+---------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [test]> show keys from payment_2006;
+--------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name          | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| payment_2006 |          1 | idx_fk_country_id |            1 | country_id  | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
+--------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

MariaDB [test]> show keys from payment_2007;
+--------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name           | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| payment_2007 |          1 | idx__fk_country_id |            1 | country_id  | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
+--------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

MariaDB [test]> show keys from payment_all;
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table       | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| payment_all |          1 | country_id |            1 | country_id  | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
+-------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
Federated儲存引擎

這個儲存引擎使提供連線到其他MySQL伺服器來建立邏輯資料庫的能力,對於分散式資料庫或資料集市很有用。

--在目標端建立表
mysql> create table poll_vote(id int(10) not null auto_increment,parents_id int(10) not null,
    -> vote_count mediumint(10) not null default 0,vote_month_count mediumint(10) not null default 0,
    -> vote_month mediumint(10) not null,primary key(id),unique key ind_poll_vote_baike(parents_id,vote_month))
    -> engine=innodb auto_increment=26020 default charset=latin1;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into poll_vote(parents_id,vote_count,vote_month_count,vote_month) values(10,100,100,100);
Query OK, 1 row affected (0.07 sec)

--在源端建立表,結構和目標端的表結構一致
mysql> create table poll_vote(id int(10) not null auto_increment,parents_id int(10) not null,
    -> vote_count mediumint(10) not null default 0,vote_month_count mediumint(10) not null default 0,
    -> vote_month mediumint(10) not null,primary key(id),unique key ind_poll_vote_baike(parents_id,vote_month))
    -> engine=federated auto_increment=26020 default charset=latin1
    -> connection='mysql://test:System#2013@192.168.78.137/fire/poll_vote';
Query OK, 0 rows affected (0.08 sec)
mysql> select * from poll_vote;
+-------+------------+------------+------------------+------------+
| id    | parents_id | vote_count | vote_month_count | vote_month |
+-------+------------+------------+------------------+------------+
| 26020 |         10 |        100 |              100 |        100 |
+-------+------------+------------+------------------+------------+
1 row in set (2.01 sec)

--當你建立了一張FEDERATED表時,表的定義檔案(.frm檔案)會存在於本地,表的實際資料檔案則存放在遠端資料庫伺服器。
--檢視建立出來的表
[root@localhost fire]# ls -trl
total 28
-rw-rw----. 1 mysql mysql   61 Apr 11 07:06 db.opt
-rw-rw----. 1 mysql mysql 8736 Apr 11 19:39 poll_vote.frm

連線的示例
connection='mysql://username:password@hostname:port/database/tablename'
connection='mysql://username@hostname/database/tablename'
connection='mysql://username:password@hostname/database/tablename'

使用另外一種方式建立表

如果你在相同的伺服器上建立了多張FEDERATED表,或者你想簡化建立FEDERATED表的流程,可以使用CREATE SERVER語句來定義要連線的伺服器引數。

mysql> create server db_01 foreign data wrapper mysql 
    -> options (user 'test',password 'System#2013', host '192.168.78.137', port 3306,database 'fire');
Query OK, 1 row affected (0.07 sec)

mysql> select * from mysql.servers\G
*************************** 1. row ***************************
Server_name: db_01
       Host: 192.168.78.137
         Db: fire
   Username: test
   Password: System#2013
       Port: 3306
     Socket: 
    Wrapper: mysql
      Owner: 
1 row in set (0.00 sec)

mysql> create table poll_vote_2(id int(10) not null auto_increment,parents_id int(10) not null,
    -> vote_count mediumint(10) not null default 0,vote_month_count mediumint(10) not null default 0,
    -> vote_month mediumint(10) not null,primary key(id),unique key ind_poll_vote_baike(parents_id,vote_month))
    -> engine=federated auto_increment=26020 default charset=latin1
    -> connection='db_01/poll_vote';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from poll_vote_2;
+-------+------------+------------+------------------+------------+
| id    | parents_id | vote_count | vote_month_count | vote_month |
+-------+------------+------------+------------------+------------+
| 26020 |         10 |        100 |              100 |        100 |
+-------+------------+------------+------------------+------------+
1 row in set (0.08 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2080516/,如需轉載,請註明出處,否則將追究法律責任。

相關文章