mysql index knowledge summary

Index Type:

Ordinary Index: This index is the most basic type, no restrictions uniqueness and the like.

Unique index: general index and is basically the same, but all the indexed columns appear only once, remain unique.

Primary key: Primary key is a unique index, but it must be designated as the "PRIMARY KEY".

Full-text indexing: full-text index of the index type FULLTEXT. Full-text index can be created on columns of type VARCHAR or TEXT.

Index operation

Create an index using the ALTER TABLE statement.

alter table table_name add index index_name (column_list) ;

alter table table_name add unique (column_list) ;

alter table table_name add primary key (column_list) ;

Use the CREATE INDEX statement on the table to increase the index.

create index index_name on table_name (column_list) ;

create unique index index_name on table_name (column_list) ;

Delete Index

drop index index_name on table_name ;

alter table table_name drop index index_name ;

alter table table_name drop primary key ;

View Index

show index from table_name;

How to use index

1, in the field often connected to index

2, the column index in the sorting or grouping

3, as a result of the establishment (where) in a conditional expression, there are two cases

  Using an index value on many different columns,

  NA fewer different values ​​on the index column

4, a plurality of columns to be sorted, composite index can be established on the column

5, you can create a short index, for example:

   alter table table_name add index index_name(column_list(length))

   alter table test add index ab(`a`(3));

6, join column appears in the need for indexing

Precautions:

1, the index does not contain null values ​​in a column

2, MySQL query to use only one index where clause so if the index has been used, then order by the column will not use the index. Therefore data 

Do not use the default library sorting operation can meet the requirements of the case of the sort; try not to sort multiple columns contain, if required to best create a composite index of these columns.

3, do not be in operation on the column

4, beginning with wildcards% and _ for query, mysql will not use the index. E.g:

Will use an index: SELECT * FROM mytable WHERE username like 'admin%'

Not use an index: SELECT * FROM mytable WHERE username like '% admin'

Composite index

The most left-priority principle composite index, the first index is a combination of fields must appear in the query group sentence, the index will be used.

If there is a combination index (col_a, col_b, col_c)

The following situations will use this index:

col_a = "some value";

col_a = "some value" and col_b = "some value";

col_a = "some value" and col_b = "some value" and col_c = "some value";

col_b = "some value" and col_a = "some value" and col_c = "some value";

For the last statement, mysql will automatically optimize the way into Article III ~ ~.

The following situations will not use the index:

col_b = "aaaaaa";

col_b = "aaaa" and col_c = "cccccc";

Guess you like

Origin www.cnblogs.com/shangping/p/12158790.html