MYSQL Index Use

Source: Internet
Author: User
Tags create index mysql index

Tian Teacher, le Study medical examination of the question statistics,

The record has 50w, statistics to more than 20 minutes

Optimized SQL and indexed, response in 0.1 seconds

SHOW INDEX from ' Exam_question_record ';

ALTER TABLE ' Exam_question_record ' ADD INDEX index_qid_status (' qst_id ', ' status ')

View index show index from ' Exam_question_record ';

1. Add primary key (primary key index)

Mysql>alter TABLE ' table_name ' ADD PRIMARY KEY (' column ')

2. Add unique (unique index)

Mysql>alter TABLE ' table_name ' ADD UNIQUE (' column ')

3. Add index (normal index)

Mysql>alter TABLE ' table_name ' ADD INDEX index_name (' column ')

4. Add fulltext (full-text index)

Mysql>alter TABLE ' table_name ' ADD fulltext (' column ')

5. Adding Multi-column indexes

mysql> ALTER TABLE ' table_name ' ADD INDEX index_name (' column1 ', ' column2 ', Column3 ')

MySQL: The index is saved in the B-tree format.

The memory storage engine can select either a hash or btree index, and the hash index can only be used for the equality comparison of = or <=>.

1. Normal index: CREATE index on Tablename (List of columns)

ALTER TABLE TableName Add index (List of columns)

CREATE TABLE TableName ([...], index [indexname] (List of columns)

2. Uniqueness Index: Create unique index

Alter ... add unique

Primary key: A unique index that must be specified as primary key

3, Full-text index: Starting from 3.23.23 Version Support full-text indexing and full-text search, Fulltext,

Can be created on a column of char, varchar, or text type.

4, single-row index, multi-column index:

Multiple single-column indexes differ from the query effect of a single multicolumn index because:

When executing a query, MySQL can use only one index, and one of the most restrictive indexes is selected from multiple indexes.

5, the leftmost prefix (leftmost prefixing): Multi-column index, for example: Fname_lname_age index, the following search conditions MySQL will use

Fname_lname_age index: Firstname,lastname,age;firstname,lastname;firstname, other conditions will not be used.

Ii. determine which type of index to create based on the SQL query statement and how to optimize the query

Select an indexed column:

A. In the performance optimization process, choosing which column to create an index on is one of the most important steps. The main factors that can be considered for using indexes are

Two types of columns: columns that appear in the WHERE clause, columns that appear in the join clause.

B. Consider the distribution of the values in the column, and the larger the cardinality of the indexed columns, the better the index works.

C. Using a short index, if you index a string column, you should specify a prefix length, which can save a lot of index space and improve query speed.

D. Using the leftmost prefix

E. Do not over-index, just keep the desired index. Each additional index consumes additional disk space and reduces the performance of the write operation.

When you modify the contents of a table, the index must be updated, and sometimes refactoring may be required, so the more indexes you have, the longer it takes.

MySQL uses indexes only on the operators: <,<=,=,>,>=,between,in,

And sometimes like (a case that does not begin with a wildcard% or _).

MySQL Index classification

In a database table, indexing a field can greatly improve query speed. By leveraging these indexes, you can make MySQL queries and operations more efficient. Indexing is the key to fast searching. MySQL indexing is important for the efficient operation of MySQL. Here are a few common types of MySQL indexes.

1, the ordinary type index

This is the most basic type of index, and it has no limitations such as uniqueness. Normal indexes can be created in the following ways:

(1) Create an index, such as the name of the CREATE INDEX index on tablename (column name 1, column name 2,...);

(2) Modify the table, for example ALTER TABLE tablename ADD index name (column name 1, column name 2,...);

(3) When creating a table, specify the index, such as CREATE TABLE TableName ([...], the name of the index (column name 1, column name

2,...) );

2. Unique index

This index is basically the same as the previous "normal index", but there is one difference: all the values of an indexed column can only occur once, that is, they must be unique. A unique index can be created in the following ways:

(1) Create an index, such as the name of the Create UNIQUE Index index on tablename (List of columns);

(2) Modify the table, such as the name of the ALTER TABLE TableName ADD UNIQUE Index (List of columns);

(3) When creating a table, specify the index, such as CREATE TABLE TableName ([...], the name of the UNIQUE index (column of column

table));

ALTER TABLE ' timetable '

ADD UNIQUE index_user_date_hour (' userId ', ' Date ', ' datehour ');

3. Primary key

The primary key is a unique index, but it must be specified as "PRIMARY key". If you've ever used a auto_increment type column, you're probably already familiar with concepts like the primary key. The primary key is typically specified when creating the table, such as "CREATE TABLE TableName ([...], PRIMARY KEY (List of columns)"; ”。 However, we can also add the primary key by modifying the table, such as "ALTER table tablename Add PRIMARY key (List of columns); ”。 There can be only one primary key per table. (The primary key is equivalent to the aggregate index, which is the fastest index to find)

4. Single-row index and multi-column index

The index can be a single-column index or a multicolumn index.

(1) A single-column index is the index of a commonly used field, a common index.

(2) Multiple-column indexes are indexes that contain multiple column fields

ALTER TABLE student Add index sy (name,age,score);

Index SY is a multicolumn index, and multi-column indexes are valid in the following cases:

SELECT * FROM student where name= ' Jia ' and age>= '//where conditions contain the first column field of the index and

A second field

SELECT * FROM student where name= ' Jia '//where conditions contain only first column fields

SELECT * FROM student where name= ' Jia ' and Score<60//where conditions contain first column field and third word

Paragraph

Summary: Multi-column indexes are valid only if the first column field in the index is included in the Where condition

5. Select an index column

How to choose the Index column, first of all to look at the query criteria, generally the query criteria in the column as an index


MYSQL Index Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.