使用MySQL的压缩功能来减少磁盘空间的占用。在创建表时,可以将BigInt列指定为压缩列(COMPRESSED)。这将使用可变长度编码以更有效的方式存储大多数小数字。
示例代码:
CREATE TABLE mytable ( id INT NOT NULL, mybigint BIGINT COMPRESSED NOT NULL, PRIMARY KEY (id) );
INSERT INTO mytable (id, mybigint) VALUES (1, 123456); INSERT INTO mytable (id, mybigint) VALUES (2, 7);
SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_KEY, INDEX_TYPE, COMPRESSION FROM information_schema.columns WHERE TABLE_SCHEMA = 'mydb' AND TABLE_NAME = 'mytable';
结果:
+-----------+---------------------+-----------+------------+-------------+ | COLUMN_NAME | COLUMN_TYPE | COLUMN_KEY | INDEX_TYPE | COMPRESSION | +-----------+---------------------+-----------+------------+-------------+ | id | int(11) | PRI | | | | mybigint | bigint(20) COMPRESSED | | | ZLIB | +-----------+---------------------+-----------+------------+-------------+
下一篇:BigInt是否表示64位数字?