Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index on a column with like and where pattern?

I've came to this conclusion that Like in below cases does seek/scan But I didn't get Why does it scan in 1st case and Seek in 2nd case. I understood the 3rd case.

SELECT c.contactname FROM Sales.Customers c
WHERE c.contactname LIKE '%a'-- Does a Scan 1st Case

SELECT c.contactname FROM Sales.Customers c
WHERE c.contactname LIKE 'a%'-- Does a Seek 2nd Case

SELECT c.contactname FROM Sales.Customers c
WHERE c.contactname LIKE '%a%'-- Does a Scan

If i build a index on contactname which has below sample data how would be the index tree.. like we if build for numbers it will compare less than greater than and will do traversing how index tree will be traverse in below case.

c.contactname

mark
anna
krishna
nadejda
allen
bob
cab
like image 639
Registered User Avatar asked Apr 18 '13 13:04

Registered User


People also ask

Does index work with like?

Indexes cannot be used with LIKE '%text%' predicates. They can, however with LIKE 'text%'.

How do I find a specific pattern in a column in SQL?

SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.

Does like use index in Oracle?

"When you use LIKE to search an indexed column for a pattern, Oracle can use the index to improve performance of a query if the leading character in the pattern is not % or _ . In this case, Oracle can scan the index by this leading character.

What is index is the index performed on a column or table?

An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the table or view . An index contains keys built from one or more columns in the table or view.


2 Answers

This is, indeed, the way that SQL Server works. You are asking why.

Think about the index. Functionally, you can think of it as storing the contactname in alphabetical order -- much like a dictionary or telephone book. (Yes, it is usually a more complicated data structure, typically a B-tree, but the result is the items in order).

When you say contactname like 'a%', then the query optimizer knows that it only needs to look at entries that start with the letter "a". The index knows exactly where those are, so the optimizer can use a seek to get to them. SQL Server implements this optimization for like (not all databases do this).

When you say contactname like '%a', you are saying "find me the entries that end in 'a'". That is a lot like looking through a dictionary to get all the words that end in "a". The ordering is not of any help. There might be entries that start with "a" and end with "a'. There might be entries that start with "z" and end with "a". So, these types of expressions require a scan instead of a seek.

like image 194
Gordon Linoff Avatar answered Sep 18 '22 21:09

Gordon Linoff


It does a scan in the first case for the same reason as in the third case: the index searches based on data from the start of the string. With a wildcard at the start of your pattern, there's no way to intelligently search the index for a match. The index does a lexographical (alphabetical) comparison for strings, which is a less than/greater than comparison.

The tree might look something like this:

        /nadejda
    mark
   /    \krishna
cab
   \    /bob
    anna
        \allen

So, searching for a% can be done, as the index will know where to go at each branch. E.g., C > A, so go left. Searching for %a isn't efficiently possible. The index would have to read all the data to figure out if each node ended with A. Having to read all the data means using the index is just wasted overhead.

like image 39
Esoteric Screen Name Avatar answered Sep 21 '22 21:09

Esoteric Screen Name