MySQL database and transaction storage engine

A relational database and non-relational databases

 1. relational database features:

  1) data is presented in tabular form

  2) the behavior of various records per name

  3) Each column name corresponding to the recorded data field

  4) Many of rows and columns form a

  5) composed of several forms database

 2. relational database advantages:

   2.1 complex query: You can use SQL statements easily between a table and multiple tables do very complex data queries.

   2.2 Transaction support: enables secure data access requirements for high performance can be achieved.

 3. Non-relational databases advantages:

   3.1 Performance: NOSQL is based on a key pair, it is conceivable to the corresponding relationship between the primary key and the values ​​in the table, and does not require the parsed SQL layer, the performance is very high.

   3.2 Scalability: as is also based on a key pair, there is no coupling between the data, it is very easy to expand horizontally.

 Problem: The difference between the three traditional database?

  A: between the three is based on the link between the data away anything else, is a tree-structured hierarchical database, network database link is a pointer structure, is a two-dimensional relational database table structure.

Second, the transaction (ACID)

  Transaction (transaction) is a series of operations as a single logical unit of work performed, these operations as a whole system to be submitted together, are either executed or not executed. A transaction is a logical unit of work integral.

  The transaction must have the following four properties, referred to as the ACID properties:

    Atomicity (Atomicity) : A transaction is a complete operation. Operation of each component of things are inseparable (atoms); or are executed or not executed;

    Consistency (Consistency) : When the transaction is completed, the data must be in a consistent state;

    Isolation (Isolation) : all concurrent transactions modify the data are isolated from each other, indicating that the transaction must be independent, it should not in any way affect or depend on other services;

    Permanent (Durability Rev) : After the transaction is complete, modify the database it is permanent, the transaction log to maintain permanent transaction.

  Transaction isolation level:

    Read uncommitted content (also known as dirty read read uncommitted): refers to all other matters can see the results of uncommitted transactions. There may be dirty reads, non-repeatable read and phantom read problems.

    Read submission (read commited): a transaction can only see changes in the firm to do that have been submitted. Avoid dirty reads, there may be non-repeatable read and phantom read problems.

    Repeatable read (repeatable read): Multiple instances of MySQL's default transaction isolation level, it ensures that the same transaction at the time of concurrent reads the data, you will see the same data row. Avoid dirty reads and non-repeatable read, phantom read may be a problem.

    Serializable (serializable): the highest level of isolation, it by adding a shared lock on each row of data read, making it impossible to conflict with each other, so as to solve the problem phantom read. Avoid dirty reads, non-repeatable read, phantom read.

  Problems caused by concurrent transactions:

    1. The lost update : two transactions T1 and T2, the same data is read and modify, the results submitted covers T2 T1 submitted results, results in a modification T1 is lost.

    2. Dirty read : Transaction T1 modifies a data and writes it back to disk, read the same data after the transaction T2, T1 is revoked for any reason, then T1 has been modified data recovery original value, T2 read data will not match the data in the database, the data is read as T2 "dirty" data, i.e. incorrect data.

    3. Non-repeatable read : means within a transaction, a plurality of times to read the same data. When this transaction is not over, another transaction also access the same data. So, between the two read data in the first transaction, due to the modification of the second transaction, then the first transaction data may be read twice is not the same. This happened in the same query twice within a transaction read data is not the same, so called non-repeatable read.

    4. Magic Reading : For example: the current payroll of 5,000 employees 10 people, Transaction A reads all wage of 5000 the number of 10 people. At this point, Transaction B inserts a record 5000 as wages. At this time, transaction A reads again wage of 5,000 employees, recorded as 11 people. At this point it arises phantom reads.

   Non-repeatable read and phantom read the difference? Non-repeatable read focus is modified, phantom read focus is added or deleted.

   MVCC (multi-version concurrency control mechanism): InnoDB is MVCC, by saving two hidden columns after each line records to achieve. Save time were the creation time and delete rows of lines (not the actual time values stored here, but the system version number). Each start a new transaction, the system will automatically increment the version number. System version number will be the start time of the transaction as the transaction ID, and version numbers for each query to the rows are compared, repeatable read after use MVCC phantom read phenomenon does not occur in isolation level.

Third, the storage engine

  Database storage engine is the underlying software organization, database management systems (DBMS) using the data engine to create, query, update, and delete data.

  1.InnoDB (clustered index mode)

    innodb underlying storage B + tree structure, each node of the tree corresponds to a innodb page, page size is fixed, usually to 16K. wherein the non-leaf nodes only key, leaf nodes contain the complete data.

    Be used: 1) frequently updated table is updated and the handling of multiple concurrent requests;

         2) support transaction-safe tables (ACID), and foreign key support line lock;

         3) can be recovered by the bin-log logs

    innodb If no primary key, will automatically generate a 6-byte main key (not visible to users).

  2.MyISAM (non-clustered index mode)

    Before MySQL 5.1 MyISAM is the default storage engine, with its emphasis on performance, but it does not support transactions, the row does not support foreign keys and locks, table locks support; When you insert or update data, you need to lock the entire table, efficiency it will be lower. Only cache index, not the real cache data. When MyISAM read data fast, and do not take up a lot of memory and storage resources.

    MyISAM允许没有主键的存在,是一种静态索引结构。

  问题:MySQL中MyISAM与InnoDB的区别?

   答:MySQL数据库中,最常用的两种引擎是innodb和myisam。InnoDB是目前MySQL的默认存储引擎。

     1)事务方面:MyISAM强调的是性能,查询速度比InnoDB类型更快,但是不支持事务。InnoDB提供事务支持。

     2)外键:MyISAM不支持外键,InnoDB支持外键。

     3)锁:MyISAM只支持表级锁,InnoDB支持行级锁和表级锁,默认为行级锁,行锁大幅度提高了多用户并发操作的性能。innodb比较适合于插入和更新操作比较多的情况,而myisam则适合于频繁查询的情况。另外,innodb表的行锁也不是绝对的,如果在执行一个SQL语句时,MySQL不能确定要扫描的范围,innodb同样会锁全表,例如:update table set num=1 where name like "%aaa%"。

     4)全文索引:MyISAM支持全文索引,Innodb不支持全文索引。innodb从MySQL5.6之后提供全文索引的支持。

     5)表主键:myisam允许没有主键的表存在;innodb:如果没有设定主键,就会自动生成一个6字节的主键(用户不可见)。

     6)表的具体行数:myisam:select count(*) from table,myisam只要简单的读出保存好的行数。因为myisam内置了一个计数器,count(*)时它直接从计数器中读。

               innodb:不保存表的具体行数,也就是说,执行select count(*) from table 时,innodb要扫描一遍整个表来计算有多少行。

Guess you like

Origin www.cnblogs.com/HuiH/p/11764263.html