Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a char/varchar/text column, why will an index for that column make it faster to search?

If it's an int, I know it will be faster, just cannot understand the string type.

notes: most Asian language don't have the space between words. and mysql cannot split the sentence to words. and also, I mean random search, that is, words can appears in any where in a sentence .

like image 465
lovespring Avatar asked Sep 09 '09 12:09

lovespring


People also ask

Why does indexing make faster?

Simple terms, an index is a data structure that maps search keys on disk to their corresponding data in memory. Indexes increase the speed of searches, since they reduce the amount of records to be found. The most effective way of improving database application performance is to use indexes.

What is the advantage of having an index on a column?

It helps us find the rows or the values to be searched faster. They have various advantages like increased performance in searching for records, sorting records, grouping records, or maintaining a unique column.

Which is faster VARCHAR or CHAR?

VARCHAR is used to store variable length character strings up to 4000 characters. But, remember CHAR is faster than VARCHAR - some times up to 50% faster.

How does indexing find rows faster?

When a data is inserted, a corresponding row is written to the index, and when a row is deleted, its index row is taken out. This keeps the data and searching index always in sync making the lookup very fast and read-time efficient.


1 Answers

One big point is that an index won't help at all for certain kinds of searches. For example:

SELECT * FROM [MyTable] WHERE [MyVarcharColumn] LIKE '%' + @SearchText + '%'

No amount of normal indexing will help that query. It's forever doomed to be slow. That LIKE expression is just not sargable.

Why? You first need to understand how indexes work. They basically take the columns being indexed along with the primary key (record pointer) into a new table. They then sort that table on the indexed column rather than the key. When you do a lookup using the index, it can very quickly find the row(s) you want because this index is sorted to facilitate a more efficient search using algorithms like binary search and others.

Now look at that query again. By placing a wildcard in front of the search text, you've just told the database that you don't know for sure what your column starts with. No amount of sorting will help; you still need to go through the entire table to be sure you find every record that matches the expression. And that means any normal index on the column is worthless for this query.

If you want to search a text column for a search string anywhere in the column, you need to use something a little different: a full-text index.

Now for contrast look at this query:

SELECT * FROM [MyTable] WHERE [MyVarcharColumn] LIKE @SearchText + '%'

This will work perfectly fine with an normal index, because you know how you expect the column to start. It can still match up with the sorted values stored in the index, and so we can say that it is sargable.

like image 63
Joel Coehoorn Avatar answered Oct 14 '22 22:10

Joel Coehoorn