Notes -6: mysql index

1. Overview Index

  The purpose of indexing: accelerate the speed of database searches.

  mysql index is divided into:

  • Ordinary Index: Use the keyword index or key is created, it can take the index column values ​​null or duplicate values.
  • Unique index: create UNIQUE keyword, its index column values ​​can not be repeated, and must be unique, but it can be null.
  • Primary key index: Use keywords to create primary key, primary key index is a unique index, but the column values ​​can not be null.
  • Clustered index: Index sequence clustered index is the physical order of the data stored. A table can have only one clustered index, and only solidDB and InnoDB storage engine is supported.
  • Full-text index: can only be created on a varchar or text column. And only supports the MyISAM storage engine.
  • Separate index: create an index on a particular column in a table can create multiple separate index.
  • Composite index: create an index on multiple columns of the table. For example, build an index on the table tb_class of the class 'in college' and 'Grade', this index is called a composite index. Index follows the most prefix rule. That is, first sorted by the first column, the first column when the values ​​are the same for the second row reordering.

2. View the data sheet on the index established

# Syntax: 
SHOW { the INDEX  | the INDEXES | KEYS} { the FROM  |  the IN } tb_name [ {the FROM | db_name the IN} ] 

# { | }: multiple selection 
# [  ] : Optional
# Display all information on the index table tb_score 
Show index  from tb_score \ G; 

# \ G: simplification

Index Results:

  • Table: indicate the name of the table where the index.
  • Non_name: The index is not a unique index, 1: no, 0: Yes
  • Key_name: name of the index. If you do not specify the name of the index when the index is created, the system automatically assigns a name index.
  • Column_name: the establishment of a column name index.
  • Collation: Description and in what order (ascending or descending) index. A: ASC, NULL: no classification.

3. Create Index

3.1 index create table statement creates an index

# Syntax:
 the Create  the Table tb_name [ col_name data_type ] 
[ CONSTRAINT index_name ]  [ UNIQUE ]  [ INDEX | KEY ] 
[ index_name ] (index_col_name [ length ] ) [ ASC | DESC ] 

# tb_name: indexing table 
# index_name: Specifies the index established name. 
# UNIQUE : Create a unique index. 
# Index_col_name: you want to create an index of the column name. 
# Length: Specifies the index is created using the first length character columns. 
# ASC  |  DESC : Specifies the index is based on ASC or DESC sort, the default is ASC.
# Create to create a new table, while the general index, the unique index
 the Create  the Table tb_score ( 
studentNo char ( 10 ) not  null  UNIQUE , # studentNo field to establish a unique index 
studentName VARCHAR ( 20 ) not  null , 
Sex char ( 2 ) not  null , 
Birthday DATE, 
Native VARCHAR ( 20 is ), 
Nation VARCHAR ( 10 ) default  ' Chinese ' , 
classno char( 6 ),
 index (studentName) # studentName field to establish a regular index 
);
Create a primary key index # create a new table while the
 Create  Table tb_score ( 
studentNo char ( 10 ), 
courseNo char ( . 5 ), 
Score a float ,
 constraint PK_score Primary  Key (studentNo, courseNo), # studentNo, courseNo fields are created primary key index
 constraint FK_score1 Foreign  Key (studentNo) References tb_student (studentNo),        
 constraint FK_score2 Foreign  Key (courseNo) References tb_course (courseNo) 
);

3.2 Creating an index using create index statement

# Syntax:
 Create  Table  [ UNIQUE ]  the INDEX index_name the ON tb_name ( col_name  [ (length) ]  [the ASC | DESC ] );
# Create a table on tb_student general index, the index field is studentNo
 the Create  index index_stu ON tb_student (studentNo); # index_stu: Index Name
Tb_course create an index on the table, requires the establishment of a descending index field value by course name courseName the first three characters.
Create  index index_course the ON tb_course (CourseName ( . 3 ) DESC ); 

# for sorting character types, English alphabetical order; Chinese sorted alphabetically in mysql corresponding pinyin.
# Establishment of Library categories (on tb_book ASC ) and title ( DESC composite index) of an index called index_book.
Create  index index_book the ON tb_book (bclassNo the ASC , bookName DESC );

3.3 Use alter table statement to create an index

# 语法格式:
alter table tb_name add [UNIQUE | FULLTEXT] [INDEX | KEY] [index_name] (col_name[length] [ASC | DESC])
# Use the ALTER the Table to create a common index statement
 the ALTER  the Table tb_student the ADD  INDEX idx_studentName (studentName);

4. Delete Index

4.1 Use drop index statement to remove the index

# Syntax:
 drop  index index_name ON tb_name 

# index_name: To delete the name of the index 
# tb_name: where the index table
# Delete the index idx_studentName on tb_student table.
drop  index idx_studentName the ON tb_student;

4.2 Use alter table statement to remove the index

# Syntax:
 the ALTER  the Table tb_name drop  index index_name
# Delete indexes on the table tb_student index_stu
 the ALTER  the Table tb_student drop  index index_stu;

5. Use the recommended index

  • Indexes can improve the efficiency of data query, but too much use of the index will affect the speed of data updates, improper use of the index will reduce system performance. Therefore avoid excessive index on frequent operational data table.
  • A small amount of data in the table is best not to index.
  • When using the composite index, the most left-prefix strictly follow the law.
  • Avoid creating an index on a different value is relatively small fields, such as: gender.

 

Guess you like

Origin www.cnblogs.com/Cyzhouke/p/11470371.html