[Database] database optimized for high performance

High-performance database optimization

First, Performance Analysis

1, DQL query process:

  1. The client sends a query to the server;
  2. After the server by checking the permissions, first checks the query cache, if the cache hit, then immediately return the results stored in the cache. Otherwise, proceed to the next stage;
  3. Parsing SQL server side, pretreatment, and then calculated by the optimizer according to the statistical information relates to the SQL data tables, generating the corresponding execution plan;
  4. According to MySQL query optimizer generates the execution plan, the storage engine API calls to execute the query;
  5. The results are returned to the client.

Query Optimizer

1, write any sql, in the end is how the real execution, according to what criteria query, the order of the last execution, more than likely there will be implementation of the program.

2, the query optimizer basic statistics (such as indexes, the number of data) of the data table, before the actual implementation of a sql, based on their own internal data, comprehensive inquiry.

3. The mysql own statistics from a variety of implementation of the program among a select it considered optimal implementation of the program, come and perform.

What do do optimization

  • Do optimization, is to let the query optimizer in accordance with our ideas, help us choose the best implementation of the program
  • Let optimizer chooses to perform in line with the statement of programmers plan to reduce IO query generated in the process

mysql common problem

  • CPU saturation
  • Disk I / O read large data
  • Server hardware configuration low

2、Explain

Query execution plan:

Use explain keyword, it can simulate a SQL statement optimizer executed, so they know how to deal with MYSQL sql statement can analyze performance bottlenecks query or table structure by Explain.

effect:

  • Check the reading order of the table
  • Data read operation type of operation
  • See which index you can use
  • See which index is actually used
  • Check references between tables
  • Check how many rows per table is executed optimizer

Instructions

explain sql
##############################################################
explain select * from stuinfo join score using(sid)\G

id

  • No. select query
  • If the id of the same vertically, from top to bottom order of execution
  • If the id of different sizes, usually a sub-query, the greater the value of id priority will be executed
explain select * from stuinfo where age in (select max(age) from stuinfo);
  • id both have the same different
explain select * from stuinfo join score using(sid) where age in (select max(age) from stuinfo);

explain select * from stuinfo where sid in (select sid from score where sid in (select max(ch) from score));
  • Summary: go in the same order, the different large go first.

select_type

  • simple: simply select the query, the query does not contain subqueries or UNION
  • primary: If the query contains any complex sub-queries, the outermost query were labeled primary
  • subquery: contains sub-queries in the select or where in
  • union: if the second select appear after the union, were labeled union
explain select * from stuinfo union select * from stuinfo;
  • derived: If the union included in the from clause subquery, the outer layer will be marked as select deriver
explain select * from ((select * from stuinfo where sid =1 ) union (select * from stuinfo where sid=2)) as a;
  • union result: result obtaining union select from the table, the combined results of two sets UNION final

type

  • ALL: The full table scan, read data from the hard disk which, All cut the amount of data if there is a very large, have to do optimization
explain select * from stuinfo;
  • INDEX :
    • All index and index difference is the type of traversing the index tree only, usually faster than the All, because the index file is usually smaller than the data file
    • all are read and the entire table index, but the index is read from the index, all of which are read from the hard
explain select sid from stuinfo;
  • system: there is a row in the table (table system) const This is a special case of the type usually does not occur
  • const: represented by the index once found, const primary or unique index to compare directly query the primary key or unique index, because only one row of data matching, so fast.
explain select * from stuinfo where sid=1;
  • range: The selection of the index table statistics and case, a rough estimate of the number of lines required to find the desired record read
explain select * from stuinfo where sid>2;
  • Eq_ref one type of index, has been used

Summary: As long as ALL (full table scans the hard disk) does not appear, then the query speed of the current SQL statement must be relatively fast

possible_keys

May themselves have created four indexes, when executed, may automatically determine according to internal use only three

It may be used to determine the index

key

The actual use of the index

  • Index actually used, if NULL, then do not use the index
  • If the query uses the coverage index, the index appears only in the key list
  • possible_keys with which the index key relationship theory should be used for practical uses which indexes
  • Fields covered field index queries and build a just agreement, which we called a covering index

key_len

  • Indicates the number of bytes used in the index, the index can be used in the query length is calculated by the column.
  • Not necessarily very accurate

ref

Whether the index is introduced to, in the end which several references to the Index

explain select * from stuinfo a,score b where a.sid=b.sid and a.age=22;

row和filtered

row is scanned, the better

filtered is applied row index is the ratio of 100% is used on the whole, is the best

#优化的比较好
explain select * from stuinfo a,score b where a.sid=b.sid and a.age=22;
#不好
explain select * from stuinfo a,score b where a.sid=b.sid and a.age=22;

Extra

Generate values:

  • Using filesort: Description mysql will use an external data sorting index
explain select * from stuinfo a,score b order by age;
  • Using temporary: the use of a temporary table to hold intermediate results, Mysql when sorting query results, use a temporary table, common in sorting and grouping queries group by orderby
  • using index: query a key field
explain select sname from stuinfo ;
  • using where: where a query is determined (in the case is not satisfied)
explain select * from stuinfo where sname;

Second, the index

What is the index: Mysql help efficiently get the data structure of the data, similar to the Xinhua Dictionary Index catalog, can be found quickly through word you want to index directory, quickly find the data row good sequence.

1. Why indexing:

  • After improve query performance, one by one, before looking back not sorted, sort through the index can be defined directly to the desired location
  • Sorted quickly find data structure -> index is

2. Advantages and Disadvantages

Advantage:

  • By sorting the index data items, data sorting reduce costs, reduce the consumption of CPU
  • Similar bibliographic index indexing university library, improve data retrieval efficiency, reduce the cost of database IO

Disadvantages:

  • In general, the index itself is also great, the index often stored on disk as files
  • Although the index to improve query speed, but will reduce the speed of updating the table
  • Will be adjusted because the key changes brought about by the index update information

3. Why search index data will be even faster

When we store data, if indexed database system maintains a data structure to meet specific search algorithm, these data structures referenced data in some way, be on top of these data structures, implement advanced search algorithm, this structure is index

In general, the index itself is also great, can not all be stored in memory, so the index is often in the form of index files are stored on disk

To accelerate the lookup data, can maintain a binary search tree, each node includes an index key, respectively, and a pointer to the physical address corresponding to the recorded data, so that you can use a binary search to obtain the corresponding data for a certain complexity record to quickly retrieve the qualifying

In addition there BTtree binary tree index, said index I usually, if not specified, refer to the index B-tree structure of the tissue, wherein the focus index, secondary index, a composite index, prefix index, the default is the only B + tree index .

In addition to B + tree index, and a hash index (Hash index) and the like.

4. binary search tree

Use a binary search to quickly search

5.B-Tree (balance multiple search tree)

m-order B-Tree satisfies the following properties:

  • (1) each node has the largest subtree m
  • (2) the root subtree least 2
  • (3) Minimum branch node has m / 2 subtree
  • (4) all leaf nodes in the same layer, each node has at most m-1 th key, and arranged in ascending order

6.B+Tree

B + Tree with respect to the B-Tree have different points:

  • 1. The non-leaf node stores only key information.
  • 2 has a chain of pointers between all leaf nodes.
  • 3. Data records are stored in the leaf nodes
    Here Insert Picture Description

Why, when B + tree index as the default

Here Insert Picture Description

7.hash Index

Special hash index, the time complexity is O (1), but only for comparative embodiment equivalence query, the scope is not suitable for size comparison or query

Here Insert Picture Description

8. Create a principle index

  1. Suitable for frequent lookup column
  2. Suitable columns are often used to determine the conditions
  3. Suitable often due to the sort column
  4. Not suitable for small data columns
  5. Not suitable for small queries column

Third, data backup and recovery

  1. Backup

    mysqldump -h localhost -u root -p123456 dbname > dbname.sql
    
  2. restore

    mysql -h localhost -u root -p123456 dbname < ./dbname.sql
    

Fourth, the index face questions

According to left-prefix rule to match

  • Combined index may be used by a single key
  • Three key indexes to form a coalition, if any two of use, do not use the index to the far left or the far left index ranking position is incorrect fail
  • Find the range of the index will lead to failure
  • General index use or operation may fail

Here Insert Picture Description

Published 116 original articles · won praise 10 · views 1366

Guess you like

Origin blog.csdn.net/weixin_44727383/article/details/104954482