In my FOSDEM 2018 presentation, I showed how you can inspect the content of a histogram using the information schema table column_statistics. For example, the following query will show the content of the histogram for the column l_linenumber in the table lineitem of the dbt3_sf1 database:

    1. SELECT JSON_PRETTY(histogram)
    2. FROM information_schema.column_statistics
    3. WHERE schema_name = 'dbt3_sf1'
    4. AND table_name ='lineitem'
    5. AND column_name = 'l_linenumber';

    The histogram is stored as a JSON document:

    1. {
    2. "buckets": [[1, 0.24994938524948698], [2, 0.46421066400720523],
    3. [3, 0.6427401784471978], [4, 0.7855470933802572],
    4. [5, 0.8927398868395817], [6, 0.96423707532558], [7, 1] ],
    5. "data-type": "int",
    6. "null-values": 0.0,
    7. "collation-id": 8,
    8. "last-updated": "2018-02-03 21:05:21.690872",
    9. "sampling-rate": 0.20829115437457252,
    10. "histogram-type": "singleton",
    11. "number-of-buckets-specified": 1024
    12. }

    The distribution of values can be found in the buckets array of the JSON document. In the above case, the histogram type is singleton. That means that each bucket contains the frequency of a single value. For the other type of histogram, equi-height, each bucket will contain the minimum and maximum value for the range covered by the bucket. The frequency value recorded, is the cumulative frequency. That is, it gives the frequency of values smaller than the maximum value of the bucket. In the example above, 64.27% of the values in the l_linenumber column is less than or equal to 3.

    In other words, if you have created a histogram for a column, you can query the information schema table to get estimates on column values. This will normally be much quicker than to get an exact result by querying the actual table.

    As discussed in my FOSDEM presentation, string values are base64 encoded in the histogram. At the time of the presentation, using MySQL 8.0.11, it was a bit complicated to decode these string values. However, from MySQl 8.0.12 on, this has become simpler. As stated in the release notes for MySQL 8.0.12:

    The JSON_TABLE() function now automatically decodes base-64 values and prints them using the character set given by the column specification.

    JSON_TABLE is a table function that will convert a JSON array to a relational table with one row per element of the array. We can use JSON_TABLE to extract the buckets of the histogram into a relational table:

    1. SELECT v value, c cumulfreq
    2. FROM information_schema.column_statistics,
    3. JSON_TABLE(histogram->'$.buckets', '$[*]'
    4. COLUMNS(v VARCHAR(60) PATH '$[0]',
    5. c double PATH '$[1]')) hist
    6. WHERE schema_name = 'dbt3_sf1'
    7. AND table_name ='orders'
    8. AND column_name = 'o_orderstatus';

    Running the above query on my DBT3 database, I get the following result:

    1. +-------+---------------------+
    2. | value | cumulfreq |
    3. +-------+---------------------+
    4. | F | 0.48544670343055835 |
    5. | O | 0.9743427900693199 |
    6. | P | 1 |
    7. +-------+---------------------+

    The above gives the cumulative frequencies. Normally, I would rather want to see the actual frequencies of each value, and to get that I will need to subtract the value of the previous row. We can use a window function to do that:

    1. mysql> SELECT v value, c cumulfreq, c - LAG(c, 1, 0) OVER () freq
    2. -> FROM information_schema.column_statistics,
    3. -> JSON_TABLE(histogram->'$.buckets', '$[*]'
    4. -> COLUMNS(v VARCHAR(60) PATH '$[0]',
    5. -> c double PATH '$[1]')) hist
    6. -> WHERE schema_name = 'dbt3_sf1'
    7. -> AND table_name ='orders'
    8. -> AND column_name = 'o_orderstatus';
    9. +-------+---------------------+----------------------+
    10. | value | cumulfreq | freq |
    11. +-------+---------------------+----------------------+
    12. | F | 0.48544670343055835 | 0.48544670343055835 |
    13. | O | 0.9743427900693199 | 0.48889608663876155 |
    14. | P | 1 | 0.025657209930680103 |
    15. +-------+---------------------+----------------------+
    16. 3 rows in set (0.00 sec)

    So by combining three new features in MySQL 8.0, histogram, JSON_TABLE, and window functions, I am able to quickly get an estimate for the frequencies of the possible values for my column.