Indexes are divided into: ordinary index (index), primary key index (primary key), full text index (Fulltext index), unique index (unique index)
How to create a primary key index/unique index?
1. Create when creating a table:
create table stu(
id int primary key,
name varchar unique (if unique is added, it defaults to a unique index)
)
2. Add after the table is created
alter table stu add primary key (id);
How to create ordinary index/unique index?
create index index_name (index name) on stu (field name);
create unique index index_name (index name) on stu (field name);
3. The full-text index can only be built in the database engine for myisam, which is less used
select * from stu where match (field name) against (word to be retrieved);
How to query the index?
show index from stu;
How to use the index?
select * from stu where id = 1; directly use the column containing the index as the condition
Why does index improve retrieval efficiency? The principle is as follows: mainly uses the binary tree algorithm
If the table containing the index is moved, we need to re-create the index! ! !
Of course, there are costs to using indexes.
First of all, it will take up computer disk space. If there are too many indexes, it will affect the speed of computer hardware.
The second is to reduce the efficiency of adding, deleting and modifying statements because the binary tree needs to be regenerated
How to check whether the index is used
You can use explain select * from stu where id = 1/G; that is, add an explain before the statement. If the key is not equal to null, the index is used