Using indexes in database tables can greatly improve the query speed. Suppose we have created a testindex table: createtabletestindex (I _testidintnotnull, vc_namevarchar (16) notnull). We randomly inserted 1000 records into the table, one of which is I _testidvc_name555erquan.
Using indexes in database tables can greatly improve the query speed. Suppose we have created a testindex table: create table testindex (I _testid int not null, vc_name varchar (16) not null); we randomly inserted 1000 records into it, one I _testid vc_name 555 erquan is in the query for vc_name = "e
Using indexes in database tables can greatly improve the query speed.
Suppose we have created a testindex table:
Create table testindex (I _testid int not null, vc_name varchar (16) not null );
We randomly inserted 1000 records into it, one of which is
I _testid vc_name
555 erquan
Find the record of vc_name = "erquan"
Select * from testindex where vc_name = 'erquanc ';
If an index has been created on vc_name, mysql can find this record accurately without any scanning! On the contrary, mysql scans all records, that is, it needs to query 1000 times ~~ Indexes increase the query speed by 100 times.
I. index is divided into single-column indexes and composite indexes
Single-Column index: an index only contains a single column. A table can have multiple single-column indexes, but this is not a combination index.
Composite Index: A cable contains multiple columns.
Ii. Introduce the index type
1. Common indexes.
This is the most basic index with no restrictions. It can be created in the following ways:
(1) create an index: create index indexname on tablename (tablecolumns (length); for char and varchar types, the length can be smaller than the actual length of the field; For blob and text types, length must be specified, the same below.
(2) modify the table structure: alter tablename add index [indexname] on (tablecolumns (length ))
(3) when creating a table, specify: create table tablename ([...], index [indexname] (tablecolumns (length ));
2. Unique index.
It is similar to the previous "normal index". The difference is that the value of the index column must be unique, but null values are allowed. If it is a composite index, the combination of column values must be unique. It can be created in the following ways:
(1) create an index: create unique index indexname on tablename (tablecolumns (length ))
(2) modify the table structure: alter tablename add unique [indexname] on (tablecolumns (length ))
(3) when creating a table, specify: create table tablename ([...], unique [indexname] (tablecolumns (length ));
3. Primary Key Index
It is a special unique index and does not allow null values. Create table testindex (I _testid int not null auto_increment, vc_name varchar (16) not null, primary key (I _testid ));
Of course, you can also use the alter command.
Remember: A table can only have one primary key.
4. Full-text index
Mysql supports full-text indexing and full-text retrieval from version 3.23.23. I will not discuss it here ~~
Syntax for deleting an index: drop index index_name on tablename