tags:

views:

448

answers:

5

This question is based on two things performance and size

Which DATATYPE is better to use TEXT or VARCHAR? Based on performance which will affect and which will impove?

+4  A: 

VARCHAR you can set a limit for how many chars it will accept per record, text is (virtually) unlimited... not exactly sure about performance, but i would assume a more specific datatype (varchar) would be faster.

Jason
+3  A: 

VARCHAR should have a better performance since it has a limited size. In fact, in all of my experiences with MySQL, the search operation was always faster with VARCHAR than TEXT. Anyway, it's based on my experience. You should check the documentation to find out more about it.

Fernando
+5  A: 

It depends on what you're using it for. I hate to give such a generic answer, but it's true. Generally, try to get the datatype as specific as you can. If you can live with a 255-character limit (or whatever you want to put it as), then go with VARCHAR because it will be faster and more efficient. If you need more space, go with TEXT. If you aren't sure how much space your text will take up, you should probably go with TEXT; the performance difference isn't that large, and it's better to be future-proof than risk having to change it later when your requirements change. Just my two cents.

musicfreak
+1 for it depends cause it always does. Just as a side note I've altered columns before from varchar to text and all seems to convert nicely. It seems mysql will try to intelligently convert the data when its datatype changes.
rezzif
+2  A: 

It really depends on your data type.

If your field is fixed-length (e.g. a 32-character hash value), then use CHAR. This has better performance because every entry takes up the same space per row.

The standard limit for VARCHAR was 255 characters but I think it's been increased now. TEXT is pretty damn long and is generally only used for big content like a whole blog post, and comments if you don't want a limit.

With regard to size there is no (or very little) difference between VARCHAR and TEXT since they just store what they need to. CHAR fields will always take up their allotted length.

Performance-wise, VARCHAR is usually faster. VARCHARs can be indexed too which leads to faster searching.

DisgruntledGoat
A: 

MySQL will internally convert TEXT to varchar while creating temporary tables. So it is better to use VARCHAR if possible. There are a few minor bugs related to TEXT column such as...

http://bugs.mysql.com/bug.php?id=36676

shantanuo