Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress columns in MySQL?

I have a table which stores e-mail correspondences. Every time someone replies, the whole body of the trail is also included and saved into the database (and I need it that way because the amount of application level changes to rectify that are going to be too high).

The size of the mail text column is 10000. But, I am having trouble storing text more than that. As I am not sure, how many correspondences can occur, I don't know what a good number will be for the column.

The engine is InnoDB. Can I use some kind of columnar compression technique in MySQL to avoid increasing the size of the column?

And, what if I go ahead and increase the varchar column to, say, 20000. The table has about 2 million records. Will that be a good thing to do?

like image 827
MontyPython Avatar asked Sep 16 '25 08:09

MontyPython


2 Answers

You are probably looking for MySQL COMPRESS() and UNCOMPRESS() function to compress data for storage and retrieval respectively.

Also look at InnoDB Compression Usage.

like image 109
Rahul Tripathi Avatar answered Sep 19 '25 01:09

Rahul Tripathi


This answer is specific to Percona

Percona introduced a compressed column format a while ago. That you can use on CREATE or ALTERs

CREATE TABLE test_compressed (
    id INT NOT NULL PRIMARY KEY,
    value MEDIUMTEXT COLUMN_FORMAT COMPRESSED
 );

Reference: https://www.percona.com/doc/percona-server/5.7/flexibility/compressed_columns.html

like image 39
Romuald Brunet Avatar answered Sep 19 '25 02:09

Romuald Brunet