10 MySQL index selection and use

Index Overview
    Each storage engine supports at least 16 for each table index, an index of total length of at least 256 bytes.
    MyISAM and InnoDB tables created by default BTREE index .MEMORY engine uses HASH indexes by default, but also supports BTREE
    MySQL does not currently support indexing function, but supports prefix index.
    MyISAM prefix index supports a maximum length of 1000 bytes; the InnoDB support prefix index is maximally 767.
    When CREATE TABLE create the index is measured according to the number of characters, so for multi-byte character sets, to consider the relationship between characters and bytes.
    MySQL also supports FULLTEXT (full-text) indexing for full-text search .5.0 version currently only MyISAM storage engine supports FULLTEXT index, and is limited to CHAR, VARCHAR, TEXT types of data columns.
    Indexes are always carried out on the entire line, it does not support partial (prefix) index.
 
    Create index syntax:
        CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
            [USING index_type]
        ON tbl_name (index_col_name,...) ;
 
    You can also use ALTER TABLE syntax increase in the index.  
          ALTER TABLE tbl_name ADD [ UNIQUE | FULLTEXT | SPATIAL ] INDEX idx_name index_col_name ;
 
 
        index_col_name :
            col_name[(length)] [ASC|DESC]
    
    Delete index syntax
        DROP INDEX index_name ON tbl_name ;
 
 
Index design principles:
 
    1) index search columns:
            WHERE clause now is pointed out in a column, or join clause specified column; instead the key columns SELECT.
    2) Use a unique index:
            The larger the base of the column index, the better the effect of such an index is stored birthday columns have different values, each line can be distinguished;. And deposited sex column, only 'F' or 'M', search out substantially fifty - fifty, meaning the index is not that big.
    3) Use a short index:
            If the string index, should specify a prefix length. For example there is a CHAR (200) column, if the first character 10-20, is the only multi-value, then do not index the entire column. Thus Disk I / O less operation, the cache block to accommodate more pairs.
    4) using the left-prefix: - todo explain this tm, authors can read your own thing?
            When you create an index n columns, is actually created MySQL available n indices. Multi-column index can play a role in multiple indexes, the index can be used because the leftmost column set to match the line. This column set is known as the most left-prefix.
    5) Do not overuse index,
            Too far. More, or a lot of unneeded index can result in disk IO. Modify the table will even reconstruct the index is updated. Will bring more work to the query optimizer. MySQL will lead to less use when querying the best of the index.
    6). For InnoDB table storage engine
            Save the order of the records:
                  Clear primary keys, in accordance with the order of the primary key;
                  But without a primary key has a unique index columns, column order by index;
                  No unique index, MySQL generates the internal column, according to the internal column sorting;
 
            Primary key / internal column access is the fastest so InnoDB possible - the primary key is specified.
            If there are several columns is unique, can be used as a primary key, select relatively short data type can reduce disk IO, as well as improve the index effect.
 
 
BTREE index and index HASH
    HASH index:
        = Only for use, which the operator <=> for equality comparison.
        HASH optimizer can not use the index to speed up ORDER BY operations.
        MySQL can not determine between two values ​​about the number of rows of data. If a MyISAM table instead of MEMORY HASH index table, will affect the efficiency of some queries.
        Only use keywords to search the entire line.
 
    BTREE Index:
        Oh is that when a>, <,> =, <=, BETWEEN,! =, <> Or LIKE 'pattern' (where the pattern does not begin with *), the index can be used on the associated column.
 
    When the scope of inquiry, HASH index, but it is still scanning the overall, while BTREE is the scope of the search.
    Can be observed using statements such as EXPLAIN: explain SELECT * FROM tbl_name WHERE ....;.
 

Guess you like

Origin www.cnblogs.com/lmxxlm-123/p/11131965.html