Collections:
COMPRESS() - Compressing Data
How to compress data using the COMPRESS() function?
✍: FYIcenter.com
COMPRESS(str) is a MySQL built-in function that
compresses a given byte string using the default LZ77 compression algorithm.
For example:
SET @str = 'FYIcenter.com,FYIcenter.com,FYIcenter.com,FYIcenter.com'; SET @bin = COMPRESS(@str); SELECT LENGTH(@str) AS StrSize, LENGTH(@bin) AS BinSize, HEX(@bin); -- +---------+---------+------------------------------------------------------------+ -- | StrSize | BinSize | HEX(@bin) | -- +---------+---------+------------------------------------------------------------+ -- | 55 | 29 | 37000000789C738BF44C4ECD2B492DD24BCECFD57123960700279D13DD | -- +---------+---------+------------------------------------------------------------+
Note that the compression rate depends on the byte sequence pattern of the input data. For example:
SET @str = REPEAT('a', 1000);
SET @bin = COMPRESS(@str);
SELECT LENGTH(@str), LENGTH(@bin), LENGTH(@bin)/LENGTH(@str);
-- +--------------+--------------+---------------------------+
-- | LENGTH(@str) | LENGTH(@bin) | LENGTH(@bin)/LENGTH(@str) |
-- +--------------+--------------+---------------------------+
-- | 1000 | 21 | 0.0210 |
-- +--------------+--------------+---------------------------+
SET @str = RANDOM_BYTES(1000);
SET @bin = COMPRESS(@str);
SELECT LENGTH(@str), LENGTH(@bin), LENGTH(@bin)/LENGTH(@str);
-- +--------------+--------------+---------------------------+
-- | LENGTH(@str) | LENGTH(@bin) | LENGTH(@bin)/LENGTH(@str) |
-- +--------------+--------------+---------------------------+
-- | 1000 | 1015 | 1.0150 |
-- +--------------+--------------+---------------------------+
Reference information of the COMPRESS() function:
COMPRESS(str): bin Compresses a given byte string using the default LZ77 compression algorithm. Arguments, return value and availability: str: Required. The byte string to be compressed. bin: Return value. The compressed byte string. Available since MySQL 4.1.
Related MySQL functions:
⇒ CRC32() - Cyclic Redundancy Check 32-Bit
⇐ AES_ENCRYPT() - AES Data Encryption
2023-12-15, 1386🔥, 0💬
Popular Posts:
How To Use SQL*Plus Built-in Timers in Oracle? If you don't have a stopwatch/timer and want to measu...
How To Generate CREATE VIEW Script on an Existing View in SQL Server? If you want to know how an exi...
Where Is the Export Dump File Located in Oracle? If you are not specifying the dump directory and fi...
How To Divide Query Output into Multiple Groups with the GROUP BY Clause in SQL Server? Sometimes, y...
How To Drop an Index in Oracle? If you don't need an existing index any more, you should delete it w...