[MDEV-11090] Copying table with OPTIMIZE/ALTER TABLE does not compress existing uncompressed pages Created: 2016-10-19  Updated: 2016-10-24  Resolved: 2016-10-24

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Alter Table, Storage Engine - InnoDB
Affects Version/s: 10.1.18
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Geoff Montee (Inactive) Assignee: Jan Lindström (Inactive)
Resolution: Not a Bug Votes: 0
Labels: innodb, page_compression

Issue Links:
Relates
relates to MDEV-11089 Enabling page compression with ALTER ... Closed

 Description   

Let's say that we create a table that does not use page compression, and we insert some data:

CREATE TABLE `tab` (
  `id` int(11) NOT NULL,
  `str` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO tab VALUES (1, 'str1');

And then let's say that we want to enable page compression for this table:

SET GLOBAL innodb_compression_algorithm=zlib;
ALTER TABLE tab PAGE_COMPRESSED=1;

As pointed out in MDEV-11089, the table's existing uncompressed pages are not going to be compressed at this point:

MariaDB [db1]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 0     |
+----------------------------------+-------+
1 row in set (0.00 sec)

Since an OPTIMIZE TABLE rewrites the table, I would expect that executing that statement against the table would compress the existing uncompressed pages in the new copy, but it seems that it does not:

MariaDB [db1]> OPTIMIZE TABLE tab;
+---------+----------+----------+-------------------------------------------------------------------+
| Table   | Op       | Msg_type | Msg_text                                                          |
+---------+----------+----------+-------------------------------------------------------------------+
| db1.tab | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| db1.tab | optimize | status   | OK                                                                |
+---------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0.03 sec)
 
MariaDB [db1]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 0     |
+----------------------------------+-------+
1 row in set (0.00 sec)

Copying the table with ALTER TABLE has similar results:

MariaDB [db1]> ALTER TABLE tab ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [db1]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 0     |
+----------------------------------+-------+
1 row in set (0.00 sec)



 Comments   
Comment by Jan Lindström (Inactive) [ 2016-10-24 ]

Works for me:

MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
MariaDB [test]> CREATE TABLE `tab` (
    ->   `id` int(11) NOT NULL,
    ->   `str` varchar(50) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0,08 sec)
 
MariaDB [test]> INSERT INTO tab VALUES (1, 'str1');
Query OK, 1 row affected (0,01 sec)
 
MariaDB [test]> SET GLOBAL innodb_compression_algorithm=zlib;
Query OK, 0 rows affected (0,00 sec)
 
MariaDB [test]> ALTER TABLE tab PAGE_COMPRESSED=1;
Query OK, 1 row affected (0,08 sec)                
Records: 1  Duplicates: 0  Warnings: 0
 
MariaDB [test]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 3     |
+----------------------------------+-------+
1 row in set (0,02 sec)
 
MariaDB [test]> OPTIMIZE TABLE tab;
+----------+----------+----------+-------------------------------------------------------------------+
| Table    | Op       | Msg_type | Msg_text                                                          |
+----------+----------+----------+-------------------------------------------------------------------+
| test.tab | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| test.tab | optimize | status   | OK                                                                |
+----------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0,10 sec)
 
MariaDB [test]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 6     |
+----------------------------------+-------+
1 row in set (0,03 sec)
 
MariaDB [test]> ALTER TABLE tab ENGINE=InnoDB;
Query OK, 0 rows affected (0,08 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 6     |
+----------------------------------+-------+
1 row in set (0,04 sec)
 
MariaDB [test]> SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
+----------------------------------+-------+
| Variable_name                    | Value |
+----------------------------------+-------+
| Innodb_num_pages_page_compressed | 9     |
+----------------------------------+-------+
1 row in set (0,02 sec)

Comment by Jan Lindström (Inactive) [ 2016-10-24 ]

Similarly to MDEV-11089, compression is done only when dirty pages are flushed to the disk. This is asynchronous operation and there could be delay before it happens.

Generated at Thu Feb 08 07:47:14 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.