MySQL: Large VARCHAR vs. TEXT?

后端 未结 8 1317
庸人自扰
庸人自扰 2020-11-22 09:21

I\'ve got a messages table in MySQL which records messages between users. Apart from the typical ids and message types (all integer types) I need to save the actual message

相关标签:
8条回答
  • 2020-11-22 10:14

    There is a HUGE difference between VARCHAR and TEXT. While VARCHAR fields can be indexed, TEXT fields cannot. VARCHAR type fields are stored inline while TEXT are stored offline, only pointers to TEXT data is actually stored in the records.

    If you have to index your field for faster search, update or delete than go for VARCHAR, no matter how big. A VARCHAR(10000000) will never be the same as a TEXT field bacause these two data types are different in nature.

    • If you use you field only for archiving
    • you don't care about data speed retrival
    • you care about speed but you will use the operator '%LIKE%' in your search query so indexing will not help much
    • you can't predict a limit of the data length

    than go for TEXT.

    0 讨论(0)
  • 2020-11-22 10:22

    Varchar is for small data like email addresses, while Text is for much bigger data like news articles, Blob for binary data such as images.

    The performance of Varchar is more powerful because it runs completely from memory, but this will not be the case if data is too big like varchar(4000) for example.

    Text, on the other hand, does not stick to memory and is affected by disk performance, but you can avoid that by separating text data in a separate table and apply a left join query to retrieve text data.

    Blob is much slower so use it only if you don't have much data like 10000 images which will cost 10000 records.

    Follow these tips for maximum speed and performance:

    1. Use varchar for name, titles, emails

    2. Use Text for large data

    3. Separate text in different tables

    4. Use Left Join queries on an ID such as a phone number

    5. If you are going to use Blob apply the same tips as in Text

    This will make queries cost milliseconds on tables with data >10 M and size up to 10GB guaranteed.

    0 讨论(0)
提交回复
热议问题