MySQL storage engine and transaction characteristics

MySQL storage engine

image.png

MySQL storage engine

  • Provided as a pluggable component

    -MySQL service software that comes with the program function, process table processor

    - different storage engines have different functions and data storage

  • The default storage engine

    -MySQL 5.0/5.1  --->  MyISAM

    -MySQL 5.5/5.6  ---> InnoDB

 Example:

Execute SHOW ENGINES \ G can view a list of instructions, MySQL 5.6 storage engine available 9 species (except for the last FEDERATED, eight other support), which is used by default storage engine InnoDB

mysql> show engines\G
*************************** 1. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 4. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: MyISAM storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: CSV
     Support: YES
     Comment: CSV storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: Archive storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 9. row ***************************
      Engine: FEDERATED
     Support: NO
     Comment: Federated MySQL storage engine
Transactions: NULL
          XA: NULL
  Savepoints: NULL
9 rows in set (0.00 sec)


Storage engine configuration

  • Changes to the table storage engine

      Manually specify when construction of the table

          - When not specified, the default storage engine

          -show create table 表名\G;

mysql> 
mysql> 
mysql> create table innotb(
    -> id int(2)
    -> ) engine=innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> show create table innotb;
+--------+-------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                              |
+--------+-------------------------------------------------------------------------------------------+
| innotb | CREATE TABLE `innotb` (
  `id` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+-------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • Set the default storage engine

   /Etc/my.cnf modify configuration files

   -default-storage-engine = storage engine name

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure_file_priv="/myload"
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-storage-engine=myisam
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| default_storage_engine | MyISAM |
+------------------------+--------+
1 row in set (0.00 sec)


Storage Engine Features

myisam storage engine

  • main feature

       - Support for table-level locking

       - does not support transactions, the transaction is rolled back, foreign keys

  • Documents related tables

    - table name .frm

    - table name .MYI

    - 表 名 .MYD


InnoDB storage engine

  • main feature

       - supports row-level locking

       - Support for the transaction, the transaction is rolled back, foreign keys

  • Documents related tables

    - table name .frm, table name .ibd

    -ibdata1

    -ib_logfile0

    -ib_logfile1


MySQL locking mechanism

  • Lock granularity

    - table-level lock: once directly on the entire table to be locked.

    - row-level locking: lock only a line

    -页级锁:对整个页面(MySQL管理数据的基本存储单位)进行加锁

  • 锁类型

    -读锁(共享锁):支持并发读

    -写锁(互斥锁、排他锁):是独占锁,上锁期间其他线程不能读表或写表

  • 查看当前锁状态

    -检查Table_lock开头的变量,%作通配符

mysql> show status like 'table_lock%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Table_locks_immediate | 100   |
| Table_locks_waited    | 0     |
+-----------------------+-------+
2 rows in set (0.00 sec)


事务引入

      现实生活中,我们往往经常会进行转账操作,转账操作可以分为两部分来完成,转入和转出。只有这两部分都完成了才可以认为是转账成功。在数据库中,这个过程是使用两条语句来完成的,如果其中任意一条语句出现了异常没有执行,则会导致两个账号的金额不同步,造成错误。

  为了防止上面可能出现的情况,MySQL引入了事务,所谓事务就是针对数据库的一组操作,它可以由一条或者多条SQL语句组成,同一个事务的操作具备同步的特点,如果其中有一条语句不能执行的话,那么所有的语句都不会执行,也就是说,事务中的语句要么都执行,要么都不执行。

       在使用数据库时需要使用事务,必须先开启事务,具体语句如下:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

     上面语句是用来开启事务,事务开启之后就可以执行SQL语句,SQL语句执行成功之后,需要使用相应语句提交事务,提交事务的语句如下:

  commit;

需要注意的是,在MySQL中直接书写的SQL语句都是自动提交的,而事务中的操作语句需要使用commit语句手动提交,只有事务提交后其中的操作才会生效。

如果不想提交事务,我们还可以使用相关语句取消事务(也称回滚),具体语句如下:

  rollback;

需要注意的是,rollback语句只能针对未提交的事务执行的回滚操作,已经提交的事务是不能回滚的。




事务特性(ACID)

  • Atomic:原子性

    -事务的整个操作是一个整体,不可分割,要么全部成功,要么全部失败。

  • Consistency;一致性

    -事务操作的前后,表中的记录没有变化。

  • Isolation:隔离性

    -事务曹总时相互隔离不受影响的。

  • Durability;持久性

    -数据一旦提交,不可改变,永久改变表数据


例:

mysql> show variables like "autocommit";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> set autocommit=off;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like "autocommit";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

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

mysql> desc intab;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name  | varchar(4) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert intab values("bob");
Query OK, 1 row affected (0.00 sec)

mysql> select * from intab;
+------+
| name |
+------+
| bob  |
+------+
1 row in set (0.00 sec)

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

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









Guess you like

Origin blog.51cto.com/11483827/2415501