当前位置:网站首页>Several characteristics of MySQL database

Several characteristics of MySQL database

2022-06-23 07:15:00 qq_ thirty-seven million two hundred and seventy-nine thousand

Reading guide :0. Storage engine MyISAM and InnoDB difference 1. Thing level 2. Clustered and non-clustered indexes 3. Back to the table 4.explain Implementation plan

0. Storage engine MyISAM and InnoDB difference

MyISAM

  • Unsupported transaction , So every query is atomic ;
  • Support table level lock , That is to say, every operation is to lock the whole table ;
  • Store the total number of tables ;
  • One MYISAM The table has three files : Index file 、 Table structure file 、 Data files ;
  • Using nonclustered indexes (Non-clustered), The data field of the index file stores the pointer to the data file .

InnoDb

  • Support ACID The business of ;
  • Supports row level locks and foreign key constraints ;
  • Do not store the total number ;
  • One InnoDb The engine is stored in a file space ( Shared tablespace , Table size is not controlled by the operating system , A table may be distributed in multiple files ), It's also possible for multiple , Limited by operating system file size ;
  • The primary key index adopts the clustered index ( The indexed data field stores the data file itself ), The data field of the secondary index stores the value of the primary key ; So look up the data from the secondary index , You need to find the primary key value through the secondary index first , Visit the secondary index ;
  • Use auto increment primary key , Prevent data insertion , To maintain B+ Tree structure , A big adjustment of the document .

1. Thing level

Query isolation level :SHOW VARIABLES LIKE 'transaction_isolation%';

Read uncommitted Read uncommitted

Read uncommitted means that a transaction can be read without being committed , Obviously, this isolation level will lead to reading other uncommitted data , Once further processing is done based on the data read , Another transaction eventually rolls back the operation , Then the data will be messed up , And it's hard to track . On the whole , Reading uncommitted levels results in dirty reads .

Read the submission Read committed

As the name suggests, a transaction cannot be read until it is committed , Suppose you take a bank card to spend , Before you pay, you see that Cary has 2000 element , Your wife is shopping on Taobao at this time , I finished the payment ahead of you , At this time, when you pay again, you will be prompted that the balance is insufficient , But you can see that the money in the card is enough .

This is when two transactions are executed , Business A I read it at the beginning. The card has 2000 element , This is the time for business B Spend all the money on the card , Business A When we finally reconfirmed the balance, we found that the card had no money . Obviously , Read submit can solve the dirty read problem , But it can't be repeated .

Sql Server,Oracle The default isolation level for is Read committed.

Repeatable Repeatable read

Look at the name and you'll see , It is to solve the problem of non repeatable reading , Business A Once the execution starts , Regardless of business B How to change the data , Business A What you always read is the value it just started reading . So here's the problem , What if B hold id by 1 The data in the table is changed to 2, Business A Don't know id There is a change , When a transaction A When we added new data, we found that 2 Of id It already exists , This is unreal reading .

MySQL The default isolation level of is Repeatable read.

Serialization serializable

This is the most invincible existence , All the transactions are executed one by one , Because there is no concurrent scenario , What kind of unreal reading 、 Dirty reading 、 It's not repeatable. It doesn't exist . But again , Basic concurrency will be very poor . Final , In the end, the isolation level should be selected according to your own business scenario , No best , Only the most suitable .

2. Clustered and non-clustered indexes

Clustered index

  • A table can only have one
  • Stored records are physically continuous
  • The leaf node is the data node , Stored data itself

Nonclustered indexes

  • There are multiple tables in a table : General index , unique index , Full-text index
  • Stored records are logically continuous , Physical storage is not continuous
  • Leaf nodes are clustered indexes of data key, A pointer to the corresponding data block

3. Back to the table

The process of searching through a nonclustered index is to find the index first key Of the corresponding clustered index key, And then take the clustered index key Find the corresponding data on the primary key index tree

4.explain Implementation plan

4.1 select_type Column : Express SELECT The type of

Common values

  • SIMPLE( A simple watch , That is, no table connection is used Or subquery ).
  • PRIMARY( Main query , That is, the outer query )、UNION(UNION The second or The query statement after the user )
  • SUBQUERY( First in subquery SELECT)

4.2 type Column : Indicates the connection type of the table

The connection types with good to poor performance are :

  • system( There is only one row in the table , Constant table ).
  • const( At most one matching row in a single table , for example primary key perhaps unique index).
  • eq_ref( For each of the preceding lines , Only one record is queried in this table , Simply speaking , It is used in multi table connection primary key perhaps unique index).
  • ref( And eq_ref similar , The difference is not to use primary key perhaps unique index, Instead, use a normal index ).
  • ref_or_null( And ref similar , The difference is that the condition contains a pair of NULL Query for ).
  • index_merge( Index merge optimization ).
  • unique_subquery(in Is followed by a subquery that queries the primary key field ).
  • index_subquery( And unique_subquery similar , The difference lies in in Is followed by a subquery that queries non unique index fields ).
  • range( Range query in a single table ).
  • index( For each of the preceding lines , All get data by querying the index ).
  • all( For each of the preceding lines , They all get data through full table scanning ).

4.3 Extra: Description and description of the implementation

  • Using index( This value represents mysql Will use overlay index , To avoid accessing tables ).
  • Using where(mysql The storage engine will retrieve the rows before filtering , many where The condition refers to the columns in the index , When ( And if the ) When it reads the index , Can be tested by the storage engine , So not all belts where The query in clause will show “Using where”. Sometimes “Using where” The appearance of is a hint : Queries can benefit from different indexes ).
  • Using temporary(mysql The temporary table is used to sort the query results ).
  • Using filesort(mysql An external index will be used to sort the results , Instead of reading rows from the table in index order .mysql There are two file sorting algorithms , Both sorts can be done in memory or on disk ,explain I won't tell you mysql What sort of file will be used , It will not tell you whether the sorting will be done in memory or on disk ).
  • Range checked for each record(index map: N) ( There's no good index , The new index will be reevaluated on each row of the join ,N Is shown in possible_keys Bitmap of index in column , And it's redundant ).
原网站

版权声明
本文为[qq_ thirty-seven million two hundred and seventy-nine thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230623344787.html