index details

index details

 

what is an index

An index is a way to speed up the retrieval of data in a table, just so that you don't want to scan the entire table.

 

 

The advantages of indexing

 

1. Greatly speed up finding data

2. Establish a unique index to ensure the uniqueness of each row of data in the data table

3. When performing grouping and sorting retrieval, the time can be significantly reduced

 

 

Disadvantages of Indexing

 

1. Reduce the speed of addition, deletion and modification

2. Creating and maintaining indexes requires our maintenance time

3. The index still needs to occupy a certain amount of physical space

 

 

Classification of indexes

 

By index method

 

  • B-Tree Index
  • Hash index

By index type

 

1. Ordinary index

Ordinary creation:

CREATE INDEX indexName ON mytable(username(length));

Modify table creation:

ALTER mytable ADD INDEX [indexName] ON (username(length)) 

Created when the table is created

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   INDEX [indexName] (username(length))   ); 

 

 

2. Unique Index

Guarantees uniqueness, but allows nulls

CREATE UNIQUE INDEX indexName ON mytable(username(length)) 

 

 

3. Primary key index

Guaranteed uniqueness, no null values ​​are allowed, as the primary key, sometimes we will add auto-increment

CREATE TABLE mytable(   ID INT NOT NULL,    username VARCHAR(16) NOT NULL,   PRIMARY KEY(ID)   );

 

 

4. Full-text indexing

For full-text search, select Full Text as the index type

 

 

5. Composite Index

In order to further squeeze the efficiency of MySQL, it is necessary to consider building a composite index

ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age);

 

Establishing such a composite index is actually equivalent to establishing the following three sets of composite indexes:

username,city,age username,city username Why is there no combined index like city and age? This is because of the "leftmost prefix" result of the MySQL composite index. The simple understanding is to only start the combination from the leftmost. Not every query that includes these three columns will use the composite index,

 

 

Another classification of indexes

 

 1. Direct index creation and indirect index creation

Create the index directly: CREATE INDEX mycolumn_index ON mytable (myclumn)

Create an index indirectly: define a primary key constraint or a unique key constraint, you can create an index indirectly

 

 

2. Ordinary and unique indexes

Ordinary index: CREATE INDEX mycolumn_index ON mytable (myclumn)

Unique index: guarantees that all data in the index column is unique, and can be used for both clustered and non-clustered indexes

CREATE UNIQUE COUSTERED INDEX myclumn_cindex ON mytable(mycolumn)

 

 

3. Single Index and Combined Index

Single index: that is, a non-compound index

Composite index: also known as composite index, contains multiple field names in the index creation statement at the same time, up to 16 fields

CREATE INDEX name_index ON username(firstname,lastname)

 

 

4. Clustered index and non-clustered index (clustered index, clustered index)

Clustered index: Physical index, the same as the physical order of the base table, the order of data values ​​is always in order

CREATE CLUSTERED INDEX mycolumn_cindex ON mytable(mycolumn) WITH ALLOW_DUP_ROW (clustered index that allows duplicate records)

Nonclustered index: CREATE UNCLUSTERED INDEX mycolumn_cindex ON mytable(mycolumn)

 

 

When to use an index

 

1. Add indexes to the fields used by where or join

Note that MySQL only uses indexes for <, <=, =, >, >=, BETWEEN, IN, and sometimes LIKE (you cannot use % in front of the keyword when like)

The index is valid:

SELECT * FROM mytable WHERE username like'admin%'

Invalid index:

SELECT * FROM mytable WHEREt Name like'%admin'

 

 

2. Indexing is not recommended

1) Repeated and evenly distributed fields, such as sex, only male and female, do not need to be indexed

2) There is too little data, if there are only a few rows of data, do not index

3) If you frequently insert and modify data, it is not recommended to add an index

 

 

 

3.  When the update frequency of field data is low, the query frequency is high and there are a large number of duplicate values, it is recommended to use a clustered index

 

4.  Frequently accessing multiple columns at the same time, and each column contains duplicate values, consider building a composite index

 

 

refer to:

http://www.jb51.net/article/49346.htm

http://blog.sina.com.cn/s/blog_6a6eb42d0100kmyz.html

http://blog.csdn.net/xyh94233/article/details/6935669

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326593392&siteId=291194637