Each time Navicat refreshes, the number of rows in a table changes. The table is Mysql InnoDB,. I don't know why.

every time Navicat is refreshed, the number of rows in a table changes. The table is Mysql InnoDB,. I don"t know why?

Jul.29,2021

the number of rows in the Innodb table is a cardinality value counted from its primary key, which can be show index table name; view, the number of rows is the same as the Cardinality of the primary key (why it's not the same sometimes, because you have to re-count both refresh and show index).

first take a look at InnoDB's B+ tree index, which looks like this (take the primary key as an example)

clipboard.png

the leaf nodes circled in red represent leaf nodes (a total of 5 leaf nodes). Each leaf node stores records of the primary key, the first leaf node has 4 records, the second has 3 records, and the third has 2 records.
(the above picture shows that the key values for storage are 5, 10, 15, 15, 25, 28, 50, 55, 60, 60, 70, 70, 75, 80, 85, 90)

the key point is that the statistical process of Cardinality is to sample 8 leaf nodes randomly:
1, get the total number of leaf nodes, mark it as A (legend is 5)
2, randomly take 8 leaf nodes, and count the number of different records on each page, that is, P1PowerP2.P8
3, Cardinality= (P1+P2+.P8) * AB8
because there are only 5 leaf nodes in the legend, these pages are always taken for random sampling. So the records are all the same, and if there are enough leaf nodes and the number of records on each page is different, the result of each sampling may be different.

attachment: the number of random sampling pages can be viewed by setting innodb_stats_sample_pages, to view show variables like 'innodb_stats_sample_pages';

the above is a simple description. For details, you can take a look at Chapter 5 Index and algorithm of MySQL Technology Insider: InnoDB Storage engine, which introduces the index data structure of Innodb and computing Cardinality.


this has nothing to do with Navicat, but the number returned by mysql is changing, and the number of rows returned by the innodb engine is an estimate. It is a time-consuming operation to calculate count accurately.


TABLE_ROWS

The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.

MySQL document, it is an approximate value under InnoDB, and the error can be up to 50%

.
Menu