当前位置:网站首页>Index invalidation caused by inaccurate MySQL statistics

Index invalidation caused by inaccurate MySQL statistics

2022-06-13 07:36:00 Fish is not fish

1. The impact of inaccurate statistical information

Inaccurate statistical information generally leads to MySQL The optimizer cannot select the correct index , Which leads to The optimizer selects the wrong index Or do not go to the index , cause Index failure , Leading to a large number of The slow query .

2. Under what circumstances will the statistical information be inaccurate ?

Generally speaking , It can be divided into the following two cases :

  1. The statistical information is not updated in time , Incorrect statistics invalidate the index .
  2. Too many fragments in the table , Lead to Cardinality inaccurate , Calculation error .

Let's talk about two scenarios in detail :

The statistical information is not updated in time

Before this topic, let's look at some parameters :
FOR MySQL 5.7:
In general, statistics are controlled by the following parameters :
innodb_stats_persistent: Controls whether statistics are persisted , The default .
innodb_stats_auto_recalc: Controls how many rows in the table are automatically updated after modification , The default is 10%, The default .
innodb_stats_persistent_sample_pages: The number of pages sampled during index statistics after statistics persistence is enabled , Default 20 A page , Too long sampling time will affect performance .
innodb_stats_method: Statistics encountered NULL How values are handled , Default NULL Equal value means nulls_equal.
In addition, you can see the details of statistical information in two tables :
mysql.innodb_index_stats and mysql.innodb_table_stats These two tables will store some details , The table structure is as follows :
 Insert picture description here  Insert picture description here
From the information of the two tables, you can see the last time of the statistical information .
Therefore, from the meaning of the parameters, we can see that there will be problems in the statistical information due to the untimely update of the statistical information .
For MySQL 8.0:
Out of the above parameters , Introduce a new parameter :
information_schema_stats_expiry: Time of statistics in cache , The default is 86400s 1 God .

Too many table fragments

First look at a picture :
 Insert picture description here
Cardinality: How many different values are counted in the table , The higher the value, the higher the index discrimination .
stay MySQL among , Index is data , But if there are too many fragments in the table , that Cardinality The statistics of values will be affected ,
For example, half of the table data is fragmented , So the marked positions are NULL That is, they all have the same value , Lead to
Cardinality Smaller value , Affect the optimizer to select the correct index .

3. How to solve the problem of inaccurate statistical information ?

Generally speaking, we can implement all three operations :

  1. Analyse table
  2. optimize table
  3. alter table T engine=innodb

The above three methods can regenerate statistics . The difference is that
The first is only for statistical information .
The second and third will not only rearrange the statistical information , And the table will be rebuilt , Recycle the fragments in the table .
 Insert picture description here

Refer to the website : https://www.modb.pro/db/77939

原网站

版权声明
本文为[Fish is not fish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270547441806.html