MySQL-- index base index basis MySQL--

MySQL-- index base

 

In this article, we'll start with the basics index, explain what types of indexes and index, and then learn how to create an index and the basic principles of index design.

This article structure for the user to test the index table is created as follows:

mark

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

  5. 来源:http://songwenjie.cnblogs.com/

  6. 出处 : https://www.cnblogs.com/songwenjie/p/9410009.html

本篇文章,我们将从索引基础开始,介绍什么是索引以及索引的几种类型,然后学习如何创建索引以及索引设计的基本原则。

本篇文章中用于测试索引创建的user表的结构如下:

mark

什么是索引

索引(在 MySQL 中也叫“键key”)是存储引擎快速找到记录的一种数据结构

——《高性能MySQL》

我们需要知道索引其实是一种数据结构,其功能是帮助我们快速匹配查找到需要的数据行,是数据库性能优化最常用的工具之一。其作用相当于超市里的导购员、书本里的目录。

索引类型

可以使用SHOW INDEX FROM table_name;查看索引详情

mark

  1. 主键索引 PRIMARY KEY

    它是一种特殊的唯一索引,不允许有空值。一般是在建表的时候同时创建主键索引。

    注意:一个表只能有一个主键

    mark

  2. 唯一索引 UNIQUE

    唯一索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。

    可以通过ALTER TABLE table_name ADD UNIQUE (column);创建唯一索引

    mark

    mark

    可以通过ALTER TABLE table_name ADD UNIQUE (column1,column2);创建唯一组合索引

    mark

    mark

  3. 普通索引 INDEX

    最基本的索引,它没有任何限制。

    可以通过ALTER TABLE table_name ADD INDEX index_name (column);创建普通索引

    mark

    mark

  4. 组合索引 INDEX

    组合索引,即一个索引包含多个列。多用于避免回表查询。

    可以通过ALTER TABLE table_name ADD INDEX index_name(column1, column2, column3);创建组合索引

    mark

    mark

  5. 全文索引 FULLTEXT

    全文索引(也称全文检索)是目前搜索引擎使用的一种关键技术。

    可以通过ALTER TABLE table_name ADD FULLTEXT (column);创建全文索引

    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. Do not over-index. Index requires additional disk space and reduce the performance of write operations. Modify the contents of the table when the index is updated even reconstructed, the more the index column, this time will be longer. So keep the index requires only beneficial to query.

  5. Source: http://songwenjie.cnblogs.com/

  6. Source:  https://www.cnblogs.com/songwenjie/p/9410009.html

Guess you like

Origin www.cnblogs.com/hsz1124/p/11641546.html