- Hash Index: faster, not support range comparision, only: =, <=>
- B-Tree Index: slower, support more comparison operator
=,
        >,
        >=,
        <,
        <=,
        or BETWEENUnderstanding the B-tree and hash data structures can help predict how different queries perform on different storage engines that use these data structures in their indexes, particularly for the
MEMORY storage engine
        that lets you choose B-tree or hash indexes.
B-Tree Index Characteristics
A B-tree index can be used for column comparisons in expressions that use the=,
        >,
        >=,
        <,
        <=,
        or BETWEEN operators. The index
        also can be used for LIKE
        comparisons if the argument to LIKE
        is a constant string that does not start with a wildcard
        character. For example, the following
        SELECT statements use indexes:
      SELECT * FROMIn the first statement, only rows withtbl_nameWHEREkey_colLIKE 'Patrick%'; SELECT * FROMtbl_nameWHEREkey_colLIKE 'Pat%_ck%';
'Patrick' <=
        key_col < 'Patricl' are
        considered. In the second statement, only rows with
        'Pat' <= key_col <
        'Pau' are considered.
      The following
SELECT statements
        do not use indexes:
      SELECT * FROMIn the first statement, thetbl_nameWHEREkey_colLIKE '%Patrick%'; SELECT * FROMtbl_nameWHEREkey_colLIKEother_col;
LIKE
        value begins with a wildcard character. In the second statement,
        the LIKE value is not a constant.
      If you use
... LIKE
        '%string%' and
        string is longer than three
        characters, MySQL uses the Turbo
        Boyer-Moore algorithm to initialize the pattern for
        the string and then uses this pattern to perform the search more
        quickly.
      A search using
col_name IS
        NULLcol_name is indexed.
      Any index that does not span all
AND levels in the
        WHERE clause is not used to optimize the
        query. In other words, to be able to use an index, a prefix of
        the index must be used in every AND
        group.
      The following
WHERE clauses use indexes:
      ... WHERETheseindex_part1=1 ANDindex_part2=2 ANDother_column=3 /*index= 1 ORindex= 2 */ ... WHEREindex=1 OR A=10 ANDindex=2 /* optimized like "index_part1='hello'" */ ... WHEREindex_part1='hello' ANDindex_part3=5 /* Can use index onindex1but not onindex2orindex3*/ ... WHEREindex1=1 ANDindex2=2 ORindex1=3 ANDindex3=3;
WHERE clauses do
        not use indexes:
      /*Sometimes MySQL does not use an index, even if one is available. One circumstance under which this occurs is when the optimizer estimates that using the index would require MySQL to access a very large percentage of the rows in the table. (In this case, a table scan is likely to be much faster because it requires fewer seeks.) However, if such a query usesindex_part1is not used */ ... WHEREindex_part2=1 ANDindex_part3=2 /* Index is not used in both parts of the WHERE clause */ ... WHEREindex=1 OR A=10 /* No index spans all rows */ ... WHEREindex_part1=1 ORindex_part2=10
LIMIT
        to retrieve only some of the rows, MySQL uses an index anyway,
        because it can much more quickly find the few rows to return in
        the result.
Hash Index Characteristics
Hash indexes have somewhat different characteristics from those just discussed:- 
            They are used only for equality comparisons that use the
            =or<=>operators (but are very fast). They are not used for comparison operators such as<that find a range of values. Systems that rely on this type of single-value lookup are known as “key-value stores”; to use MySQL for such applications, use hash indexes wherever possible.
- 
            The optimizer cannot use a hash index to speed up
            ORDER BYoperations. (This type of index cannot be used to search for the next entry in order.)
- 
            MySQL cannot determine approximately how many rows there are
            between two values (this is used by the range optimizer to
            decide which index to use). This may affect some queries if
            you change a MyISAMorInnoDBtable to a hash-indexedMEMORYtable.
- 
            Only whole keys can be used to search for a row. (With a
            B-tree index, any leftmost prefix of the key can be used to
            find rows.)
 
 
 Source: https://dev.mysql.com/doc/refman/5.5/en/index-btree-hash.html
 
Không có nhận xét nào:
Đăng nhận xét