Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databases: Are "TEXT" fields less efficient than "varchar"?

Is it less efficient to use TEXT than varchar in an SQL database?

If so why?

If not why would you not just always use TEXT?

I'm not targetting a specific database here but oracle is probably the most relevant, although I'm testing on MySQL for the time being as part of a proof of concept.

like image 840
Omar Kooheji Avatar asked Oct 16 '08 09:10

Omar Kooheji


People also ask

Is VARCHAR more efficient than TEXT?

VARCHAR is stored inline with the table (at least for the MyISAM storage engine), making it potentially faster when the size is reasonable. Of course, how much faster depends on both your data and your hardware. Meanwhile, TEXT is stored off table with the table having a pointer to the location of the actual storage.

Which is better TEXT or VARCHAR?

TEXT: The Short Answer. If you're looking for a TL;DR, it's this: use VARCHAR if your data is of variable length and you know it fits into VARCHAR's 65,535 character limit. In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed.

What is difference between CHAR VARCHAR and TEXT?

CHAR items, which are fixed length, are the fastest to store and retrieve but can waste storage space. VARCHAR, a variable-length string, can be slower to store and retrieve but does not waste storage space. TEXT is a character BLOB that requires more storage space and I/O than the other two.

Is VARCHAR always better than CHAR?

Because of the fixed field lengths, data is pulled straight from the column without doing any data manipulation and index lookups against varchar are slower than that of char fields. CHAR is better than VARCHAR performance wise, however, it takes unnecessary memory space when the data does not have a fixed-length.


2 Answers

From Microsoft here

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

When you use varchar(max) over text you can use it in the WHERE clause, because they work the same as their smaller counterparts, varchar,nvarchar and varbinary. Below is a small list of what should be used as opposed what was to be used:

  • Use varchar(max) instead of text
  • Use nvarchar(max) instead of ntext
  • Use varbinary(max) instead of image
like image 167
Galwegian Avatar answered Oct 11 '22 14:10

Galwegian


PostgreSQL documentation says:

Tip: There are no performance differences between these three types, apart from increased storage size when using the blank-padded type, and a few extra cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, it has no such advantages in PostgreSQL. In most situations text or character varying should be used instead.

like image 21
Milen A. Radev Avatar answered Oct 11 '22 14:10

Milen A. Radev