In my MySql DB I want to store some text which will be a mimimun of 1000 words, it will contain some special characters like "",'()<>-.!;
data will be passed using php.
There are several datatypes that hold text. When you declare any of them you can also specify their collation and character set. For example:
CREATE OR REPLACE TABLE mytable {
mytext VARCHAR(2000) COLLATE utf8mb4_general_ci
)
This gives you a place to store up to 2000 characters of Unicode text. Each Unicode character can take from 1 to 4 bytes of storage. Unicode, as you know, handles many different human scripts, not to mention emojis and math symbols.
To store text you have
CHAR()
data type if you know upfront exactly how many characters you must store.VARCHAR()
data type. It has best performance especially if you want to filter on it.LONGTEXT
... up to 4GiB in your text. This is what WordPress uses for content, for what it's worth.MEDIUMTEXT
... up to 16MiBTEXT
... up to 64KiBTINYTEXT
... up to 255 bytes (useless, use VARCHAR(255)
instead)These all have reasonable overhead in terms of disk space used.
If you want to put indexes on VARCHAR()
columns limit their size to VARCHAR(768)
.
Pro tip: Avoid the LOB data types unless you absolutely need them. They make the server do extra work when you use them in SELECT or WHERE clauses. Use VARCHAR()
instead.