Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will indexing improve varchar(max) query performance, and how to create index

Firstly, I should point out I don't have much knowledge on SQL Server indexes.

My situation is that I have an SQL Server 2008 database table that has a varchar(max) column usually filled with a lot of text.

My ASP.NET web application has a search facility which queries this column for keyword searches, and depending on the number of keywords searched for their may be one or many LIKE '%keyword%' statements in the SQL query to do the search.

My web application also allows searching by various other columns in this table as well, not just that one column. There is also a few joins from other tables too.

My question is, is it worthwhile creating an index on this column to improve performance of these search queries? And if so, what type of index, and will just indexing the one column be enough or do I need to include other columns such as the primary key and other searchable columns?

like image 986
johna Avatar asked May 02 '12 22:05

johna


People also ask

Can varchar max be indexed?

you cannot create an index on a varchar(max) field. The maximum amount of bytes on a index is 900. If the column is bigger than 900 bytes, you can create the index but any insert with more then 900 bytes will fail.

Does indexing improve query performance?

Indexing makes columns faster to query by creating pointers to where data is stored within a database. Imagine you want to find a piece of information that is within a large database. To get this information out of the database the computer will look through every row until it finds it.

Can we create index on varchar data type?

1) You can also create indexes on multiple fields combining int and varchar types, taking special care for filtering always on first field defined in the index. If you just filter using the second field index will not be used.


1 Answers

The best analogy I've ever seen for why an index won't help '%wildcard%' searches:

Take two people. Hand each one the same phone book. Say to the person on your left:

Tell me how many people are in this phone book with the last name "Smith."

Now say to the person on your right:

Tell me how many people are in this phone book with the first name "Simon."

An index is like a phone book. Very easy to seek for the thing that is at the beginning. Very difficult to scan for the thing that is in the middle or at the end.

Every time I've repeated this in a session, I see light bulbs go on, so I thought it might be useful to share here.

like image 196
Aaron Bertrand Avatar answered Oct 05 '22 08:10

Aaron Bertrand