zoukankan      html  css  js  c++  java
  • WHERE col1=val1 AND col2=val2;index exists on col1 and col2, the appropriate rows can be fetched directly

    http://www.jianblog.com/2006/07/10/70/

    Suppose that you issue the following SELECT statement:

    
    
    mysql> SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;

    If a multiple-column index exists on col1 and col2, the appropriate rows can be fetched directly. If separate single-column indexes exist on col1 and col2, the optimizer tries to find the most restrictive index by deciding which index finds fewer rows and using that index to fetch the rows.

    If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).

    MySQL cannot use a partial index if the columns do not form a leftmost prefix of the index. Suppose that you have the SELECT statements shown here:

    SELECT * FROM tbl_name WHERE col1=val1; 
    SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;  
    SELECT * FROM tbl_name WHERE col2=val2; 
    SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;

    If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

    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 * FROM tbl_name WHERE key_col LIKE 'Patrick%'; SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';

    In the first statement, only rows with 'Patrick' < = < 'Patricl' are considered. In the second statement, only rows with < 'Pau' are considered.The following SELECT statements do not use indexes: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.key_col < 'Patricl' are considered. In the second statement, only rows with 'Pat' < = key_col < 'Pau' are considered. < 'Pau' are considered.

    SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%'; SELECT * FROM tbl_name WHERE key_col LIKE other_col;

    In the first statement, the LIKE value begins with a wildcard character. In the second statement, the LIKE value is not a constant.

    MySQL 4.0 and later versions perform an additional LIKE optimization. If you use ... LIKE '%string%’ and

    A search using col_name IS NULL employs indexes if col_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:

    ... 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 index on index1 but not on index2 or index3 */ ... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3;

    These WHERE clauses do not use indexes:

    /* 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 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 uses 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 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.
    • The optimizer cannot use a hash index to speed up ORDER BY operations. (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 MyISAM table to a hash-indexed MEMORY table.
    • 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.)
  • 相关阅读:
    每日英语:Is Bo Xilai the Past or Future?
    每日英语:Cyclists Live Six Years Longer
    每日英语:No Consensus: China Debate on Women's Roles
    每日英语:How to Be a Better Conversationalist
    每日英语:Our Unique Obsession With Rover And Fluffy
    每日英语:For Michael Dell, Saving His Deal Is Just First Step
    每日英语:Secrets Of Effective Office Humor
    每日英语:China Underwhelmed After First Apple Event
    三分钟读懂TT猫分布式、微服务和集群之路
    Java基础精选,你答对了几道?
  • 原文地址:https://www.cnblogs.com/cy163/p/1327644.html
Copyright © 2011-2022 走看看