Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

MySQL 事務表與非事務表:MyISAM 與 InnoDB 的差別

編輯:關於 Android 開發

MySQL 事務表與非事務表:MyISAM 與 InnoDB 的差別

在查閱 MySQL 的 max_binlog_stmt_cache_size 參數說明時,會看到這麼一句話:「If nontransactional statements within a transaction require more than this many bytes of memory, the server generates an error.」那麼,究竟什麼是 nontransactional statements(非事務語句)呢?這就要先理解什麼是「非事務表」。

什麼是非事務表?

Nontransactional Tables 就是「不支援事務的表」,也就是使用 MyISAM 儲存引擎的表。它最大的特點是「不支援回滾」。看下面的例子:

> create table no_trans(id int) ENGINE=MyISAM;
> start transaction;
> insert into no_trans values(1);
> select * from no_trans;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

> rollback;
Query OK, 0 rows affected, 1 warning (0.00 sec)

> show warnings;
+---------+------+---------------------------------------------------------------+
| Level   | Code | Message                                                       |
+---------+------+---------------------------------------------------------------+
| Warning | 1196 | Some non-transactional changed tables couldn't be rolled back |
+---------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)

> select * from no_trans;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

可以看到,對非事務表執行 rollback 時會拋出警告,提示「部分非事務表的變更無法被回滾」,資料依然存在。

對照的事務表

與非事務表相對的,就是使用 InnoDB 這類支援事務的表,它能正常回滾:

> create table trans(id int);
> start transaction;
> insert into trans values(1);
> select * from trans;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

> rollback;
Query OK, 0 rows affected (0.00 sec)

> select * from trans;
Empty set (0.00 sec)

由此可見,nontransactional statements 指的就是「操作非事務表的語句」。

與參數的關聯

釐清這個概念後,就能理解兩個 binlog 快取參數的作用範圍:
- max_binlog_stmt_cache_size:影響的是「非事務表」(如 MyISAM),當空間不足時便會提示需要更多空間。
- max_binlog_cache_size:影響的是「事務表」(如 InnoDB),空間不足時同樣會提示需要更多空間。

掌握事務表與非事務表的差異,不僅能寫出正確的交易邏輯,也能幫助我們在調校 MySQL 參數時做出正確判斷。

轉載請注明:
十字螺絲釘
https://blog.chinaunix.net/uid/23284114.html
QQ:463725310
E-MAIL:houora#gmail.com(#請自行替換為@)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved