The difference between btree and hash

Source: Internet
Author: User

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?
    1. SELECT * from tbl_name WHERE key_col like ' patrick% ';
    2. 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?
    1. SELECT * from tbl_name WHERE key_col like '%patrick% ';
    2. 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?
  1. ... WHERE index_part1=1 and index_part2=2 and other_column=3
  2. /* index = 1 OR index = 2 * /
  3. ... WHERE index=1 OR a=10 and index=2
  4. /* Optimized like "index_part1= ' Hello '" * /
  5. ... WHERE index_part1=' Hello ' and index_part3=5
  6. /* Can use the index on INDEX1 and not on index2 or index3 * /
  7. ...  WHERE index1=1 and index2=2 OR index1=3 and index3=3;


These WHERE clauses do not use indexes:

[SQL]View PlainCopyprint?
  1. /* Index_part1 is not used * /
  2. WHERE index_part2=1 and index_part3=2
  3. /* Index is not used in both parts of the WHERE clause * *
  4. WHERE index=1 OR a=10
  5. /* No index spans all rows * /
  6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.