Mysql index
Advantages of index
Index seems to be a very lofty name. To put it bluntly, it is the latest table of contents of our book.
If you use Xinhua Dictionary to search for the Chinese character "张" without using the table of contents, you may have to go from the first page of Xinhua Dictionary to the last page, which may take two hours. The thicker the dictionary, the more time it will take you. Now you use the directory to search for the Chinese character "Zhang". The first letter of Zhang is z, and the Chinese characters starting with z start from more than 900 pages. With this clue, it may only take a minute for you to search for a Chinese character. This shows the importance of the index. sex.
Indexes are used to quickly find rows that have a specific value in a column.
Without using an index, MySQL must start from the first record and then read the entire table until it finds the relevant row. The larger the table, the more time it takes. If the queried column in the table has an index, MySQL can quickly get to a point where it searches the middle of the data file without having to look at all the data.
Of course, it is not easy to have too many indexes. The more indexes are written, the slower the modification speed. Because when writing modified data, the index must also be modified.
MySQL index type
Index type | Function description |
---|---|
Normal index | The most basic index, it has no restrictions |
Unique index | If a row uses a unique index, it is not allowed There are duplicate values in the row data for this column. Each row of data for this column is required to be unique |
Primary key index | It is a special unique index that does not allow null values. Generally, the primary key index is created at the same time when creating the table, which is often used for user ID. Similar to the page numbers in the book |
Full-text index | For data that requires global search, perform full-text index |
Detailed description | |
---|---|
alter table table add index(field) | |
ALTER TABLE | moneyADD INDEX( username); |
Add an index to the username field of the money table |
Full text index
Detailed description | |
---|---|
alter table table add UNIQUE(field) | |
ALTER TABLE | moneyADD UNIQUE(email );
|
Add a unique index to the email field of the money table |
Type | Detailed description |
---|---|
Basic syntax | alter table table add FULLTEXT(field) |
Example | ALTER TABLEmoney ADD FULLTEXT(content ); |
Example explanation | Add a unique index to the content field of the money table |
Primary key index
Type | Detailed description |
---|---|
alter table table add PRIMARY KEY(field) | |
ALTER TABLE | moneyADD PRIMARY KEY( id); |
Add a primary key index to the id field of the money table |
PRIMARY KEY (field)Note: The index name in brackets represents optional. The overall example is as follows:INDEX [index name] (field)
FULLTEXT [index name] (field)
UNIQUE[index name] (field)
CREATE TABLEtest
(
idINT NOT NULL ,
usernameVARCHAR(20) NOT NULL ,
passwordINT NOT NULL ,
contentINT NOT NULL ,
PRIMARY KEY (
id),
INDEX pw (
password),
UNIQUE (
username),
FULLTEXT (
content)
) ENGINE = InnoDB;