当前位置:网站首页>On the implementation plan of MySQL explain

On the implementation plan of MySQL explain

2022-07-04 14:53:00 Tonghua misses the rain

Preface

We often use Explain Check the execution plan , As we all know, this . But ask the interviewer during the interview , Do you use Explain Mainly to see what ? The answer of the other party is mostly to check whether the index is used , This is obviously not the best answer

Explain Each field and its meaning

  • id : Express SQL Identification of the order of execution , SQL From big to small execution
  • select_type: Represents each... In the query select The type of clause
  • table: Show which table this row's data is about , Sometimes it's not a real name
  • type: Express mysql How to find the desired row in the table , Also known as access type . Common types are :ALL, index, range, ref,eq_ref, const, system, NULL( From left to right , Performance from poor to good )
  • possible_keys: Pointed out that mysql Which index can be used to find records in the table , If there is an index on the field involved in the query , Then the index will be listed , But not necessarily used by queries
  • Keykey Columns show mysql Actually decide which key to use ( Indexes ), If index is not selected , The key is NULL
  • key_len: Represents the number of bytes used in the index , You can use this column to calculate the length of the index used in the query (key_len The displayed value is the maximum possible length of the index field , It's not the actual length , namely key_len It is calculated according to the table definition , It is not retrieved from the table )
  • ref: Indicates the join matching condition of the above table , That is, which columns or constants are used to find values on index columns
  • rows: Express mysql According to table statistics and index selection , Estimated number of rows to read to find the required record , In theory, the less , Better query performance
  • Extra: This column contains mysql Solve query details

stay Explain In the execution plan , We focus on Type and Extra Two fields , They are the core

Explain Medium Type

Explain Medium Type stay MySql The official website of is interpreted as : Connection type (the join type). It describes the scanning method used to find the required data . The most common scanning methods are

  • system: Indicates that the data has been loaded into memory , No disk required IO, This kind of scanning is the fastest
  • const: Hit the primary key index or unique index , The connected part is a constant value
  • eq_ref: Equivalent scanning of primary key index or non empty unique index
  • ref: Non primary key index or non unique index equivalent scanning
  • range: Range scan , It's a range query on the index , It scans the index for values in a specific range
  • index: Index tree scan , You need to scan all the data on the index tree
  • ALL: Full table scan

The above scanning methods range from fast to slow :system > const > eq_ref > ref > range > index > ALL

Explain Medium Extra

Explain Medium Extra The value is

  • Using index: explain SQL All column data to be returned is in an index tree , Without having to access the actual row record
  • Using where: explain SQL Used where Condition filter data
explain select * from account_user_base where id > 4;
  • Using index condition: Indicates that the index was indeed hit , But not all of the column data is on the index tree , You also need to access the actual row record
explain select * from account_user_security t1, account_user_base t2 where t1.user_id = t2.id;
  • Using filesort: Explain to get the desired result set , All records need to be sorted . Typical , On a column that is not indexed order by, It will trigger filesort, The common optimizations are , stay order by Add index... To the column of , Avoid full ordering every time you query . For details, please refer to :mysql in order by Principle
  • Using temporary: It indicates that a temporary table needs to be created to temporarily store intermediate results . This kind of SQL Statement performance is low , Often also need to optimize . Typical group by and order by At the same time , And when it works on different fields , It's going to create a temporary table , In order to calculate the final result set
原网站

版权声明
本文为[Tonghua misses the rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141238394305.html