Performance Optimization - under what circumstances, use the index

6, index optimization

1. What is the index?

The role of the index is equivalent to the book catalog, you can quickly find what you need based on the directory page.

Database using the index to find a specific value, then the clockwise locate the row containing the value. The establishment of the index in the table, then find the index values ​​match the query in the index, the last corresponding record in the table by ROWID (equivalent page) stored in the index to quickly find. Indexing is a table comparing a directional field, the equivalent of a directory, such as administrative area code, with a local administrative area code are the same, then this column to add the index to avoid repeating it scans, so achieve the purpose of optimizing!

2, how to create an index

You can create an index when the CREATE TABLE statement, you can also use CREATE INDEX or ALTER TABLE to add a separate index for the table.
1, ALTER TABLE

ALTER TABLE用来创建普通索引、UNIQUE索引或PRIMARY KEY索引。
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)

Description: where table_name is the name of the table to increase the index, column_list point out which column index, separated by commas between each column when multiple columns. Index name index_name optional, by default, MySQL will assign a name based on the first column of an index. In addition, ALTER TABLE allows you to change multiple tables in a single statement, so you can create multiple indexes at the same time.

2、CREATE INDEX

CREATE INDEX可对表增加普通索引或UNIQUE索引。
CREATE INDEX index_name ON table_name (column_list)
CREATE UNIQUE INDEX index_name ON table_name (column_list)

Description: table_name, index_name and column_list have the same meaning ALTER TABLE statement, the index name is not optional. In addition, you can not create PRIMARY KEY index with CREATE INDEX statement.

3, index type
when creating an index, you can specify whether the index contains duplicate values. If not, then the index should be created for the PRIMARY KEY or UNIQUE index. For single uniqueness of the index, which guarantees single does not contain duplicate values. For multi-column index uniqueness, to ensure combination of a plurality of unique values.

PRIMARY KEY index and the UNIQUE index is very similar.

In fact, PRIMARY KEY index is only the name of a UNIQUE index PRIMARY have. This means that a table can contain only one PRIMARY KEY, because a table index can not have two of the same name.
The following SQL statement against the students Table add PRIMARY KEY index on sid.

ALTER TABLE students ADD PRIMARY KEY (sid)

4, delete the index
can use ALTER TABLE or DROP INDEX statement to remove the index. Similar CREATE INDEX statement, DROP INDEX may be internal as an ALTER TABLE statement processing, syntax.

DROP INDEX index_name ON talbe_name
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY

Among them, the first two statements are equivalent, delete the index index_name table_name.
Article 3 statement is only used when deleting PRIMARY KEY index, because a table can have only one PRIMARY KEY index, there is no need to specify the index name. If you do not create PRIMARY KEY index, but the table has one or more UNIQUE indexes, MySQL will remove the first UNIQUE index.

If you delete a column from the table, the index will be affected. For multi-column index portfolio, if you delete a column in which, the column will be removed from the index. If you delete all columns of the index, the entire index will be deleted.

5, see the index

mysql> show index from tblname;

mysql> show keys from tblname;

6, under what circumstances, use the index?
1, the primary key table
2, automatically create unique index
3, the table only constraint field
4, field (for field condition constraint in SQL) query directly condition
5, the query associated with other fields in Table
6, sort of query field (if sort field to access through an index that will greatly improve the sorting speed)
7, the query statistics or statistical grouping fields
8, table records too few (if only five records in a table, using the index to access record, then it first needs to access the index table, then accessing the data table through the index table, the general index table and the data table is not the same data block)
in table 9, frequently insert, delete, modify (to some business for the regular process should minimizing index) in the case of query allows
10, data duplication and distribution field average (if a table has rows 100,000, there is a field a T and F only two kinds of values, and the probability distribution for each value of about 50%, then this table a field of construction index generally does not improve query speed of the database.)
11, and often the main field a query but the main index field value more tables Field
means 12, items indexed to ten million MySQL database and improve performance

Released 1079 original articles · won praise 888 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/103993922