MySQL- index using the index and classification

1. What is the index?

Index: A data structure for storing engine quickly record found, the default B-Tree index. The index is the storage engine layer implementation.

Index purpose : to improve the efficiency of data queries, query performance optimization, just like the book directory.

 Common model indexes: hash table, ordered array, the search tree.

 InnoDB index Model: In InnoDB, primary key tables are sequentially placed in the form of an index, table of this storage is referred to as an index table organization. InnoDB using a B + Tree index model, the data are stored in a B + Tree tree.

 

Prerequisite: Suppose a table with 10W of data, data which is a nickname = 'css', this query data sql: select * from table_award where nickname = 'css'

No index: MySQL full table scan and scan 10W of data to find this data .

An index:  establish a nickname field index, mysql nickname only need to scan this data = 'css' of

 

1.1 Classification Index

The main index contains five parts: a primary index, a unique index, the general index, full-text index, the composite index, single-field index. Wherein the main index key automatically means that the main primary index.

 

1.2 Syntax Index

建立索引:
CREATE [UNIQUE|FULLTEXT]  INDEX index_name ON tbl_name (col_name [(length)] [ASC | DESC] , …..);

ALTER TABLE table_name ADD INDEX [index_name] (index_col_name,...)

 

删除索引
DROP INDEX index_name ON tbl_name;
ALTER TABLE table_name DROP INDEX index_name;

 

查询索引:
SHOW INDEX FROM table_name;

SHOW KEYS FROM table_name;
DESC table_Name;

 

 

Classification of the index, grammatical form of the text seems to have become numb, and can refer Photo: index classified as five parts, three kinds of syntax index operations (add, delete, query).

 

 

 

 

 

 1.3 Use Index

Improve performance optimization Index: coverage index, the most left-prefix index, which pushed down.

Covering indexes: You can reduce the number of searches of the tree, significantly improve query performance.

 

Use Index

MATCH (col1,col2,...) AGAINST (expr).

To a query using an index of the most important condition is that the query conditions require the use of an index.

It is possible to use the index in the following situations:
1. For multi-column indexes created, as long as the query using the leftmost column, the index will generally be used.
2, using queries like, if it is constant and only later% is not the first character, before the index can be used.
3, if column_name is null will use the index.

下列的表将不使用索引:
1,如果条件中有or,即使其中有条件有索引也不会使用。
2,对于多列索引,不是使用的第一部分,则不会使用索引。
3,like查询是以%开头
4,如果列类型是字符串,那一定要在条件中将数据使用引号引用起来。否则不使用索引。
5,如果mysql估计使用全表扫描要比使用索引快,则不使用索引。

 

1.4 查看索引的使用情况
show status like ‘Handler_read%’;
大家可以注意:
handler_read_key:这个值越高越好,越高表示使用索引查询到的次数。

handler_read_rnd_next:这个值越高,说明查询低效。

 

 B-Tree索引通常意味着所有的值都是按顺序存储的,并且每一个叶子页到根的距离相同。

Guess you like

Origin www.cnblogs.com/wendyw/p/11508998.html