# Sampling and Accuracy

|                                     |            |             |
| :---------------------------------- | :--------- | :---------- |
| `tidb_analyze_version `             | 1          | 2           |
| Buckets number of Histogram         | 256        | 256         |
| `TopN` values number                | 20         | 500         |
| `SAMPLES / SAMPLERATE`              | 10000      | 1           |
| Array length of `Count-Min Sketch ` | `[2048,5]` | unsupported |

------------------------------------------------------------------------------
In the previous section, the result of the command `SHOW ANALYZE STATUS WHERE table_name LIKE 'T2';` includes a column named `Job_info`, which contains the details `analyze table all columns with 256 buckets, 500 topn, 1 samplerate`. This indicates the current sampling precision.

Higher precision in sampling during statistical information collection results in more accurate statistics, increasing the likelihood of the optimizer generating an optimal execution plan. However, elevating precision levels demands additional CPU and I/O resources during collection, potentially impacting system performance. Moreover, since statistics are typically stored in the TiDB server's cache during runtime, heightened precision consumes more cache space, which may influence caching efficiency during SQL statement execution. As a result, experienced engineers often adjust statistical precision to achieve a balance that is sufficiently effective.

In cases of automatic statistics collection, precision may vary based on the configuration of the system variable `tidb_analyze_version`. We have compiled a reference table for this in the slide.

To explain sampling rates or sample sizes: prior to TiDB Database version 5.3, `tidb_analyze_version = 1`, known as Statistics Version 1, was used. At that time, reservoir sampling with a default sample size of 10,000 was employed. However, reservoir sampling can generate redundant results and escalate memory and resource demands when dealing with large tables. Consequently, in Statistics Version 2, configured with `tidb_analyze_version = 2`, TiDB Database adopted Bernoulli sampling. This method uses a uniform sampling rate, where each element is sampled with the same probability. For instance, a sampling rate of 70% implies that 70% of the elements will be sampled, with each element having a 70% probability of selection.

Let's now explore how to conduct manual sampling and adjust sampling precision.