MySQL urgent need to enhance the capacity of design optimization

       I used to not pay enough attention to MySQL, just stay at the application stage, superficial understanding of the principles and optimization, this ism is particularly evident in terms of the index and the database engine, is limiting the vision my thoughts. Luckily get two predecessors should point out, I feel the distance to be a high level of R & D personnel and further. After a series of these days access to information, knowledge of this blind spot is being lit, but this is only stay in the theoretical knowledge, but also a broader vision to see the problem, but also need more proven ideas in the experiment, in the final practice complete absorption of knowledge.

 

Reference books "MySql Inside InnoDb Storage Engine" chapter index and algorithm


The difference between the database engine.
A:
MyISAM: high-speed query and insert. Good insertion and query does not support transactions.
Innodb: data integrity, concurrency handling, good update, delete, and support services.
MyISAM emphasis on performance, while Innodb emphasis on safety.


Why Innodb for good update, and delete and insert MyISAM good and queries?
A:
The former Innodb good at dealing with concurrency.
Because it uses a row-level locks, only lock the row, other rows not locked,
the data stored in the table and index space inside.
The latter MyISAM specializes in high-speed read and write.
Because it itself is a file system, it is an abbreviation indexed sequential access method,
the index file and data file are separate, high maintenance efficiency index.


Advantages and disadvantages of various independent data and index table space and shared table space.
A:
Independent advantages:
high maintenance efficiency index,
fragmentation is not obvious, retractable unused space.
Share drawback:
indexing and after a large number of deleted records, shared table space can not be retracted.
Shared advantages:
easy management, such as: sub-table to facilitate operation.
Independent drawback:
the library table in the number of long, difficult demolition demolition library table.


Index What?
A:
By function are:
primary key index, the only index, the general index, the joint index
by search engines Category:
hash index, btree index (bifurcated algorithm)


Why index will be faster?
A:
When no index, to find records by primary key will be scanned from beginning to end,
    needs to N scanning lines, time complexity of O (n).
When there is an index, 3 types, hash, btree, the full text.
When the hash lookup, positioned directly to the primary key of the record, the time complexity of O (1).
When the binary search, because the height of B + trees are generally in the 2-4 layer,
a maximum of only 2-4 times IO, efficiency is also more available.
Full-text search with the inverted index, if the key is available, the time complexity of O (1).
If the keyword is not included, according to the most left-prefix match to find.

 

If a large number of duplicate records, indexing, what's the problem?
A: The construction of the index must first consider discrimination, there will be performance issues in this case,
a negative example: the establishment of a regular index to gender field.
It will result in the general index and the clustered index switch back and forth, big overhead.
Get the address from the index only, if we really want access to the data or to the table once IO.
If the data is taken from line 10 in 5 rows of data, the equivalent of five times the index access, then access the five times table,
add up the cost of not less than to direct a full table scan.

 

Why varchar is not suitable for indexing?
A: The
difference between the performance of an index which is way,
rather than the data type of the index.
If using as a hash index, efficiency does not decrease.
The use of a btree index, index maintenance is likely to consume a lot of performance.
This non-monotonic field when inserting a new record,
data file in order to maintain the characteristics of the B + Tree and frequent split adjusted,
in some cases very inefficient, but also improved the way
you can use short index, calculated maximum identification of m = m / q,
for example, approximately the first 5 bits only,
then do not index the entire column.


Save a lot of IP in the database, may be repeated, how to remove the most efficient?
A:
1. IP to establish a common index, most people can think of.
2. Method IP to dotted decimal numeric type, the type of storage used int UNSIGNED more efficient.
3. If needed fuzzy queries, enable adaptive hash index. (For Innodb)
4. If the discrimination result in the same IP prefix is not high,
    but they need to be removed by the interval may be considered a multi-column index.
5. Consider whether the index can cover the query.
6. Expression of fields involved in the field to ensure independent operator side.
7. If desired fuzzy query, not left to the wildcard at the beginning.

 

On inquiry, what skills or precautions efficient?
A:
When build composite index, the highest distinction in the far left.
Multi-criteria query to determine the condition of the equal sign to put the left.
Use the index to cover the query, to avoid the back to the table.
Index file has the most left-prefix matching characteristics, avoid the use of vague or left whole fuzzy.

 

How to efficiently query a record period of time?
A:
1. Indexing can speed up queries
2. Time type datetime,
    timestamp consider the use of numeric types int (11)
3. Use BETWEEN-AND, after indexing, index scan can be used to improve the efficiency of queries.
4. Do not use the database function, it can lead to not use the index.


Without taking the index might have?
A:
1. Function Operation
2. implicit conversion
3. Fuzzy queries
4. range query
5. calculating operation

 


Full-text index is what principle?
A:
Over the past only MyISAM supports full-text indexing, Innodb now also supports full-text indexing.
But it does not support the language of the word delimiters, such as CJK.
You need to install the sphinx and other search engine supports full-text indexing.
The principle is to use an inverted index,
InnoDB will split the word storage, while looking, according to the word match.
Here quite taste the search engine,
but only precise query,
Lucene and other search engines are better at word and rounding transactions,
access control, and a small part of the record (document), may miss the
exchange to quickly search response.

 

What database optimization skills?
A: The
more frequent as the field query should create an index
field uniqueness of the poor are not suitable for creating a separate index, even as a frequent query
heavily-updated fields are not suitable for creating an index
does not appear in the WHERE clause field is not the index is created

 

Published 103 original articles · won praise 175 · Views 250,000 +

Guess you like

Origin blog.csdn.net/ai_64/article/details/104528329