The understanding of b-tree and hash data structures can help predict differences in query performance using different indexes under different storage engines, especially those that allow you to select B-tree or hash indexes.
Features of the B-tree index
The B-tree index can be used for column comparisons using =,;, >=, <, <=, or between operators. This index can also be used if the like parameter is a constant string that does not start with a wildcard character. For example, the following SELECT statement uses the index:
[SQL]View PlainCopyprint?
- SELECT * from tbl_name WHERE key_col like ' patrick% ';
- SELECT * from tbl_name WHERE key_col like ' pat%_ck% ';
In the first sentence, only the records of ' Patrick ' <= key_col < ' patricl ' are considered. In the second sentence, only the records of ' Pat ' <= key_col < ' Pau ' are considered.
The following SELECT statement does not use an index:
[SQL]View PlainCopyprint?
- SELECT * from tbl_name WHERE key_col like '%patrick% ';
- SELECT * from tbl_name WHERE key_col like other_col;
In the first sentence, the value of like starts with a wildcard character. In the second sentence, the value of like is not a constant.
If you use this: ... Like '%string% ', where the string is no greater than three characters, MYSQL uses the Turbo Boyer-moore algorithm to initialize the string expression and use this expression to make the query faster.
If the Col_name column creates an index, a query that uses col_name is NULL can use that index.
Any index that does not cover the conditions of all and levels in the WHERE clause will not be used. In other words, in order to use an index, the leading column of the index must be used in each and combination.
The following WHERE clause uses the index:
[SQL]View PlainCopyprint?
- ... WHERE index_part1=1 and index_part2=2 and other_column=3
- /* index = 1 OR index = 2 * /
- ... WHERE index=1 OR a=10 and index=2
- /* Optimized like "index_part1= ' Hello '" * /
- ... WHERE index_part1=' Hello ' and index_part3=5
- /* Can use the index on INDEX1 and not on index2 or index3 * /
- ... WHERE index1=1 and index2=2 OR index1=3 and index3=3;
These WHERE clauses do not use indexes:
[SQL]View PlainCopyprint?
- /* Index_part1 is not used * /
- WHERE index_part2=1 and index_part3=2
- /* Index is not used in both parts of the WHERE clause * *
- WHERE index=1 OR a=10
- /* No index spans all rows * /
- WHERE index_part1=1 OR index_part2=10
Sometimes, even if an index is available, MySQL does not use any indexes. One of the scenarios where this happens is that the optimizer estimates that using this index will require MYSQL to access most of the records in this table. In this case, a table scan might be faster because it requires a smaller number of queries. However, if such a query uses LIMIT to retrieve only a small number of records, MYSQL will still use the index because it can find this record more quickly and return it.
Features of the Hash index
The Hash index has distinct characteristics compared to the features discussed just now:
- The Hash index can only be used for equality comparisons with = or <=> operators (but faster). The Hash index cannot be used for comparison operators such as < for finding a range value. Systems that rely on this single-value lookup are referred to as "key-value stores" and, for such systems, use hash indexes whenever possible.
- The optimizer is not able to use a hash index to speed up the ORDER by operation. This type of index cannot be used to find the next entry in order.
- MYSQL cannot use a hash index to estimate how many rows are between two values (this is the case by the Range optimizer to decide which index to use). If you convert a MyISAM or InnoDB table into a hash-indexed memory table, some queries may be affected by this.
- Finding a row of records must match all keys. In the B-tree index, any left prefix of the key can be used to find records.
Original link: http://dev.mysql.com/doc/refman/5.5/en/index-btree-hash.html.
The difference between btree and hash