MySQL storage engine, the index and basic optimization strategy

Storage Engine

With Oracle, SQL Server these different databases, MySQL offers a variety of storage engines. What is the storage engine? Storage engine is actually a set of data for how to store, query, update, establishment of implementation of the interface indexes. Different storage engines have different characteristics, we choose according to need, such as ETL operations include OLTP (online transaction processing) project we often choose InnoDB, whereas for reading almost no more write operations OLAP (online analytical processing) is the election of more MyISAM. So not everyone with a similar environment, the same version of MySQL, can use the feature is consistent. View supported storage engine in MySQL terminal, defaults and a brief introduction:

  1. SHOW ENGINES;

In the version I use, there are 10 kinds of storage engine is used by default InnoDB.

Available storage engine

When you create a table using a specified storage engine:

  1. CREATE TABLE IF NOT EXIST mytest (foo VARCHAR(32)) ENGINE=InnoDB;

Check engine has been created to store tables used:

  1. SHOW CREATE TABLE mytest;

Although MySQL offers a variety of data storage engine, but we have the most contact or MyISAM and InnoDB, both storage engines have been through a lot of practice, very reliable.

MyISAM

MyISAM is an earlier version (MySQL before 5.5.5) the default storage engine, and there is not support transactions, foreign keys, and row-level locking. Table-level locking, locking granularity is relatively large, overhead is relatively small, but because of the increased potential for conflict when doing data updates, more suitable for the query-based business. It is worth mentioning a detail is, the data table rows MyISAM directly stored, so the search count will be free conditions results in constant time. MyISAM supports B-tree / FullText / R-tree index type.

InnoDB

The new version has the InnoDB as the default storage. Compared to MyISAM, InnoDB has an excellent transaction support, but also support foreign keys, and row-level locking. These features make the InnoDB data is updated in the face of intensive scene is still very powerful solution. InnoDB indexes cached data as well as the cache itself, which will lead to greater take up more storage space to discuss index related content in more detail below. InnoDB also supports our common auto_increment attribute. InnoDB supports Hash / B-tree index type.

Other storage engines

As described above, MySQL also offers several other storage engines, as used in temporary tables MEMORY storage location in memory, used to make the cache, and the data compression ARCHIVE archive storage, but my personal exposure to these storage engines not much, you can see other information online.

index

The index is an additional data structure in order to accelerate query operations on the data sheet and maintenance. We usually according to certain rules (such as for a condition where often appear in a column) index on the table, so for this type of query after will be very efficient. In the primary key and foreign key MySQL table will be created automatically add an index (if supported) on. But precisely because the index is an additional safeguard data structures, so it will not only take up more storage space, will bring an additional burden for the insert and update data. Prudent and reasonable to add an index for the table, it is an important means to improve the performance of MySQL. More details on the index, data structures and algorithms behind the principle of MySQL index writes very well.

B-Tree and B + Tree index

A recursive lookup B-Tree is a balanced tree multiple query process by comparing values ​​within the value of the node to be queried, the decision to return match is found, or a branch of a mismatch by the lower layer, or can not continue recursive lookup return to lookup failed. Search algorithm in such a tree to reduce the time complexity of the number of levels, very efficient. But in order to maintain the orderly nature of the Fengyun B-Tree with balance, insert and update data in the event will bring additional costs, add or delete specific algorithm change search tree on balance, interested students can read the query here not discussed.

B-Tree have many variants, B + Tree is one of them. B + Tree of B-Tree with one significant difference is that all of the B + Tree of data stored in the leaf node, so each query will reach the bottom of the tree. Modern B + Tree Database often do some additional optimizations, such as increasing the pointer between the bottom node, leaf nodes to form a structure such that a similar list (or one long jump table rather special) to speed traverse and range queries. The main practice and other classic red-black tree data structure has not been used as a database, because the disk IO performance considerations and other reasons related to the more abstract, which has been mentioned at the end of this link page.

MySQL in the index

MySQL supports multiple storage engines have different support for the index.

In MyISAM storage engine, a B + Tree is used by default as an index. In MyISAM, the index data is separated, B + Tree leaf node stores a pointer pointing to real data, the pointer to the query process after the indexing, the result depending on the value returned pointer. MyISAM indexes are the way to become a non-clustered index.

InnoDB storage engine also used as a B + Tree indexing, but the specific implementation is not the same. In InnoDB, the data itself is stored in the primary key B according to the above organization + Tree, so InnoDB table can no primary key. Another feature to be mentioned that, for other secondary indexes on tables InnoDB storage engine based on the same B + Tree, but the final value corresponding to the primary key data, in other words, a query process will be divided into two stage, after an index, if the search is successful, it will hold the primary key value corresponding to the data stored again based on real data and look for a B on the primary key organizations + Tree. InnoDB's index approach is called clustered index.

practice

The basic performance analysis tools

What there is to see an index on a table:

  1. SHOW INDEX FROM mytest;

If you want to learn to use an index for a query, you can use the command before adding EXPLAIN. Displays the most recent time the query EXPLAIN consumed:

  1. SHOW PROFILES;

The basic optimization strategy

Understand the basic principles behind the index, as well as fundamental analysis tools, provides a guideline for the efficient use of our index. How to optimize DB is a more delicate matter, and the specific circumstances. Many index SQL statement will not do anything improper well established, such as multiple (> 1) the scope of the column, the column is missing some combination index (the worst is the lack of the first column in the leftmost match), containing the function or expression, selective (non-repeating number of filter rules by index ratio of the number of all recording) is too low.
In addition to the secondary index for the high frequency added query, select the key also has certain knowledge. InnoDB storage engine index combined with the underlying store details manner, simply, using a logical independent self-energizing field as the primary key is a good idea.

Guess you like

Origin www.cnblogs.com/fengyun2050/p/12114947.html