http://blog.csdn.net/xiaoyu714543065/article/details/8211265
A transaction has four characteristics: atomicity (atomicity), consistency (consistency), isolation (isolation), and persistence (durability). These four properties are referred to as ACID properties.
mysql5.5 is used by default InnoDB storage engine, where InnoDB and BDB provide transaction security tables , and other storage engines are non-transactional security tables.
show Engines and show variables like ' have% ' can list the engines that the current database supports. A record where value is displayed as disabled indicates that the database supports this engine and is disabled when the database is started. After MySQL5.1, there is a engines table in the INFORMATION_SCHEMA database that provides the same information as the show engines; The statement is exactly the same, and you can use the following statement to query which storage engines support the processing of things: Select engine From Information_chema.engines where transactions = ' yes ';
primary storage Engine: MyISAM, InnoDB, memory, and merge Description:
at the time of creating the table by engine= ... Or type= ... To specify what you want to use to the engine. Show Table status from DBname to view the specified table to the engine.
A MyISAM
it does not support transactions, does not support foreign keys, especially fast access, there is no requirement for transactional integrity, or a SELECT, insert-based application can basically use this engine to create tables.
Each myisam is stored as 3 files on disk, with the same file name and table names, but with the following extensions:
- . frm (save table definition)
- MYD (MYData, storing data)
- MYI (myindex, storage index)
Data files and index files can be placed in different directories, evenly allocated Io, to get faster speeds. To specify the path to the data file and the index file, you need to specify the file path by using Data directory and the index directory statement when creating the table, which requires an absolute path.
Each MyISAM table has a flag that is set by the server or MYISAMCHK program when it checks the MyISAM data table. The MyISAM table also has a flag indicating whether the data table has been shut down properly since it was last used. This flag can be used to determine if a data table needs to be checked and repaired if the server thinks it is a crash or a machine. If you want this check to occur automatically, you can use the--myisam-recover behavior when you start the server. This causes the server to automatically check the data table flags and perform the necessary repair processing each time a MyISAM data table is opened. Tables of type MyISAM may be corrupted, you can use the Check Table statement to check the health of the MyISAM table, and fix a corrupt to MyISAM table with the Repair table statement.
The MyISAM table also supports 3 different storage formats:
- static (fixed-length) table
- Dynamic tables
- Compression table
where the static table is the default storage format. The fields in the static table are non-variable length fields, so each record is fixed-length, the advantage of this storage method is that the storage is very fast, easy to cache, the fault is easy to recover, the disadvantage is that the space occupied is usually more than the dynamic table. A static table complements the spaces defined by the column definition when the data is stored, but does not receive these spaces when accessed, and these spaces are removed before they are returned to the app. It is also important to note that, in some cases, you may need to return a space after the field, and the following space will be automatically processed when using this format.
dynamic tables contain variable-length fields, where records are not fixed-lengths, so storage has the advantage of less space, but frequent updates to delete records can be fragmented, and you need to periodically execute optimize table statements or MYISAMCHK-R commands to improve performance. and recovery is relatively difficult in the event of a failure.
compressed tables are created by the Myisamchk tool and occupy very little space because each record is individually compressed, so there is very little access expense.
(ii) InnoDB
the InnoDB storage Engine provides transactional security with commit, rollback, and crash resiliency. However, compared to the MyISAM storage engine, InnoDB writes are less efficient and consume more disk space to preserve data and indexes.
1) Auto-Grow column:
the auto-grow column of the InnoDB table can be inserted manually, but if it is empty or 0 inserted, then the actual insert is automatically increased to the value. You can pass the ALTER TABLE ... Auto_increment=n; " Statement to set the starting value of the autogrow value by default to 1, but the force to default value is saved in memory and the value is lost after the database restarts. You can use LAST_INSERT_ID () to query the value used by the last inserted record for the current thread. If you insert more than one record at a time, the auto-grow value used by the first record is returned.
for InnoDB tables, the autogrow column must be an index. If it is a composite index, it must also be the first column of the combined index, but for the MyISAM table, the autogrow column can be the other column of the combined index, so that when the record is inserted, the autogrow column is incremented by the combined index to the previous column sort.
2) foreign KEY constraint:
MySQL supports foreign key storage engine only InnoDB, when creating foreign keys, the parent table must have a corresponding index, the child table when creating foreign keys will also automatically create the corresponding index.
When you create an index, you can specify the corresponding actions on the child table when the parent table is deleted, updated, including restrict, Cascade, set NULL, and no action. where restrict and no action are the same, it means that the parent table cannot be updated if the child table is associated, Casecade indicates that the parent table updates or deletes the records corresponding to the child table when updating or deleting, and set NULL indicates that the parent table is updated or deleted. The field corresponding to the child table is set NULL.
When a table is created with a foreign key reference by another table, the table's corresponding index or primary key is forbidden from deletion.
you can use set foreign_key_checks=0, temporarily turn off foreign key constraints, set Foreign_key_checks=1, and open constraints.
(iii) MEMORY
memory creates a table using the content that exists in it. Each memory table actually corresponds to a disk file in the format of. frm. The memory type of table access is very fast because it goes to the data in memory, and the hash index is used by default, but once the server shuts down, the data in the table is lost, but the table continues to exist.
by default, the memory data table uses a hash index, which makes the "equality comparison" very fast, but the "range comparison" is much slower. Therefore, the hash index value is suitable for use in the "=" and "<=>" operators, not suitable for use in the "<" or ">" operators, also not applicable in the order by sentence. If you do want to use the < or > or the betwen operator, you can use the Btree index to speed up.
The data rows stored in the Memory data table use the same length format, thus speeding up processing, which means that you cannot use a variable length data type such as BLOB and text. VARCHAR is a variable-length type, but it can be used because it is treated as a fixed-length char type within MySQL.
create table tab_memory engine=memory select id,name,age,addr from man order by id; |
Use the using Hash/btree to specify a specific to index.
create index mem_hash using hash on tab_memory(city_id); |
When starting the MySQL service, use the--init-file option to put statements such as insert into...select or load data infile into this file , you can load tables from a persistent data source when the service starts.
The server needs enough memory to maintain the memory table at the same time, and when it is no longer in use, to release memories that are used You should execute delete from or TRUNCATE TABLE or delete the entire table.
(iv) Merge
the merge storage engine is a combination of a set of MyISAM tables that must be identical in structure , there is no data in the merge table, and the merge type table can be queried, updated, and deleted, and these operations are actually operations on the internal MyISAM table. For the insert operation on the merge table, which is the inserted table defined by the INSERT_METHOD clause, there can be 3 different values, first and last values allow the insert operation to be performed on the initial or final table, without defining this clause or no, Indicates that the merge table cannot be inserted. You can drop operations on the merge table, which simply removes the definition of the merge table and has no effect on the internal table. The merge retains 2 files that start with the merge table name: the definition of the. frm file storage table; the. mrg file contains information about the combined table, including which tables are composed of the merge table, and the basis for inserting the data. Can be modified by. MRG file to modify the merge table, but will be refreshed by flush table after modification.
create table man_all(id int , name varchar (20))engine=merge union =(man1,man2) insert_methos= last ; |
features of several common storage engines
Here we focus on several common storage engines and compare the differences and recommended usage between each storage engine.
features |
Myisam |
BDB |
Memory |
InnoDB |
Archive |
Storage limits |
No |
No |
Yes |
64TB |
No |
Transaction security |
|
Support |
|
Support |
|
Lock mechanism |
Table lock |
Page lock |
Table lock |
Row lock |
Row lock |
B-Tree Index |
Support |
Support |
Support |
Support |
|
Hash index |
|
|
Support |
Support |
|
Full-Text Indexing |
Support |
|
|
|
|
Cluster index |
|
|
|
Support |
|
Data caching |
|
|
Support |
Support |
|
Index cache |
Support |
|
Support |
Support |
|
Data can be compressed |
Support |
|
|
|
Support |
Space use |
Low |
Low |
N/A |
High |
Very low |
Memory usage |
Low |
Low |
Medium |
High |
Low |
Speed of BULK Insert |
High |
High |
High |
Low |
Very high |
Support for foreign keys |
|
|
|
Support |
|
The 2 most commonly used storage engines:
- MyISAM is the default storage engine for MySQL. When create creates a new table, MyISAM is used by default when the storage engine for the new table is not specified. Each myisam is stored as three files on disk. The file name is the same as the table name, and the extension is. frm (store table definition), respectively. MYD (MYData, storing data),. MYI (myindex, storage index). Data files and index files can be placed in different directories, evenly distributed IO, for faster speeds.
- The InnoDB storage Engine provides transactional security with commit, rollback, and crash resiliency. However, compared to the MyISAM storage engine, InnoDB writes are less efficient and consume more disk space to preserve data and indexes.
How to choose the right storage engine
Selection criteria: According to the application characteristics of the appropriate storage engine, for the complex application system can be selected according to the actual situation of a variety of storage engine combinations.
The following are the applicable environments for common storage engines:
- MyISAM: Default MySQL plug-in storage engine, which is one of the most commonly used storage engines in the Web, data warehousing, and other application environments
- InnoDB: For transactional applications, with many features, including acid transaction support.
- Memory: Keep all your data in RAM and provide extremely fast access in environments where you need to quickly find references and other similar data.
- Merge: Allows a MySQL DBA or developer to logically group together a series of equivalent MyISAM tables and reference them as 1 objects. Ideal for VLDB environments such as data warehousing.
Database transaction management and storage engine