sql-serverperformancetextvarcharsql-types

Using varchar(MAX) vs TEXT on SQL Server


I just read that the VARCHAR(MAX) datatype (which can store close to 2GB of char data) is the recommended replacement for the TEXT datatype in SQL Server 2005 and Next SQL SERVER versions.

If I want to search inside a column for any string, which operation is quicker?

  1. Using a the LIKE clause against a VARCHAR(MAX) column?

    WHERE COL1 LIKE '%search string%'

  2. Using the TEXT column and put a Full Text Index/Catalog on this column, and then search using the CONTAINS clause?

    WHERE CONTAINS (Col1, 'MyToken')


Solution

  • The VARCHAR(MAX) type is a replacement for TEXT. The basic difference is that a TEXT type will always store the data in a blob whereas the VARCHAR(MAX) type will attempt to store the data directly in the row unless it exceeds the 8k limitation and at that point it stores it in a blob.

    Using the LIKE statement is identical between the two datatypes. The additional functionality VARCHAR(MAX) gives you is that it is also can be used with = and GROUP BY as any other VARCHAR column can be. However, if you do have a lot of data you will have a huge performance issue using these methods.

    In regard to if you should use LIKE to search, or if you should use Full Text Indexing and CONTAINS. This question is the same regardless of VARCHAR(MAX) or TEXT.

    If you are searching large amounts of text and performance is key then you should use a Full Text Index.

    LIKE is simpler to implement and is often suitable for small amounts of data, but it has extremely poor performance with large data due to its inability to use an index.