mysql - index

advantage:

(1) by creating a unique index or primary key index, to ensure the uniqueness of each row of database table data

(2) greatly improves the efficiency of data retrieval, reducing the number of table rows retrieved

 

Disadvantages:

(1) when creating indexes and index maintenance would be time-consuming, as data volumes increase the time complexity increases

(2) the index file will take up physical memory

(3) When the data table of additions and deletions, so they will maintain the dynamic, and this will reduce the data rate to maintain

 

Category: separate index, a composite index

A separate index

(1) a column index contains only a

(2) there may be a plurality of separate index

(3) is divided into separate index: primary key index, unique index, the general index

Composite index

It may comprise a combination of two or more columns

 

Create a common index

Embodiment a 
create index on table index name (field names) 
Second way 
alter table add index table [index name using {btree | hash}] (field)
 

 For example: the first five characters in the Student ID column of the table stu students and the establishment of a kidney index stu_id

Embodiment a 
create index stu_id on stu (student number (5), asc); 
Second way 
alter table stu add index stu_id [using {btree | hash}] (school (5), asc);

  

Create a unique index, the general index of similar, must be unique, there can be null values

A way 
create unique index on the index table name (field names) 
Second way 
alter table [add unique table name using {btree | hash}] (field)

  

Create a primary key index must be unique, not null values 

A way to 
create a table of time to add 
Second way 
alter table [add primary key table name using {btree | hash}] (field names)

 

 

Composite index

The above statement, a plurality of field definition, with segmentation, only one index name

Embodiment a 
create index on table index name (field names, field names) 
Second way 
alter table [add index table using {btree | hash}] (field names, field names)

  E.g

Embodiment a 
create index fuhe_id on stu (student number, course number) 
Second way 
alter table stu add index [using {btree | hash}] (student number, course number)

  

View index Fact Sheet

show index from 表名;

 

Table creation process of creating the index:

# Normal create table 
create table [if not exists] table name ( 
       column name data type, 
       column name data type, 
       column name data type, 
       Primary key [using {hash | btree}] (column name, column name), 
       index [key [] using {hash | btree}] (column names, column names), 
       UNIQUE [key] [using {hash | btree}] (column names, column names),);          

  

Delete Index

The way a 
drop index index name on table name; 
Second way 
alter table table name drop primary key | drop index key index name foreign key foreign key name;

  

Guess you like

Origin www.cnblogs.com/ivyharding/p/11564769.html