10.1 통계 정보

10.1.1 테이블 및 인덱스 통계 정보

MySQL 서버의 통계 정보

MySQL 5.6 이전 MySQL 5.6 이후
휘발성 영구성
메모리에만 관리 InnoDB 스토리지 엔진 사용하는 테이블에 대한 통계 정보를 영구적으로 관리
ALTER TABLE testdb.employees STATS_PERSISTENT=1;

SELECT * 
FROM mysql.innodb_index_stats
WHERE database_name = 'testdb' AND table_name = 'employees';

+---------------+------------+---------------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name          | last_update         | stat_name    | stat_value | sample_size | stat_description                  |
+---------------+------------+---------------------+---------------------+--------------+------------+-------------+-----------------------------------+
| testdb        | employees  | PRIMARY             | 2022-10-14 21:17:31 | n_diff_pfx01 |     299335 |          20 | emp_no                            |
| testdb        | employees  | PRIMARY             | 2022-10-14 21:17:31 | n_leaf_pages |        886 |        NULL | Number of leaf pages in the index |
| testdb        | employees  | PRIMARY             | 2022-10-14 21:17:31 | size         |        929 |        NULL | Number of pages in the index      |
| testdb        | employees  | ix_firstname        | 2022-10-14 21:17:31 | n_diff_pfx01 |       1304 |          20 | first_name                        |
| testdb        | employees  | ix_firstname        | 2022-10-14 21:17:31 | n_diff_pfx02 |     329443 |          20 | first_name,emp_no                 |
| testdb        | employees  | ix_firstname        | 2022-10-14 21:17:31 | n_leaf_pages |        496 |        NULL | Number of leaf pages in the index |
| testdb        | employees  | ix_firstname        | 2022-10-14 21:17:31 | size         |        609 |        NULL | Number of pages in the index      |
| testdb        | employees  | ix_gender_birthdate | 2022-10-14 21:17:31 | n_diff_pfx01 |          1 |           3 | gender                            |
| testdb        | employees  | ix_gender_birthdate | 2022-10-14 21:17:31 | n_diff_pfx02 |       9061 |          20 | gender,birth_date                 |
| testdb        | employees  | ix_gender_birthdate | 2022-10-14 21:17:31 | n_diff_pfx03 |     309467 |          20 | gender,birth_date,emp_no          |
| testdb        | employees  | ix_gender_birthdate | 2022-10-14 21:17:31 | n_leaf_pages |        361 |        NULL | Number of leaf pages in the index |
| testdb        | employees  | ix_gender_birthdate | 2022-10-14 21:17:31 | size         |        417 |        NULL | Number of pages in the index      |
| testdb        | employees  | ix_hiredate         | 2022-10-14 21:17:31 | n_diff_pfx01 |       4468 |          20 | hire_date                         |
| testdb        | employees  | ix_hiredate         | 2022-10-14 21:17:31 | n_diff_pfx02 |     296778 |          20 | hire_date,emp_no                  |
| testdb        | employees  | ix_hiredate         | 2022-10-14 21:17:31 | n_leaf_pages |        294 |        NULL | Number of leaf pages in the index |
| testdb        | employees  | ix_hiredate         | 2022-10-14 21:17:31 | size         |        353 |        NULL | Number of pages in the index      |
+---------------+------------+---------------------+---------------------+--------------+------------+-------------+-----------------------------------+

SELECT *
FROM mysql.innodb_table_stats
WHERE database_name = 'testdb' AND table_name = 'employees';

+---------------+------------+---------------------+--------+----------------------+--------------------------+
| database_name | table_name | last_update         | n_rows | clustered_index_size | sum_of_other_index_sizes |
+---------------+------------+---------------------+--------+----------------------+--------------------------+
| testdb        | employees  | 2022-10-14 21:17:31 | 299335 |                  929 |                     1379 |
+---------------+------------+---------------------+--------+----------------------+---------------------

10.1.2 히스토그램

히스토그램 정보 수집 및 삭제

-- // 히스토그램 정보 수집
ANALYZE TABLE testdb.employees
UPDATE HISTOGRAM ON gender, hire_date;

+------------------+-----------+----------+------------------------------------------------------+
| Table            | Op        | Msg_type | Msg_text                                             |
+------------------+-----------+----------+------------------------------------------------------+
| testdb.employees | histogram | status   | Histogram statistics created for column 'gender'.    |
| testdb.employees | histogram | status   | Histogram statistics created for column 'hire_date'. |
+------------------+-----------+----------+------------------------------------------------------+

-- // 히스토그램 정보 조회
SELECT *
FROM information_schema.column_statistics
WHERE schema_name = 'testdb' AND table_name = 'employees' \\G

*************************** 1. row ***************************
SCHEMA_NAME: testdb
 TABLE_NAME: employees
COLUMN_NAME: gender
  HISTOGRAM: {"buckets": [[1, 0.6022134473264744], [2, 1.0]],
						 "data-type": "enum",
						 "null-values": 0.0,
						 "collation-id": 45,
						 "last-updated": "2022-10-18 14:48:18.023937",
						 "sampling-rate": 0.3464196832892688,
						 "histogram-type": "singleton",
						 "number-of-buckets-specified": 100
						}
*************************** 2. row ***************************
SCHEMA_NAME: testdb
 TABLE_NAME: employees
COLUMN_NAME: hire_date
  HISTOGRAM: {"buckets": [
                            ["1985-02-01", "1985-02-28", 0.009898927691638892, 28], 
                            ["1985-03-01", "1985-03-28", 0.020069555136936677, 28], 
                            ["1985-03-29", "1985-04-25", 0.0301405593392262, 28], 
                            ["1985-04-26", "1985-05-24", 0.04006665700623098, 29], 
                            ["1985-05-25", "1985-06-20", 0.05003803796551225, 27], 
                            ...
                            ["1998-08-07", "2000-01-28", 1.0, 467]
                         ], 
						"data-type": "date", 
						"null-values": 0.0, 
						"collation-id": 8, 
						"last-updated": "2022-10-18 14:48:18.033156", 
						"sampling-rate": 0.3464196832892688, 
						"histogram-type": "equi-height", 
						"number-of-buckets-specified": 100
						}