数据库类型空间效率探索-tinyint与enum与set

Posted 鴻飛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库类型空间效率探索-tinyint与enum与set相关的知识,希望对你有一定的参考价值。

mysql> select count(*) from userinfo;
+----------+
| count(*) |
+----------+
| 115597 |
+----------+
1 row in set (0.00 sec)

mysql> select concat(truncate(sum(data_length)/1024/1024,3),‘MB‘) as data_size,

-> concat(truncate(sum(max_data_length)/1024/1024,3),‘MB‘) as max_data_leng
th,
-> concat(truncate(sum(data_free)/1024/1024,3),‘MB‘) as data_free,
-> concat(truncate(sum(index_length)/1024/1024,3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.477MB | 268435455.999MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+

以下测试tinyint

mysql> ALTER TABLE `userinfo`
-> ADD COLUMN `type` tinyint NOT NULL DEFAULT 0 COMMENT ‘反应类型‘ AFTER `i
ntegral`;
Query OK, 115597 rows affected (0.54 sec)
Records: 115597 Duplicates: 0 Warnings: 0

mysql> select concat(truncate(sum(data_length)/1024/1024,3),‘MB‘) as data_size,

-> concat(truncate(sum(max_data_length)/1024/1024,3),‘MB‘) as max_data_leng
th,
-> concat(truncate(sum(data_free)/1024/1024,3),‘MB‘) as data_free,
-> concat(truncate(sum(index_length)/1024/1024,3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.477MB | 268435455.999MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+

mysql> insert into userinfo(app,imei,type) values(‘‘,‘0‘,43);
Query OK, 1 row affected (0.00 sec)

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘
-> ;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.478MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.00 sec)

mysql> update userinfo set type=30;
Query OK, 115598 rows affected (2.70 sec)
Rows matched: 115598 Changed: 115598 Warnings: 0

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 22.038MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.00 sec)

以下测试enum

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.478MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+

mysql> ALTER TABLE `userinfo`
-> ADD COLUMN `type` enum(‘未知‘,‘化合‘,‘分解‘,‘置换‘,‘复分解‘,‘取代‘,‘加成‘
,‘消去‘,‘加聚‘,‘酯化‘,‘水解‘,‘聚合‘,‘缩聚‘,‘吸热‘,‘放热‘,‘氧化‘,‘还原‘) AFTER `i
ntegral`;
Query OK, 115597 rows affected (0.63 sec)
Records: 115597 Duplicates: 0 Warnings: 0

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.694MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.00 sec)

mysql> update userinfo set type=‘复分解‘;
Query OK, 115597 rows affected (2.54 sec)
Rows matched: 115597 Changed: 115597 Warnings: 0

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.694MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.00 sec)

以下测试set

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘
-> ;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.478MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.00 sec)

mysql> ALTER TABLE `userinfo`
-> ADD COLUMN `type` set(‘未知‘,‘化合‘,‘分解‘,‘置换‘,‘复分解‘,‘取代‘,‘加成‘,
‘消去‘,‘加聚‘,‘酯化‘,‘水解‘,‘聚合‘,‘缩聚‘,‘吸热‘,‘放热‘,‘氧化‘,‘还原‘) AFTER `in
tegral`;
Query OK, 115597 rows affected (0.61 sec)
Records: 115597 Duplicates: 0 Warnings: 0

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 21.590MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.01 sec)

mysql> update userinfo set type=‘加成‘;
Query OK, 115597 rows affected (3.63 sec)
Rows matched: 115597 Changed: 115597 Warnings: 0

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 23.235MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.00 sec)

mysql> update userinfo set type=‘加成,取代,消去,放热‘;
Query OK, 115597 rows affected (4.37 sec)
Rows matched: 115597 Changed: 115597 Warnings: 0

mysql> select concat(round(sum(data_length/1024/1024),3),‘MB‘) as data_size,
-> concat(round(sum(max_data_length/1024/1024),3),‘MB‘) as max_data_length,
-> concat(round(sum(data_free/1024/1024),3),‘MB‘) as data_free,
-> concat(round(sum(index_length/1024/1024),3),‘MB‘) as index_length
-> from information_schema.tables where table_name=‘userinfo‘;
+-----------+-----------------+-----------+--------------+
| data_size | max_data_length | data_free | index_length |
+-----------+-----------------+-----------+--------------+
| 23.235MB | 268435456.000MB | 0.000MB | 1.319MB |
+-----------+-----------------+-----------+--------------+
1 row in set (0.01 sec)

以上是关于数据库类型空间效率探索-tinyint与enum与set的主要内容,如果未能解决你的问题,请参考以下文章

Tinyint(byte),SmallInt(Int16) 与 EF5 中的 Enum 不兼容

MYSQL_精讲数据库数据类型

数据库类型空间效率探索

MYSQL09_精讲数据库数据类型

MySQL数据类型

TINYINT,SMALLINT,MEDIUMINT,INT,INTEGER,BIGINT;text,longtext,mediumtext,ENUM,SET等字段类型区别