[Mysql] mysql index type normal, unique, full text

前言

Reprinted from: https://www.cnblogs.com/wmm123/p/8549512.html

内容

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

normal: indicates a normal index

unique: indicates a unique index and does not allow duplicate indexes. 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 the index for full-text search. FULLTEXT works best when searching for a long article. Used in relatively short text, if it is only one or two lines, ordinary INDEX can also be used.

In summary, the type of index is determined by the content characteristics of the indexed field, and normal is usually the most common.

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

In order to make the use of indexes more efficient, when creating an index, you must consider which fields to create an index on and what type of index to create. There are 7 principles:

1. Select the unique index
2. Create indexes for fields that often require sorting, grouping, and joint operations
3. Create an index for the fields that are often used as query conditions4
. Limit the number of indexes
5. Try to use indexes with less data
. Try to use prefixes to index
7. Delete indexes that are no longer used or rarely used

1. MySQL: Index is saved in B-tree format

Memory storage engine can choose Hash or BTree index. Hash index can only be used for equality comparison of = or <=>.

1. Ordinary index: create index on Tablename (list of columns)

alter table TableName add index (list of columns)

create table TableName([…], index [IndexName] (list of columns)

2. Unique index: create unique index

alter … add unique

Primary key: a unique index, must be designated as the primary key

3. Full-text index: Full-text index and full-text search are supported starting from version 3.23.23, FULLTEXT,

Can be created on char, varchar or text type columns.

4. Single-column index and multi-column index:

The query effect of multiple single-column indexes is different from that of a single multi-column index because:

When executing a query, MySQL can only use one index, and will select the most restrictive index from multiple indexes.

5. Leftmost Prefixing: Multi-column index, for example: fname_lname_age index, the following search conditions will be used by MySQL

fname_lname_age index: firstname, lastname, age; firstname, lastname; firstname, otherwise it will not be used.

2. Determine which type of index to create according to the sql query statement and how to optimize the query

Select index column:

a. In the process of performance optimization, choosing which column to create an index on is one of the most important steps. The main considerations for using indexes are

Two types of columns: the columns that appear in the where clause, and the columns that appear in the join clause.

b. Consider the distribution of values ​​in the column. The greater the cardinality of the indexed column, the better the index.

c. Use a short index. If you index a string column, you should specify a prefix length, which can save a lot of index space and improve query speed.

d. Use the leftmost prefix

e. Don't over-index, only keep the required indexes. Each additional index takes up additional disk space and reduces the performance of write operations.

When modifying the contents of the table, the index must be updated, and sometimes it may need to be reconstructed. Therefore, the more indexes, the longer it takes.

MySQL only uses indexes for the following operators: <,<=,=,>,>=,between,in,

And sometimes like (not starting with wildcard% or _).

MySQL index classification

In the database table, indexing the fields can greatly improve the query speed. By making good use of these indexes, MySQL queries and operations can be made more efficient. Index is the key to fast search. The establishment of MySQL index is very important for the efficient operation of MySQL. Here are several common MySQL index types.
1. Ordinary index
This is the most basic index type, and it has no restrictions such as uniqueness. Ordinary indexes can be created in the following ways:
(1) Create an index, such as the name of the CREATE INDEX index ON tablename (column name 1, column name 2,...);
(2) Modify the table, such as ALTER TABLE tablename ADD INDEX index Name (column name 1, column name 2,...);
(3) Specify the index when creating the table, such as CREATE TABLE tablename ([…], INDEX index name (column name 1, column name
2,…) );
2 , Unique index
This index is basically the same as the previous "ordinary index", but there is one difference: all values ​​of the index column can only appear once, that is, it must be unique. Unique indexes can be created in the following ways:
(1) Create an index, such as CREATE UNIQUE INDEX index name ON tablename (column list);
(2) Modify the table, such as ALTER TABLE tablename ADD UNIQUE index name (column List);
(3) Specify the index when creating the table, such as CREATE TABLE tablename ([…], UNIQUE index name (column
list));
3. Primary key
The primary key is a unique index, but it must be specified as "PRIMARY KEY". If you have ever used AUTO_INCREMENT type columns, you may already be familiar with concepts such as primary keys. The primary key is generally specified when the table is created, such as "CREATE TABLE tablename ([…], PRIMARY KEY (list of columns) ); ". However, we can also add the primary key by modifying the table, such as "ALTER TABLE tablename ADD PRIMARY KEY (column list);". Each table can only have one primary key. (The primary key is equivalent to the aggregate index, which is the fastest index to find)
4. Single-column index and multi-column index The
index can be a single-column index or a multi-column index.
(1) A single-column index is a commonly used index of a column field, a common index.
(2) A multi-column index is an index containing multiple column fields
alter table student add index sy(name, age, score);
index sy is a multi-column index, and a multi-column index can only be effective in the following situations:
select * from student where name='jia' and age>='12' //where condition contains the first column and
second column of the index
select * from student where name='jia' //where condition contains only the first column Field
select * from student where name='jia' and score<60//where condition contains the first column field and the third
field.
Summary: Multi-column index is only valid when the where condition contains the first column field in the index
5. Select the index column
How to choose the index column, first of all depends on the query condition, generally the column in the query condition is used as the index

Guess you like

Origin blog.csdn.net/s1441101265/article/details/114627701