I need to store a very big amount of text in mysql database. It will be millions of records with field type LONGTEXT and database size will be huge.
So, I want ask, if there is a safe way to compress text before storing it into TEXT field to save space, with ability to extract it back if needed?
Something like:
$archived_text = compress_text($huge_text);
// saving $archived_text to database here
// ...
// ...
// getting compressed text from database
$archived_text = get_text_from_db();
$huge_text = uncompress_text($archived_text);
Is there a way to do this with php or mysql? All the texts are utf-8 encoded.
UPDATE
My application is a large literature website where users can add their texts. Here is the table I have:
CREATE TABLE `book_parts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`book_id` int(11) NOT NULL,
`title` varchar(200) DEFAULT NULL,
`content` longtext,
`order_num` int(11) DEFAULT NULL,
`views` int(10) unsigned DEFAULT '0',
`add_date` datetime DEFAULT NULL,
`is_public` tinyint(3) unsigned NOT NULL DEFAULT '1',
`published_as_draft` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `key_order_num` (`order_num`),
KEY `add_date` (`add_date`),
KEY `key_book_id` (`book_id`,`is_public`,`order_num`),
CONSTRAINT FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Currently it has about 800k records and weights 4 GB, 99% of queries are SELECT. I have all reasons to think that numbers increase diagrammatically. I wouldn't like to store texts in the files because there is quite heavy logic around and my website has quite a few hits.
You may also want to use a COMPRESS option to enable compression of packets. Read some information about this option:
For PHP I have found this - MYSQLI_CLIENT_COMPRESS for mysqli_real_connect function.
You could use php functions gzdeflate and gzinflate for text.
Are you going to index these texts. How big is read load on this texts? Insert load?
You can use InnoDB data compression - transparent and modern way. See docs for more info.
If you have realy huge texts (say, each text is above 10MB), than good idea is not to store them in Mysql. Store compressed by gzip texts in file system and only pointers and meta in mysql. You can easily expand your storage in future and move it to e.g. DFS.
Update: another plus of storing texts outside Mysql: DB stays small and fast. Minus: high probability of data inconsistence.
Update 2: if you have much programming resourses, please, take a look on projects like this one: http://code.google.com/p/mysql-filesystem-engine/.
Final Update: according to your info, you can just use InnoDB compression - it is the same as ZIP. You can start with these params:
Later you will need to play with
KEY_BLOCK_SIZE
. SeeSHOW STATUS LIKE 'COMPRESS_OPS_OK'
andSHOW STATUS LIKE 'COMPRESS_OPS'
. Ratio of these two params must be close to 1.0: Docs.There are no benefits in compressing large texts into a database.
Here are the problems you might face in the long run:
I think storing these large texts into a disk file will be easier for:
If you're compressing (eg. gzip), then don't use TEXT fields of any sort. They're not binary-safe. Data going into/coming out of text fields is subject to character set translation, which probably (though not necessarily) mangle the compressed data and give you a corrupted result when you retrieve/uncompress the text.
Use BLOB fields instead, which are binary-transparent and do not to any translation of the data.
It might be better to define the text field as blob, and compress the data in PHP to save costs in communication.
In PHP, use gzcompress and gzuncompress.