mysql index summary

Question 1: What is the difference between mysql index types normal, unique, and full text?

normal: Indicates a common index

. Unique: Indicates a unique index that does not allow duplicates. If the field information is guaranteed not to be repeated, for example, when the ID number is used as an index, it can be set to unique

full textl: Indicates an index for full-text search. FULLTEXT works best when searching for very long articles. It is used for relatively short text, if it is only one or two lines of words, ordinary INDEX can also be used.



Question 2: In the actual operation process, which fields in the table should be selected as indexes?

In order to make the use of the index more efficient, when creating an index, it must be considered on which fields to create an index and what type of index to create. There are seven principles:

1. Select a unique index
2. 3. Build indexes for fields that often require sorting, grouping, and union operations
. 4. Build indexes for fields that are often used as query conditions
. 5. Limit the number of indexes
. 6. Try to use indexes with a small amount of data
. Try to use prefixes to index
7. Deleting indexes that are no longer used or rarely used Btree


and Hash indexes in Mysql Btree and Hash index comparison, this article explains the B-Tree index features, Hash index features and other content, friends who need it can refer to the following



The most commonly used index structure in mysql is btree(O(log(n))), but there are always cases where we want to use other types of indexes for better performance. Hash is one of the options. For example, when we retrieve user id by user name, they are always in a one-to-one relationship, and the operator used is only =. If hash is used as an index data structure, the time complexity can be down to O(1). Unfortunately, in the current mysql version (5.6), hash only supports MEMORY and NDB engines, and our most commonly used INNODB and MYISAM do not support hash-type indexes.
In any case, it is still necessary to understand the difference between these two indexes. The following is translated from the explanation of the two in the mysql official website documentation.
B-Tree Index Features
B-Tree indexes can be used on comparison operators like =, >, >=, <, <= and BETWEEN. It can also be used with the LIKE operator, as long as its query condition is a constant that does not start with a wildcard. An index can be used in a statement like the following:
Copy the code as follows:

SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';
Indexes are not used in the following two cases: The
code to copy the code is as follows:

SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE other_col;
The first is because it starts with a wildcard, and the second is because no constants are used.
If you use... LIKE '%string%' and the string exceeds three characters, MYSQL uses the Turbo Boyer-Moore algorithm to initialize the query expression, and then use this expression to make the query faster.
A query such as col_name IS NULL can use an index on col_name.
Any index that does not cover all WHERE AND-level conditions will not be used. That is, to use an index, the first column in this index needs to be present in each AND group.
The following WHERE condition uses an index:
Copy the code as follows:

... 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
    /* optimize to "index_part1='hello'" */
... WHERE index_part1='hello' AND index_part3=5
    /* can use index1 but not index2 and index3 */
... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3;
the following WHERE condition will not use the index:
copy the code as follows:

    /* index_part1 is not used*/
... WHERE index_part2=1 AND index_part3=2
    /* index index does not appear in every where clause */
... WHERE index=1 OR A=10
    /* no index covers all columns */
... WHERE index_part1=1 OR index_part2=10
Sometimes mysql will not use the index, even if this is available. For example, when MySQL estimates that most of the row data will be read using the index. (In this case, a full table scan may be faster than using an index because it requires fewer retrievals). However, if the statement uses LIMIT to limit the number of rows returned, MySQL will use the index. Because it is more efficient to use an index when the number of result rows is small.
Hash Index Features
Hash-type indexes have some features that differ from those described above:
1. They can only be used for peer comparisons, such as the = and <=> operators (but much faster). They cannot be used in range query conditions like <. If the system only needs to use a storage structure like a "key-value pair", try to use a hash type index.
2. The optimizer cannot use the hash index to speed up the ORDER BY operator. (This type of index cannot be used to search the value of the next order)
3.mysql cannot determine how many pieces of data there are between two values ​​(this requires the use of a range query operator to decide which index to use). If you convert a MyISAM table to a MEMORY table that relies on a hash index, it may affect some statements (performance).
4. Only complete keys can be used to search a row of data. (If a B-tree index is used, any segment of a key can be used for lookups. I think it may mean that the LIKE operator with wildcards will not work).
postscript

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326594674&siteId=291194637
Recommended