The index mysql

Reproduced in: https://www.cnblogs.com/songwenjie/p/9410009.html

What is the index

Index (In MySQL also called "key key") is a storage engine to quickly find the record of a data structure

- "High Performance MySQL"

We need to know the index is actually a data structure, its function is to help us quickly find the matching rows of data needed to optimize database performance is one of the most commonly used tools. Which acts as the supermarket Purchasing Guide, the books in the catalog.

Index Type

You can use SHOW INDEX FROM table_name;View Details Index

mark

  1. Primary key index PRIMARY KEY

    It is a special unique index, does not allow nulls. In general it is time to build the table at the same time create a primary key index.

    Note: A table can have one primary key

    mark

  2. Unique index UNIQUE

    The value of the unique index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique.

    You can ALTER TABLE table_name ADD UNIQUE (column);create a unique index

    mark

    mark

    By ALTER TABLE table_name ADD UNIQUE (column1,column2);creating a unique combination index

    mark

    mark

  3. General index INDEX

    The most basic index, it does not have any restrictions.

    You can ALTER TABLE table_name ADD INDEX index_name (column);create a regular index

    mark

    mark

  4. Composite index INDEX

    Combination index, i.e. an index containing a plurality of columns. Multi-table query to avoid back.

    By ALTER TABLE table_name ADD INDEX index_name(column1, column2, column3);index creation combination

    mark

    mark

  5. Full-text index FULLTEXT

    Full-text indexing (also known as full-text search) is a critical technology in the search engine.

    You can ALTER TABLE table_name ADD FULLTEXT (column);create a full-text index

    mark

    mark

Index can not be changed once it is created, if you want to modify the index, can only remove the reconstruction. It can be used DROP INDEX index_name ON table_name;to delete the index.

Principles of Design Index

  1. Suitable indexed columns appear in the where clause is a column, or columns specified in clause connected

  2. Smaller base class, the index less effective, there is no need in this column index

  3. Using short index, if the index of the long string column, should specify a prefix length, this can save a lot of space index

  4. 不要过度索引。索引需要额外的磁盘空间,并降低写操作的性能。在修改表内容的时候,索引会进行更新甚至重构,索引列越多,这个时间就会越长。所以只保持需要的索引有利于查询即可。

 

 

 

 

转载于:https://www.jianshu.com/p/29461f3e792b

InnoDB索引实现

虽然InnoDB也使用B+Tree作为索引结构,但具体实现方式却与MyISAM截然不同。

第一个重大区别是InnoDB的数据文件本身就是索引文件。从上文知道,MyISAM索引文件和数据文件是分离的,索引文件仅保存数据记录的地址。而在InnoDB中,表数据文件本身就是按B+Tree组织的一个索引结构,这棵树的叶结点data域保存了完整的数据记录。这个索引的key是数据表的主键,因此InnoDB表数据文件本身就是主索引。

 
image.png

上图为InnoDB主索引(同时也是数据文件)的示意图,可以看到叶结点包含了完整的数据记录。这种索引叫做聚集索引。因为InnoDB的数据文件本身要按主键聚集,所以InnoDB要求表必须有主键(MyISAM可以没有),如果没有显式指定,则MySQL系统会自动选择一个可以唯一标识数据记录的列作为主键,如果不存在这种列,则MySQL自动为InnoDB表生成一个隐含字段作为主键,这个字段长度为6个字节,类型为长整形。

第二个与MyISAM索引的不同是InnoDB的辅助索引data域存储相应记录主键的值而不是地址。换句话说,InnoDB的所有辅助索引都引用主键作为data域。例如,图11为定义在Col3上的一个辅助索引:

 
image.png

这里以英文字符的ASCII码作为比较准则。聚集索引这种实现方式使得按主键的搜索十分高效,但是辅助索引搜索需要检索两遍索引:首先检索辅助索引获得主键,然后用主键到主索引中检索获得记录。

Learn index different storage engine implementations for proper use and optimize indexes are very helpful, for example, know the InnoDB after the index to achieve, it is easy to see why not recommend the use of long field as the primary key, because all secondary indexes are the main reference index, long the main index will make secondary indexes become too large. As another example, a non-monotonic field as the primary key in InnoDB not a good idea because InnoDB data file itself is a B + Tree, non-monotonic primary key will result in the insertion of a new recording data file in order to maintain + Tree characteristic B and frequent split adjusted, very inefficient, and use auto-increment field as the primary key is a good choice.

Guess you like

Origin www.cnblogs.com/wsnan/p/11647152.html