当前位置:网站首页>What happens when MySQL tables change from compressed tables to ordinary tables
What happens when MySQL tables change from compressed tables to ordinary tables
2022-07-28 06:25:00 【Strange patron saint】
Preface
This article has done mysql The experimental process of table transition from compressed table to ordinary table , See what happens when a compressed table becomes an ordinary table ? This paper aims at mysql5.7 and mysql8 Experiments were carried out .
1、 What is table compression
Before introducing compressed tables into ordinary tables , First of all, let's popularize , What is table compression .
Table compression , It means that the data in the table is stored in a compressed format , Compression can significantly improve processing speed and compress disk . Compression means that the data transferred between hard disk and memory is smaller and takes up relatively less memory and hard disk , For secondary indexes , This compression brings more obvious benefits , Because the index data is also compressed .
Table compression is of great benefit , Can reduce disk I/O, It can also improve system throughput , Save space , The more compression , The smaller the disk space used , File transfer time increases , Reduce data storage and network transmission costs .
2、 How to compress tables ( mysql The version of must be greater than 5.5 )
1、 The first set my.inf Parameters
# Open profile
vim /etc/my.inf
# Add configuration items
innodb_file_per_table=1
innodb_file_format=Barracuda
innodb_strict_mode=1 # Advice and
innodb_default_row_format = COMPRESSED # Set when row compression format is enabled by default for the entire library , Do not change this value
# Restart the database
systemctl restart mysqld
2、 Compress the table
mysql> alter table t1 ROW_FORMAT=COMPRESSED;
3、 Convert compressed table to normal table
mysql> alter table t1 ROW_FORMAT=DEFAULT;
in the light of mysql5.7 Start the experiment
mysql Database version :5.7.31
linux edition :centos5.7
1、 Create tables and initialize test data
#1、 Build table
CREATE TABLE test_compress (
id bigint(20) unsigned NOT NULL,
identification_id int(10) unsigned DEFAULT NULL,
timestamp datetime NOT NULL,
action varchar(50) NOT NULL,
result varchar(50) NOT NULL,
PRIMARY KEY (id),
KEY INDEX_test_compress_result (result),
KEY INDEX_test_compress_timestamp (timestamp)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#2、 Insert test data (linux Execute script in )
for NUM in {
1..100000}; do mysql -h localhost PS_57 -e "insert into test_compress (id, identification_id, timestamp, action, result) values ($NUM,$NUM*100,now(),concat('string',$NUM),concat('VeryVeryLargeString',$NUM))"; done
2、 Verify the size of the table
Let's verify the size of the table ( Before execution innodb_stats_persistent_sample_pages=100000 Of ANALYZE surface , So that the statistical information is as true as possible ).
set global innodb_stats_persistent_sample_pages=100000;
analyze table test_compress;
+------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------+---------+----------+----------+
| PS_57.test_compress | analyze | status | OK |
+------------------------+---------+----------+----------+
Query OK, 0 rows affected (0.00 sec)
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+----------------+
| table_schema | table_name | table_rows | TOTAL_MB | create_options |
+--------------+---------------+------------+----------+----------------+
| PS_57 | test_compress | 100000 | 37 | |
+--------------+---------------+------------+----------+----------------+
3、 Compress the table
Next , We will use KEY_BLOCK_SIZE=4 Compression meter ( This size is arbitrary , At no time does it indicate or determine whether it is the optimal value , in fact , It is not ).
ALTER TABLE test_compress ROW_FORMAT=COMPRESSED,KEY_BLOCK_SIZE=4,ALGORITHM=INPLACE,LOCK=NONE;
Query OK, 0 rows affected (3.33 sec)
We verify the size of the table again ( Previously implemented innodb_stats_persistent_sample_pages=100000 Of ANALYZE surface , So that the statistical information is as true as possible ).
set global innodb_stats_persistent_sample_pages=100000;
Query OK, 0 rows affected (0.00 sec)
analyze table test_compress;
+------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------+---------+----------+----------+
| PS_57.test_compress | analyze | status | OK |
+------------------------+---------+----------+----------+
Query OK, 0 rows affected (0.00 sec)
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+----------------------------------------+
| table_schema | table_name | table_rows | TOTAL_MB | create_options |
+--------------+---------------+------------+----------+----------------------------------------+
| PS_57 | test_compress | 100000 | 19 | row_format=COMPRESSED KEY_BLOCK_SIZE=4 |
+--------------+---------------+------------+----------+----------------------------------------+
The table has been compressed , Let's check its structure .
show create table test_compress;
*************************** 1. row ***************************
Table: test_compress
Create Table: CREATE TABLE `test_compress` (
`id` bigint(20) unsigned NOT NULL,
`identification_id` int(10) unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `INDEX_test_compress_result` (`result`),
KEY `INDEX_test_compress_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4
1 row in set (0.00 sec)
4、 Decompress the compressed table ( Become an ordinary watch )
ALTER TABLE test_compress ROW_FORMAT=DEFAULT,ALGORITHM=INPLACE,LOCK=NONE;
Query OK, 0 rows affected (6.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
Decompress successfully , Let's check .
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+--------------------+
| table_schema | table_name | table_rows | TOTAL_MB | create_options |
+--------------+---------------+------------+----------+--------------------+
| PS_57 | test_compress | 100000 | 25 | KEY_BLOCK_SIZE=4 |
+--------------+---------------+------------+----------+--------------------+
Better inspection :
show create table test_compress;
*************************** 1. row ***************************
Table: test_compress
Create Table: CREATE TABLE `test_compress` (
`id` bigint(20) unsigned NOT NULL,
`identification_id` int(10) unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `INDEX_test_compress_result` (`result`),
KEY `INDEX_test_compress_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=4
Something's wrong !KEY_BLOCK_SIZE Still 4.
A second try :
ALTER TABLE test_compress ROW_FORMAT=DEFAULT,KEY_BLOCK_SIZE=0,ALGORITHM=INPLACE,LOCK=NONE;
Query OK, 0 rows affected (2.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+--------------------+
| table_schema | table_name | table_rows | TOTAL_MB | create_options |
+--------------+---------------+------------+----------+--------------------+
| PS_57 | test_compress | 100000 | 25 | |
+--------------+---------------+------------+----------+--------------------+
Better inspection :
show create table test_compress\G
*************************** 1. row ***************************
Table: test_compress
Create Table: CREATE TABLE `test_compress` (
`id` bigint(20) unsigned NOT NULL,
`identification_id` int(10) unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`) KEY_BLOCK_SIZE=4,
KEY `INDEX_test_compress_result` (`result`) KEY_BLOCK_SIZE=4,
KEY `INDEX_test_compress_timestamp` (`timestamp`) KEY_BLOCK_SIZE=4
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Something's wrong ! The primary key and secondary index continue to display KEY_BLOCK_SIZE=4.
Although when the table is converted from compressed to uncompressed , In the internal , Indexed KEY_BLOCK_SIZE Support table index , but CREATE TABLE Statements are not . At first , This will be an aesthetics / Appearance problems , But when you dump , It's a real problem , because CREATE TABLE Retain the KEY_BLOCK_SIZE value , This is not good . Here are mysqldump Output :
mysqldump -h localhost PS_57 test_compress --no-data > test_compress.sql
cat test_compress.sql
...
--
-- Table structure for table `test_compress`
--
DROP TABLE IF EXISTS `test_compress`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `test_compress` (
`id` bigint(20) unsigned NOT NULL,
`identification_id` int(10) unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`) KEY_BLOCK_SIZE=4,
KEY `INDEX_test_compress_result` (`result`) KEY_BLOCK_SIZE=4,
KEY `INDEX_test_compress_timestamp` (`timestamp`) KEY_BLOCK_SIZE=4
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
As you can see , There seems to be no way to use the whole ALTER TABLE command ( If you can call it like this ) Reverse the table definition index KEY_BLOCK_SIZE, So we will try for the last time :
ALTER TABLE test_compress
DROP PRIMARY KEY, add PRIMARY KEY (id),
DROP key INDEX_test_compress_result, add key INDEX_test_compress_result (result),
DROP key INDEX_test_compress_timestamp, add key INDEX_test_compress_timestamp (timestamp),
ROW_FORMAT=DEFAULT,KEY_BLOCK_SIZE=0,ALGORITHM=INPLACE,LOCK=NONE;
Now? , It has the right definition , No, KEY_BLOCK_SIZE:
show create table test_compress;
*************************** 1. row ***************************
Table: test_compress
Create Table: CREATE TABLE `test_compress` (
`id` bigint(20) unsigned NOT NULL,
`identification_id` int(10) unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `INDEX_test_compress_result` (`result`),
KEY `INDEX_test_compress_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+----------------+
| table_schema | table_name | table_rows | TOTAL_MB | create_options |
+--------------+---------------+------------+----------+----------------+
| PS_57 | test_compress | 100000 | 25 | |
+--------------+---------------+------------+----------+----------------+
5、 For 4 There is a problem in step bug
mysql This is explained in bug: https://bugs.mysql.com/bug.php?id=56628
in the light of mysql8 experiment
stay MySQL 8 in , The situation is as follows :
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_ROWS | TOTAL_MB | CREATE_OPTIONS |
+--------------+---------------+------------+----------+----------------+
| PS_8 | test_compress | 31000 | 15 | |
+--------------+---------------+------------+----------+----------------+
Let's carry out ALTER To compress the table :
alter table test_compress ROW_FORMAT=COMPRESSED,KEY_BLOCK_SIZE=4,ALGORITHM=INPLACE,LOCK=NONE;
Query OK, 0 rows affected (4.54 sec)
Records: 0 Duplicates: 0 Warnings: 0
Let's check again :
analyze table test_compress;
+-----------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------------+---------+----------+----------+
| PS_8.test_compress | analyze | status | OK |
+-----------------------+---------+----------+----------+
1 row in set (0.07 sec)
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+----------------------------------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_ROWS | TOTAL_MB | CREATE_OPTIONS |
+--------------+---------------+------------+----------+----------------------------------------+
| PS_8 | test_compress | 100000 | 19 | row_format=COMPRESSED KEY_BLOCK_SIZE=4 |
+--------------+---------------+------------+----------+----------------------------------------+
show create table test_compress;
*************************** 1. row ***************************
Table: test_compress
Create Table: CREATE TABLE `test_compress` (
`id` bigint unsigned NOT NULL,
`identification_id` int unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `INDEX_test_compress_result` (`result`),
KEY `INDEX_test_compress_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4
1 row in set (0.01 sec)
up to now , Everything goes with MySQL 5.7 identical :KEY_BLOCK_SIZE Remain in the definition of the entire table , Not in the definition of index .
alike , You can also pass below sql Decompress the table :
alter table test_compress ROW_FORMAT=DEFAULT, KEY_BLOCK_SIZE=0,ALGORITHM=INPLACE,LOCK=NONE;
Query OK, 0 rows affected (2.56 sec)
Records: 0 Duplicates: 0 Warnings: 0
Check the decompression
show create table test_compress;
*************************** 1. row ***************************
Table: test_compress
Create Table: CREATE TABLE `test_compress` (
`id` bigint unsigned NOT NULL,
`identification_id` int unsigned DEFAULT NULL,
`timestamp` datetime NOT NULL,
`action` varchar(50) NOT NULL,
`result` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `INDEX_test_compress_result` (`result`),
KEY `INDEX_test_compress_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
select table_schema, table_name, table_rows, round(data_length / 1024 / 1024)+round(index_length / 1024 / 1024)+round(data_free / 1024 / 1024) TOTAL_MB, create_options from information_schema.tables where table_name='test_compress';
+--------------+---------------+------------+----------+----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_ROWS | TOTAL_MB | CREATE_OPTIONS |
+--------------+---------------+------------+----------+----------------+
| PS_8 | test_compress | 100000 | 25 | |
+--------------+---------------+------------+----------+----------------+
Conclusion
stay MySQL 5.7 in , The only way to completely decompress a compressed table ( At least in the definition of tables and their indexes ) Is to regenerate the primary key and all its indexes . otherwise , When the primary key and secondary index continue to display the compressed table KEY_BLOCK_SIZE.
And then in MySQL8 in , Fixed this problem in MySQL5.7 What happened .
More content, please pay attention to WeChat official account. 【 Programming talent 】, Share high-quality articles , Programming black Technology , Help you become a programmer !
边栏推荐
- 福禄克DSX2-5000 网络线缆测试仪为什么每年都要校准一次?
- TCL and eltcl? Cdnext and CMRL?
- set_clock_groups
- Perl入门学习(八)子程序
- 雷达成像 Matlab 仿真 2 —— 脉冲压缩与加窗
- EXFO 730C光时域反射计只有iOLM光眼升级OTDR(开通otdr权限)
- mixup_ratio
- Low power design isolation cell
- (PHP graduation project) based on PHP online travel website management system to obtain
- Cronbach’s α?KMO系数?因子载荷?史上最易懂的问卷信效度分析教程!!!(SPSS和AMOS)
猜你喜欢

浅谈FLUKE光缆认证?何为CFP?何为OFP?

VAN(DWConv+DWDilationConv+PWConv)

How can fluke dsx2-5000 and dsx2-8000 modules find the calibration expiration date?

【服务器使用记录】通过跳板机登录远程服务器并进行文件传输

Led selection - hardware learning notes 3

权重衰减 weight decay

Photovoltaic power generation system MPPT maximum power point tracking

Detailed explanation of creepage distance and electrical clearance

Triode design, understanding saturation, linear region and cut-off region

set_ case_ analysis
随机推荐
clickhouse聚合之内存不足怎么办?那就提升聚合性能
Weight decay
(PHP graduation project) based on PHP online travel website management system to obtain
低功耗设计-Power Switch
(PHP graduation project) based on thinkphp5 community property management system
VAN(DWConv+DWDilationConv+PWConv)
When to replace jack socket for dsx-pc6 jumper module?
Install visual studio 2019 steps and vs2019 offline installation package on win7
AEM testpro K50 and south Guangdong survey
Esxi on arm 10/22 update
Beginners choose sensors
3、 Openvino practice: image classification
PyTorch 学习笔记 2 —— About Tensor
TCL and eltcl? Cdnext and CMRL?
Cronbach’s α?KMO系数?因子载荷?史上最易懂的问卷信效度分析教程!!!(SPSS和AMOS)
VB OCX applied to Web
短跳线DSX-8000测试正常,但是DSX-5000测试无长度显示?
clickhouse聚合之探索聚合内部机制
(PHP graduation project) obtain the campus network repair application management system based on PHP
ICC2(一)Preparing the Design