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 | money ADD INDEX( username); |
Add an index to the username field of the money table |