編碼的世界 / 優質文選 / 歷史

何時在mysql中使用TEXT而不是VARCHAR


2022年5月15日
-   

Since VARCHAR can have 65k bytes now, when then should TEXT be used instead of VARCHAR?
解決方案
A long VARCHAR is stored in the same manner as a TEXT/BLOB field in InnoDB.
From storage prospective BLOB, TEXT as
well as long VARCHAR are handled same
way by Innodb. This is why Innodb
manual calls it “long columns” rather
than BLOBs.
Unless you need to index these columns (in which case VARCHAR is much faster) there is no reason to use VARCHAR over TEXT for long fields - there are some engine specific optimisations in MySQL to tune the data retrieval according to length, and you should use the correct column type to take advantage of these.
In case you're using MyISAM an in-depth discussion on the topic is here.
TEXT and BLOB are stored off the table with the table just having a pointer to the location of the actual storage.
VARCHAR is stored inline with the table. VARCHAR is faster when the size is reasonable.
According to this test, VARCHAR is about thrice as fast as text.

熱門文章