# Histogram - Cont'd

+ The sample table `T2` has 19,500 of identical rows
+ By default, the histogram is comprised of 256 buckets.
+ The `Count` column reflects a cumulative total
+ `Ndv` (number of distinct values) is provided for the current bucket when `tidb_analyze_version = 2` is enabled.


```
tidb> SHOW ~!@STATS_BUCKETS@!~ WHERE db_name = 'test' AND table_name = 'T2';
+---------+------------+----------------+-------------+----------+-----------+-------+---------+-------------+-------------+------+
| Db_name | Table_name | Partition_name | Column_name | Is_index | Bucket_id | Count | Repeats | Lower_Bound | Upper_Bound |  Ndv |
+---------+------------+----------------+-------------+----------+-----------+-------+---------+-------------+-------------+------+
|  test   |     T2     |                |     id      |    0     |     0     |  77   |    1    |     501     |     577     |  0   |
|  test   |     T2     |                |     id      |    0     |     1     |  154  |    1    |     578     |     654     |  0   |
|  test   |     T2     |                |     id      |    0     |     2     |  231  |    1    |     655     |     731     |  0   |
|  test   |     T2     |                |     id      |    0     |     3     |  308  |    1    |     732     |     808     |  0   |
|  test   |     T2     |                |     id      |    0     |     4     |  385  |    1    |     809     |     885     |  0   |
|  test   |     T2     |                |     id      |    0     |     5     |  462  |    1    |     886     |     962     |  0   |
|  test   |     T2     |                |     id      |    0     |     6     |  539  |    1    |     963     |     1039    |  0   |
...
|  test   |     T2     |                |     id      |    0     |    248    | 19173 |    1    |    19597    |    19673    |  0   |
|  test   |     T2     |                |     id      |    0     |    249    | 19250 |    1    |    19674    |    19750    |  0   |
|  test   |     T2     |                |     id      |    0     |    250    | 19327 |    1    |    19751    |    19827    |  0   |
|  test   |     T2     |                |     id      |    0     |    251    | 19404 |    1    |    19828    |    19904    |  0   |
|  test   |     T2     |                |     id      |    0     |    252    | 19481 |    1    |    19905    |    19981    |  0   |
|  test   |     T2     |                |     id      |    0     |    253    | 19500 |    1    |    19982    |    20000    |  0   |
+---------+------------+----------------+-------------+----------+-----------+-------+---------+-------------+-------------+------+
254 rows in set (0.00 sec)
```
------------------------------------------------------------------------------
```
tidb> SHOW CREATE TABLE T2\G
*************************** 1. row ***************************
 Table: T2
Create Table: CREATE TABLE `T2` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `pname` varchar(32) DEFAULT NULL,
 `price` varchar(32) DEFAULT NULL,
 `info` varchar(32) DEFAULT NULL,
 PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
 KEY `idx_pname` (`pname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=30001
1 row in set (0.00 sec)

tidb> SELECT * FROM T2 LIMIT 10;
+----+-------+-------+------+
| id | pname | price | info |
+----+-------+-------+------+
| 1  |  000  | 9999  | none |
| 2  |  000  | 9999  | none |
| 3  |  000  | 9999  | none |
| 4  |  000  | 9999  | none |
| 5  |  000  | 9999  | none |
| 6  |  000  | 9999  | none |
| 7  |  000  | 9999  | none |
| 8  |  000  | 9999  | none |
| 9  |  000  | 9999  | none |
| 10 |  000  | 9999  | none |
+----+-------+-------+------+
10 rows in set (0.00 sec)

tidb> SHOW STATS_BUCKETS WHERE db_name = 'test' AND table_name = 'T2';
+---------+------------+----------------+-------------+----------+-----------+-------+---------+-------------+-------------+------+
| Db_name | Table_name | Partition_name | Column_name | Is_index | Bucket_id | Count | Repeats | Lower_Bound | Upper_Bound |  Ndv |
+---------+------------+----------------+-------------+----------+-----------+-------+---------+-------------+-------------+------+
|  test   |     T2     |                |     id      |    0     |     0     |  77   |    1    |     501     |     577     |  0   |
|  test   |     T2     |                |     id      |    0     |     1     |  154  |    1    |     578     |     654     |  0   |
|  test   |     T2     |                |     id      |    0     |     2     |  231  |    1    |     655     |     731     |  0   |
|  test   |     T2     |                |     id      |    0     |     3     |  308  |    1    |     732     |     808     |  0   |
|  test   |     T2     |                |     id      |    0     |     4     |  385  |    1    |     809     |     885     |  0   |
|  test   |     T2     |                |     id      |    0     |     5     |  462  |    1    |     886     |     962     |  0   |
|  test   |     T2     |                |     id      |    0     |     6     |  539  |    1    |     963     |     1039    |  0   |
...
|  test   |     T2     |                |     id      |    0     |    248    | 19173 |    1    |    19597    |    19673    |  0   |
|  test   |     T2     |                |     id      |    0     |    249    | 19250 |    1    |    19674    |    19750    |  0   |
|  test   |     T2     |                |     id      |    0     |    250    | 19327 |    1    |    19751    |    19827    |  0   |
|  test   |     T2     |                |     id      |    0     |    251    | 19404 |    1    |    19828    |    19904    |  0   |
|  test   |     T2     |                |     id      |    0     |    252    | 19481 |    1    |    19905    |    19981    |  0   |
|  test   |     T2     |                |     id      |    0     |    253    | 19500 |    1    |    19982    |    20000    |  0   |
+---------+------------+----------------+-------------+----------+-----------+-------+---------+-------------+-------------+------+
254 rows in set (0.00 sec)
```

The `id` column in table T2 serves as the primary key, with a secondary index `idx_pname` on the `pname` column. Currently, T2 contains 19,500 rows of data, where each row features a unique value in the `id` column, while values in other columns remain identical across all rows.

To examine the details of each bucket in the histogram, we execute the command `SHOW STATS_BUCKETS WHERE db_name = 'test' AND table_name = 'T2'`. By default, the histogram is comprised of 256 buckets. From the output, the first row’s `Column_name` indicates the column `id`. The `Bucket_id` denotes the bucket number, while the `Count` specifies the total number of values within the bucket (representing the histogram's depth). The `Lower_Bound` marks the smallest value, and the `Upper_Bound` the largest value in the bucket, delineating the histogram's range. The `repeats` field quantifies the occurrences of the maximum value within the bucket, and the `ndv` (number of distinct values) is provided for the current bucket when `tidb_analyze_version = 2` is enabled.

It's important to note that although one might expect the `Count` value to remain constant across an equal-depth histogram, the values are visibly increasing here. This is because the `Count` column reflects a cumulative total, where each entry corresponds to the aggregate of the current and all preceding rows. Histograms thus play a crucial role in index selection, particularly for determining indexes during index range scans.

For an in-depth explanation of the `SHOW STATS_BUCKETS` command, please visit: [Buckets of Histogram Documentation](https://docs.pingcap.com/tidb/stable/statistics#buckets-of-histogram).