Chapter 5: Create a high-performance index (on)

The index is a data structure storage engine used to quickly find the record, this is the basic function of the index. In MySQL also called "key key". Ultimately good performance index. In other words, the index query performance can be optimized easily increase several orders of magnitude.

1. The index base

select first_name from actor where actor_id = 5;

Run the above query: if there is an index on actor_id column, MySQL will use the index to find actor_id 5 columns, that is to say: MySQL first look by value on the index, and then return the value of the row that contains data.

  • The most left-prefix column index may contain one or more columns, column order is very important, MySQL can only efficient use of the index

Type index of 1.1

MySQL indexes achieved by the storage layer, different implementations of storage engines

MySQL supports an index:

  • B-Tree index
    • B-Tree index B-Tree data structure used to store data
    • B InnoDB used + Tree index implemented
    • Storage engines by starting the search from the root of the index instead of full table scan in order to improve the speed of data access
    • B-Tree index organized sequentially stored in the column, so for the Look Data
    • You can use the query type B-Tree indexes:
      • Match and index all columns: All values ​​match
      • The most left-prefix match: match the index of the first column
      • Column prefix match: matches the beginning of a column value
      • Match value range
      • And a column match exactly match a range of columns: the first full match, matching the range of the second column
      • Access only indexed columns
    • B-Tree may be used in order by operation
    • There InnoDB adaptive hash index, can create a hash index on the basis of the B-Tree
    • B-Tree limits:
      • Find only applies to starting from the leftmost column index
      • Can not skip column index, such an index is three, only the first three are unable to find the
      • If the query has a column range queries, it can not be the right of all columns use the Index Tuning Find
  • Hash Index : Based on the hash table, only an exact match queries all indexed columns to be effective.
    • In some scenarios more suitable hash index performance is more obvious
    • The hash index in the index, while all of the hash table stored hash code storing pointers to data rows.
    • If the hash value is the same as the plurality of columns, the index will be stored in a linked list a plurality of records to the same hash entry to
    • Simply save the hash value corresponding to the space-saving, quick look
    • Hash restrictions
      • Value of the index can not be used to avoid reading lines (small effect on performance)
      • Not in accordance with the order of the index value can not be used to sort
      • It does not support partial matching index column
      • Only supports the equivalent comparison, can not find the range
      • Too many hash collisions result in increased index maintenance costs
    • Custom hash indexes: the Sham Hash based on B-Tree, when using a key lookup hash index lookup, using a custom hash function only after the where clause
  • Spatial Data Index
  • Full-text index
  • other

2. The advantage of index

  • It reduces the amount of data servers to be scanned
  • Avoid sorts and temporary tables
  • The random I / O becomes sequential I / O

Only storage engine index to help quickly locate records maintenance costs greater than the benefits paid index, the index is valuable

3. High performance indexing strategy

3.1 Independent column

"Independent columns": When the column can not be part of an expression index, nor is the parameter of the function.

3.2 prefix index and selectivity index

  • When the index is too long can index only part of the character to start, so you can save space and improve efficiency, but will reduce the selectivity index.

  • Selective Index: the ratio of the total will not be repeated recording data table index value and the number of T: from 1 / T 1 between
    • The higher the selectivity index, the higher the efficiency of the query, the higher the selectivity filter out MySQL nine more rows.
    • In general prefix selectivity of a column is high enough

More than 3.3 column index

Establishment of an independent separate indexes on multiple columns can not improve query performance of MySQL in most cases.

The combined index is an optimization strategy sometimes results when more often describes the index built on the table and worse:

  • When the server has multiple indexes do intersect operation (multiple and conditions), which implies the need for a multi-column index contains all the columns
  • When the server to do the combined operation occurs multiple index (or a plurality of conditions), generally requires a lot of CPU and memory need to merge on the cache algorithm.

If you see an index explain the merger, you should check queries and table structure to determine whether the hi already is optimal, or at some point you can turn off or ignore an index.

3.4 index column select the appropriate order

  • The correct order depends on the use to query the index, and also need to consider how to meet the needs sorting and grouping, from left to right order of the index column is always time to find.

  • Without considering selective sorting and grouping will be the highest at the forefront of normal good practice, but only limited where the query.

  • However, performance is not only dependent on the selectivity index of the column, and also related to the specific value query conditions, i.e. the distribution of values.

Guess you like

Origin www.cnblogs.com/mydailycoding/p/12393104.html