Performance analysis of MySQL index

itread01 2021-01-21 23:27:24
performance analysis mysql index


# Why do you do performance analysis? Do you have such a situation . In front of a person you haven't written much about 、 Complex business , You've been thinking about it for a long time , Finally, I started to knock down the first code . The process of writing is vague , Sometimes I can make myself dizzy . But you finally finished it . But click to execute , Finished , Yes bug. What should I do? ?debug There are many ways , Console printing is a kind of . Information printed through the console , We can modify code based on feedback , Until the code runs properly . It's the same with building an index . Last post [《 Talking about sql Indexes 》](https://www.cnblogs.com/tandk-blog/p/14283455.html), It is said that the difficulty of index is to make the most suitable index for a specific table . Because it doesn't just depend on what's in your watch , It depends more on your business , What keywords do you often search for in your business . We can write code every day , Index can't be built every day . So most of the time , Because I'm not familiar with , The indexes we build in the beginning are often not the best , Only adjust the index according to the feedback , In order to make a most suitable index for this table . What I want to share today is how to understand this feedback , That is, how to do efficiency analysis .# How to do efficiency analysis EXPLAIN Keywords ! Use EXPLAIN Keywords can tell MySQL How to deal with you SQL Sentence of , Analyze the performance bottleneck of your query statement or table structure . but EXPLAIN It doesn't give us direct feedback on the quality of the index we built . The usage is like this , We built the index , Take the most common sentences in business SQL Statement EXPLAIN once , If the feedback works well , Then the index is the most suitable for this table , On the contrary, it needs to be improved . Or change the index , If you don't change SQL.# EXPLAIN How to play ### Sentence `explain To examine sql Sentence `( Horizontal table ) or `explain To examine sql Sentence \G`( Vertical table )### A concrete example ![](https://img2020.cnblogs.com/blog/1573343/202101/1573343-20210121162454574-528124047.png)### The explanation of each field can be seen from the figure above ,explain The information that comes out has the following fields :|id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra|| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | Take a closer look , Is it a bit like a payslip ? The meaning of each field is like this :- id: View the reading order of the table . The example above only queries one table , But if it's a multi table joint query , Then there are : id In the same way , The order of execution is from top to bottom ; id Different words ,id The higher the value, the higher the priority ;- select_type: Show differential union queries 、 Subquery 、 General query, etc . Here are the possible values , And the corresponding information : SIMPLE -- Simple select Inquire about , Does not contain subqueries or union; PRIMARY -- If the query contains any complex sub parts , The outermost query is marked as PRIMARY; SUBQUERY -- stay select or where Include subqueries ; DERIVED -- stay from The list contains subqueries marked as DERIVED( derivative ),MySQL These subqueries are executed recursively , Put the results in a provisional form ; UNION -- If the second one select Appears in union After , Is marked as union; if union Included in from Clause in the subquery , The outer layer select Will be marked as :DERIVED; UNION RESULT -- From union Table to get the result select;- table: Display the table name .- type: Shows which type of query is used . Here are the possible values , And the corresponding information : system -- There is only one line in the table ( System table ), This is const A special case of type , It doesn't usually appear , You can ignore it . const -- It means that through the index, you can find ,const Used to compare primary key( Primary key ) perhaps unique( only ) Indexes . Because it only matches one line of data , So soon , If you set the primary key at where In the list ,mysql You can convert the query to a constant . eq_ref -- Unique scan index , For each index key , In the table ** It just happened to be ** There's a record that matches . Used for primary key or unique index scanning . ref -- Non unique scan index , Returns all rows that match a single value . above `eq_ref` The multivalued case of . Such as where age=1,age=1 There's just one line of information about , It shows eq_ref,age=1 How many lines of information are there , It shows ref. range -- Retrieve only rows in a given range , To select a line using a quotation . If type The value of the column is `range`,key Column will show which index is used . It's usually in where In the sentence between、<、>、in And so on . This kind of range scan query is better than full table scan . index -- Full Index Scan( Scan all indexes ),index And ALL The difference is index Type only traverses the index tree . It's all about reading the whole meter , however index It's reading from the index ,all It's read from the hard disk , And index files are usually smaller than data files . all -- Full Table Scan( Scan the whole table ), Traverse the entire table to find a matching row , The index is totally useless . From the best to the worst is :system>const>eq_ref>ref>range>index>ALL Details : 1. type yes ALL, When the data reaches more than one million, it must be optimized . 2. In general , If you want to optimize the query to at least range Level , It's better to achieve ref.- possible_keys and key possible_key: Show the indexes that may be applied to this table , One or more . Indexes that could theoretically be used , But it doesn't have to be actually used by queries . key: Index actually used , If it is null, No index is used . There are four situations in these two columns : 1. possible_key Valuable ,key Valuable : normal , Sometimes the former has multiple values, but only one of the latter is normal . 2. possible_key Valuable ,key It's not worth it :** Index failed , There's a problem **. 3. possible_key It's not worth it ,key Valuable : Condition query ( Such as where) No index or no condition query is used , But the columns of the query (select The fields at the back ) Just in sequence 、 The quantity is consistent with the index . 4. possible_key It's not worth it ,key It's not worth it : normal , You didn't build the index .- key_len: Represents the number of bits used in the index . This column can be used to calculate 【 The length of the index used in the query 】, With the same query results , The smaller the value, the better . key_len The value displayed is the maximum possible length of the index field , Instead of the actual length , It is calculated by table definition , It's not a search in a table . Suppose you build a composite index (col1,col2), If 【 Through col1 Condition query 】 and 【 Through col1 and col2 Condition query 】 The result is the same , Then the former is better , Because you only need to use one field ,key_len The value of will be smaller , As mentioned above, it is determined by the length of the table definition key_len Value .- ref: Display key The value of the index reference in the column . There are two possible values , And the corresponding information : Library name . Table name . Field name -- Indicates which column of which table in which library the index refers to ; const -- The value representing the index reference is a constant , It's usually where id=1 That's how it comes out ;- rows: According to table statistics and index selection , Roughly estimate the number of rows to read to find the record to query .- Extra: Very important additional information . Here are the possible values , And the corresponding information : Using filesort -- Explain mysql Completely or partially not sorted according to the index you built , It needs to be optimized .MySQL A sort operation that cannot be done with an index is called “ File sorting ”; Using temporary-- Temporary tables are used to store intermediate results ,mysql Use temporary tables when sorting query results . It also needs to be optimized , Because the creation and deletion of temporary tables are cost-effective , Common in order by and group by; Using index -- It means corresponding select Overlay index used in operation , Avoid accessing the columns of the table , It's efficient . If it appears at the same time Using where, Indicates that the index is used to perform a query of the key value of the index , If not , Indicates that the index is used to read data rather than perform query actions ; Using where -- Used where Filtration ; Using join buffer -- Using wire caching , If this field always appears , You can increase this value appropriately in the configuration file ; Impossible where -- where The value of clause is always false, Cannot be used to get any tuples ; Select tables optimized away -- In the absence of group by In the case of clause , Based on index optimization MIN/MAX To operate or respond to MyISAM Storage engine optimization count(*) operation , You don't have to wait for the execution phase to calculate , The query execution plan generation stage completes the optimization ; distinct -- Optimization distinct operation , Stop looking for the same value immediately after finding the first set of matching tuples . Be careful : 1. Use group by When sorting , If it's useful to index , It's best to follow the order of the index , such as , There is a composite index (col1,col2), When sorting, if you skip col1, Use it directly col2 Sort , It can lead to Using filesort、Using temporary And other serious problems . 2. Try to use an overlay index ,select The following column names are exactly in the order of the index created 、 The number is the same . In this way, the index can be used to read data directly , Avoid reading table columns .# Finally, a simple case , I'll put the topic first , Let's think again , Finally, put the answer .### The title is to write SQL The order of execution .![](https://img2020.cnblogs.com/blog/1573343/202101/1573343-20210121172815826-249366881.png)### Ideas 1. First of all to see id Column ,id The bigger, the higher the priority , Index from id For 4 I'm going to start looking at , In this line of work table For t2, The query is t2, So the first query is the last part `select name,id from t2`.2. id For 3 My line of work ,table yes t1, So what we're looking for is `select id,name from t1 where other_column=''`; At the end of the line Extra Column `Using where` It also proves this point .3. id For 2 My line of work ,table yes t3, So what we're looking for is `select id from t3`; in addition ,key The column is primary The primary key is used as index ,Extra Column `Using Index` Indicates that the overlay index is used ( That is to say, the reference is in select Behind ).4. id For 1 My line of work ,
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121211618390j.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云