MySQL數據類型之TEXT與BLOB的區別

因爲CHAR與VARCHAR只能用於存儲少量的字符串(1-65535個字符),所以當存儲大文本時,就需要用到TEXT或BLOB,其兩者的區別在於,TEXT只能保存字符串,而BLOB可以保存二進制數據(多媒體文件),見下表:

類型 可存儲字節 空間佔用

TINYTEXT/TINYBLOB

256 bytes 256bytes
TEXT/BLOB 65,535 bytes 64KB
MEDIUMTEXT/MEDIUMBLOB 16,777,215 bytes 16MB
LONGTEXT/LONGBLOB 4,294,967,295 bytes 4GB

這兩種數據類型在執行刪除(尤其是大量刪除)時會呈現出一些問題:

刪除操作會在表中留下很多的“空洞”,在未來向這些“空洞”內插入數據時會有性能虧損,所以爲了提高性能,建議使用optimize table命令對錶進行壓縮,儘量的消除“空洞"

STEP1:創建數據庫

create database test;

STEP2:調用數據庫並創建表

create table test(id char(1),testchar text);

STEP3:創建test表

create table test(id char(1),testchar text);

STEP4:向test表中使用repeat函數插入大量字符串,插入1000個Hello

insert into test values(1,repeat('Hello',1000));
insert into test values(2,repeat('Hello',1000));
insert into test values(3,repeat('Hello',1000));

STEP5:反覆插入數據,語句意爲將整表數據插入表(複製)

insert into test select * from test;
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 12 rows affected (0.02 sec)
Records: 12  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 24 rows affected (0.00 sec)
Records: 24  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 48 rows affected (0.11 sec)
Records: 48  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 96 rows affected (0.13 sec)
Records: 96  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 192 rows affected (0.68 sec)
Records: 192  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 384 rows affected (0.04 sec)
Records: 384  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 768 rows affected (0.04 sec)
Records: 768  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 1536 rows affected (0.07 sec)
Records: 1536  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 3072 rows affected (0.12 sec)
Records: 3072  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 6144 rows affected (1.81 sec)
Records: 6144  Duplicates: 0  Warnings: 0

insert into test select * from test;

Query OK, 12288 rows affected (3.15 sec)
Records: 12288  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 24576 rows affected (12.30 sec)
Records: 24576  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 49152 rows affected (22.52 sec)
Records: 49152  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 98304 rows affected (41.78 sec)
Records: 98304  Duplicates: 0  Warnings: 0

insert into test select * from test;
Query OK, 196608 rows affected (37.81 sec)
Records: 196608  Duplicates: 0  Warnings: 0

STEP6:查看物理文件大小,默認的mysql數據目錄在/var/lib/mysql數據庫與該目錄下的文件同名

cd /var/lib/mysql/test

du -sh test.ibd 
2.1G    test.ibd

STEP7:從數據庫中刪除所有ID=1的行

delete from test where id = 1;
Query OK, 131072 rows affected (33.21 sec)

STEP8:再進入數據庫目錄查看物理文件大小

du -sh test.ibd 
2.1G    test.ibd

STEP9:進入數據庫使用optimize table命令對錶進行碎片整理

optimize table test;
+-----------+----------+----------+-------------------------------------------------------------------+
| Table     | Op       | Msg_type | Msg_text                                                          |
+-----------+----------+----------+-------------------------------------------------------------------+
| test.test | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| test.test | optimize | status   | OK                                                                |
+-----------+----------+----------+-------------------------------------------------------------------+

STEP10:退出再次查看物理文件大小

du -sh test.ibd           
1.6G    test.ibd

可以從物理文件大小看出,表中的“空洞”已經被清空,這一特性與數據類型無關,是表本身呈現的問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章