MYSQL study notes eight (index)

First, the concept index
1.1 concept
in MySQL, keyword index for the index, also known as "key (key), is a storage engine for a data structure to quickly find the record.
When the amount of data in the table more and more, index for good performance is critical index optimization should be the most effective means to optimize query performance, to create a truly optimal index often need to rewrite the SQL query.
Note: the index is generally created in the query field often.

Works 1.2 index
to understand how MySQL index, the easiest way is to look at the index part of a book: for example, you want to find a topic in a book, usually the first reading of the index directory , find the corresponding chapter, after the corresponding page number can quickly find what you want to see.

In MySQL storage engine using a method similar to the index, which is to find the corresponding values ​​in the index, and then find the corresponding data line based on the index matching records, and finally returns the result set of data to the client.

1.3 index type of
the base type mysql index of four, which are the primary key index, the only index, the general index and full-text indexing.
Primary key index:
primary key is a unique index, but it must be specified as PRIMARY KEY, each table can have only one primary key.
The only index:
all values of the indexed column can only occur once, that must be unique, value can be empty.
Common Index:
basic index type, value can be empty, without limitation uniqueness.
Full-text indexing:
full-text index of the index type FULLTEXT. Full-text index can be created on varchar, char, text type columns. Can be created by ALTER TABLE or CREATE INDEX command. For large data sets, through the ALTER TABLE (or CREATE INDEX) command to create full-text index than the record into an empty table with a full-text index faster. Support for full-text indexing MyISAM, InnoDB after mysql5.6 supports full-text indexing.
Full-text indexes do not support the Chinese need to borrow sphinx (coreseek) or fast search <, code> Chinese processing technology.

Second, the index creation
# create a test table, creating a script table is as follows:
the CREATE TABLE DATAl
(
ID the INT,
studentNumber NVARCHAR (30),
subjectName NVARCHAR (30),
Score the INT
)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
2.1 create a primary key index
created primary key index syntax structure as follows:

TABLE the ALTER table_namethe ADD PRIMARY KEY ( column)
1
EG: data1 create a primary key index as the ID of the script statement is as follows:

The ADD PRIMARY KEY TABLE data1 the ALTER (ID)
1
2.2 creates a unique index
to create a unique index grammatical structure is as follows:

ALTER TABLE table_name ADD UNIQUE (
column`
)

eg: data1 create a unique index as studentNumber is, the script statement is as follows:

ALTER TABLE data1 ADD UNIQUE (
studentNumber
)

2.3 Create a regular index
to create a common index of grammatical structure is as follows:

TABLE the ALTER table_namethe ADD INDEX index_name ( column)
1
EG: data1 create a regular index as subjectName is, the script statement is as follows:

TABLE data1 the ADD INDEX index_name the ALTER (subjectName)
1
2.4 to create a full-text index
create full-text index of the grammatical structure is as follows:

TABLE the ALTER table_namethe ADD FULLTEXT ( column)
1
EG: data1 create a full-text index as subjectName is, the script statement is as follows:

The ADD FULLTEXT INDEX TABLE data1 the ALTER fulltext_123 (studentNumber, subjectName);
1
Note that the above script execution time, and sometimes the following error message: The
MYSQL study notes eight (index)
reported this error is because they do not support full-text indexing, the solution:

1 to see which engine used to create a table, and if InnoDB, instead MyISAM, InnoDB FULLTEXT index does not support the type
2, view the configuration file mysql.ini, search default-storage-engine =, if it is InnoDB, instead MyISAM. Restart MySQL service.

Third, the index view
index query in two ways, are as follows:

Method One:
Show index from tblname;
MYSQL study notes eight (index)
Method two:
Show Keys from tblname;
MYSQL study notes eight (index)
Fourth, 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

Guess you like

Origin blog.51cto.com/14525650/2436589