MySQL (02) - Storage Engine

MySQL (02) - Storage Engine

Storage engine of choice

  1. `Enter the command show enginesto view the current support database storage engines: you can see the default database engine InnoDB, you can also see the introduction of engine-related features simple

    mysql> show engines;
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    | Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    | InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
    | MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
    | MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
    | BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
    | MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
    | CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
    | ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
    | PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
    | FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    rows in set
  2. Enter the command SHOW VARIABLES LIKE '%storage_engine'to see which database engine is used by default (if blank try SHOW VARIABLES LIKE '%storage_engine%')

    mysql> show variables like '%storage_engine';
    +----------------------------------+--------+
    | Variable_name                    | Value  |
    +----------------------------------+--------+
    | default_storage_engine           | InnoDB |
    | default_tmp_storage_engine       | InnoDB |
    | internal_tmp_disk_storage_engine | InnoDB |
    +----------------------------------+--------+
    rows in set
  3. When you create a table, you can use the command: CREATE TABLE table_name(...)ENGINE = InnoDB/MyISAM DEFAULT CHARSET=gbkyou can specify the database engine

  4. You can use show create table table_namethe statement to see the SQL table creation statements that can see the engine used by the table

  5. Execute commands ALTER TABLE tablename engine = InnoDBto change the engine table


Storage engine features

InnoDB engine

Features:

  1. InnoDB storage engine provided with a commit, rollback, and crash recovery capabilities of transaction security . Compared MyISAM storage engine, InnoDB write processing efficiency and almost take up more disk space and data retention index
  2. Provides database ACID support (atomicity Atomicity, Consistency Consistency, Isolation Isolation, persistence Durability) transactions to achieve the SQL standard four levels of isolation
  3. Design goal isProcessing large capacity database systems, MySQL InnoDB runtime will create buffer pools in memory for buffering data and index
  4. Execution select count(*) from tablerequires a full table scan when the statement, simply because InnoDBnot like MyISAMthat does not save a specific number of rows, so the need to scan the entire table to calculate how many rows
  5. Because it isRow lock, particle size is smaller, so the write operation does not lock the entire table, in a high concurrent use InnoDB will increase efficiency.I.e. the presence of a large number of UPDATE/INSERToperation, high efficiency
  6. InnoDBIt does not support the FULLTEXTtype of index that does not support full-text indexing
  7. InnoDBEmpty large amount of data tables, is very slow, because InnoDByou must process each row in the table, based on InnoDBthe design principles of the transaction, we first need to "deletion" write "transaction log" and then write the actual table. Therefore, a large empty table, the best direct drop tableand reconstruction. That is, InnoDBline by line delete, will not rebuild the table

scenes to be used:

  1. Often UPDETE / INSERT table, using the processing of multiple concurrent update request
  2. Support transactions, InnoDB Required
  3. You can be recovered (+ transaction rollback log) from a disaster
  4. Foreign key constraints, attributes AUTO_INCREMENT column support

Unlike other engine characteristics Introduction:

  1. Automatic growth column:

    1. Auto grow columns may be inserted by hand, but if the insert is null or 0, the actual value of the increment.
    2. Use ALTER TABLE tablename AUTO_INCREMENT = forcibly set the initial value of the growth column initial_value, the default is one. But the initial_value value is stored in memory, restart the database before starting if you use this value, then this mandatory default values ​​will be lost, it will need to be reset.
    3. Execution select LAST_INSERT_ID () you can view the current value of the last inserted record thread used, but once inserted a lot of records, then returns the value from the first strip. I.e. insert tablename values ​​(1), (2), (3) is returned value (1) self-value.
    4. Since inclusion must be InnoDB index (primary key of a clustered index), if the combination of the index, it must be the first column of the composite index. But may be any other combination of columns MyISAM index (incremented after sorted according to the composite index front columns), i.e. index (col1, col2) is the auto-increment col1
  2. Foreign key constraint:

    1. Only InnoDB to MySQL only supported key, foreign key is created when the requirements of the parent table must have a corresponding index, a sub table will automatically create a corresponding index when creating the foreign key.

    2. In the creation of the index, can be specified in the delete, update the parent table, the corresponding sub-table operation, such as: RESTRICT, CASCADE, SET NULL, NO ACTION (the same RESTRICT)

      RESTRICT: limit the child table when the association has recorded the parent table can not be updated.

          CASCADE: the parent table when the update or delete, update / delete child table corresponding to the record.

          SET NULL: Delete or update the parent table when the child table corresponding field in the SET NULL

          For example: the child table foreign key specified time has ON DELETE RESTRICT ON UPDATE CASCADE, indicate when to delete the parent table, the child table has a corresponding record can not be deleted, when updating records in the main table, the child table if the child has a corresponding record table corresponding to the update.

      CONSTRAINT fk_student_course FOREIGN KEY(student_id) REFERENCES student(student_id) ON DELETE RESTRICT ON UPDATE CASCADE;
    3. When a table is created with reference to foreign keys in other tables, the table corresponding index or primary key is deleted ban. If the operation LOAD DATA and ALTER TABLE temporarily closing foreign key checks can speed up processing, executed SET FOREIGN_KEY_CHECKS = 0 off, after performing SET FOREIGN_KEY_CHECKS = 1 restitution. You can view index keys with the command

      mysql> show index from user;
      +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
      +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      | user  |          0 | PRIMARY  |            1 | id          | A         |           3 | NULL     | NULL   |      | BTREE      |         |               |
      | user  |          0 | PRIMARY  |            2 | major_id    | A         |           3 | NULL     | NULL   |      | BTREE      |         |               |
      +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      rows in set
      
      mysql> show keys from user;
      +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
      +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      | user  |          0 | PRIMARY  |            1 | id          | A         |           3 | NULL     | NULL   |      | BTREE      |         |               |
      | user  |          0 | PRIMARY  |            2 | major_id    | A         |           3 | NULL     | NULL   |      | BTREE      |         |               |
      +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
      rows in set
  3. Storage format: InnoDB storage tables and indexes There are two main ways, one shared table space for storage, and second, the use of multiple tables (separate) storage space.

    1. Table space using a shared storage: table and table structure stored in the .frm file (in fact nothing to do with engine), and index data stored in innodb_data_home_dir and innodb_data_file_path table space, may be more than one file. MySQL is the default storage.

      . A using the command: show variables like 'innodb_data%' to view:

      mysql> show variables like 'innodb_data%';
      +-----------------------+------------------------+
      | Variable_name         | Value                  |
      +-----------------------+------------------------+
      | innodb_data_file_path | ibdata1:12M:autoextend |
      | innodb_data_home_dir  |                        |
      +-----------------------+------------------------+
      rows in set

      The default table space for ibdata1, 12M initial value;

      b. Table space may consist of multiple files, so the file size is not limited by the size limit, but the limit of its own

    2. Multi-use table (alone) storage space:

      • Table and table structure in .frm file (in fact there is nothing to engine), but the data with a separate index for each table stored in .ibd, if the partition table is then the corresponding separate "show + partition name" as the file name .ibd file;
      • Multi Table spatial parameters innbdb_file_per_table = ON after the entry into force of the new table only take effect;
      • Multi-table space data file no size limit, no need to set the initial size, do not need to set a maximum limit file extension parameters.
      • Use multi-table space characteristics of the table, you can easily backup and restore a single table, but a direct copy .ibd file is not feasible, because the data dictionary table space information is not shared, byte file and copy .ibd .frm file is not correct recognition.
      • Even in multi-table space in storage, shared table space is still necessary because the InnoDB internal data dictionary and log on this file.


MyISAM engine

Features:

  1. MyISAM does not support transactions do not support foreign keys, SELECT / INSERT-based applications can use this engine.
  2. Each MyISAM storage into three files, extensions are:
    1. .frm: stores the table (table structure information)
    2. .MYD (MYData), the stored data
    3. .MYI (MYIndex), storage index
  3. Specifies the path to the index files and data files, you need to specify the statement by DAT DIRECTORY and INDEX DIRECTORY When you create a table, that index and data files in different MyISAM tables can be placed under a different path.
  4. MyISAM type table provides repair tool, you can use CHECK TABLE statement to check MyISAM table health, and repair a corrupted MyISAM table with REPAIR TABLE statement.
  5. Only MyISAM supports FULLTEXT full-text indexing

Storage format:

Table MyISAM supports three different storage formats, namely: static tables (the default), dynamic table, compressed table.

  1. Static tables: Table fields are non-variable-length fields, each record is a fixed length, when the table does not contain variable-length columns (VARCHAR, BLOB, TEXT) use.
    • Advantages: storage quickly and easily caching, easy recovery fails
    • Cons: the footprint is generally more than the dynamic table, this is because when the data is stored static table will complement the space according to the column width, but the time of the visit is not visible (there is real data spaces will be removed)
  2. Dynamic: Table contains variable-length field, is not fixed-length record. When the table contains variable length (VARCHAR, BLOB, TEXTDynamic), or when a table is created by using ROW_FORMAT = DYNAMIC option.
    • Advantages: relatively small space
    • Cons: Frequent update / delete records tend to fragment, you need to perform OPTIMIZE TABLE tablename or myisamchk -r command to improve performance on a regular basis; failure recovery is relatively difficult.
  3. Compression table: is created by myisampack tool, taking up very little disk space, because each record will be compressed separately, so there is very little bearing expenses.


Supplementary summary

  1. Above is to introduce the MySQL storage engine in terms of usage scenarios, such as storage format, select make good use of the storage engine can achieve a multiplier effect in the real world;
  2. When we need time to make some operations need to consider the use of additional storage engine supports , for example, you establish a foreign key constraint on MyISAM tables are not being given, it will not prompt;
  3. Of course, InnoDB MyISAM with two common storage engine can be mixed , under high concurrency of the table, it is necessary to restore the transaction, etc. may be used in the InnoDB of SELECT / INSERT frequent use MyISAM table may be used.

Guess you like

Origin www.cnblogs.com/pankypan/p/11209675.html