mysql-- Index - concept

First, the index



Combination index database table or a plurality of columns formed, whose role is to speed up the search of the data table.

Index is created in the table above, the structure of a data table in a plurality of columns or sorting.


All the information through the index, query data can be read without having to record, but simply query the index column.


Index advantages: to improve the speed of data retrieval;

Index Cons: Create and maintain indexes takes time, the number of time-consuming with increasing amount of data increases; the index needs to take up physical space, each index to account for certain physical space,

          Adding, deleting, modifying data, to maintain the dynamic of the index, resulting in reduced maintenance speed data.


Note: The index can improve query speed, but will affect the speed of the inserted record. When the table has indexed record is inserted, the database will be sorted by the index, thus reducing the speed of the insertion recording,

       Speed ​​when inserting a large number of records is more significant. In this case, the best way is to delete the table index, and then insert the data. After insertion is completed, and then create the index.



Category Index


1 , the general index


2 , unique index


3 , full-text indexing


4 , a separate index


5 , multi-column index


6 , spatial index


==================================================================================================
Design Principles index

1 , select the unique index

2 , is often need sorting, grouping and joint field operations indexing

3 , often indexed as the field of query

4 , limit the number of indexes

5 , to make use of a small amount of data index

6 , try to use the prefix index

7 , remove unused or little-used index

===========================================================================================================

Creating an index

First, create a table when creating an index

Syntax is as follows:

create table table name (attribute name Data type [integrity constraints],
                     Attribute Name Data Type [integrity constraints],
                     ……
                     Property name Data Type
                     [unique | fulltext | spatial] index | key
                       [Alias] (attribute name 1 [(length)]) [ASC | desc])
                   );

unique is an optional parameter is the index of the unique index;

fulltext is an optional parameter indicates the index for the full-text index;

It is an optional spatial parameter index indicates the spatial index;

and a key index field is used to specify the parameters of the index, which can select one of the two, the effect is the same;

"Alias" is an optional parameter used to take the name of the new index is created;

Name of the field, "Attribute 1" corresponding to the specified index parameter, the field must be defined in front of the field;

"Length" is an optional parameter, an index which refers to the length of a string type can be used;

"Asc" and "desc" are optional parameters, "asc" indicates ascending sort, "desc" indicates descending order;


1 , to create a common index

Create a common index, you do not need to add any unique, fulltext, spatial parameters.

Example:
create table index1 ( id int,
                      name varchar(20),
                      sex varchar(20),
                      index index1_id (id)
                     );

show create table index1;

SELECT EXPLAIN * from index1, WHERE ID =. 1; / * The possible_keys: index1_id; KYE: index1_id; Description is referenced index * /



2 , create a unique index

When you create a unique index, it requires the use of unique parameter constraints.

Example: Create a table named table index2 establish unique index index2_id named in the table id field, and are arranged in ascending order.

create table index2 ( id int,
                      name varchar(20),
                      sex varchar(20),
                      unique index index2_id (id asc)
                     );

show create table index2;



3 , to create a full-text index

Full-text index can be created only on char, varchar, text types of fields, and only myisam storage engine supports full-text indexing.

create table index3 ( id  int,
                      info varchar(20),
                      fulltext index index3_info ( info )
                     )engine=myisam;

show create table index3;

4 , create a separate index

Create a separate index is an index on a field in a table

Subject field length is 20, and the length of the index only index4_st 10, the aim is to speed up the search.

For character data, you can not query all the information, but only query information in front of several characters.


create table index4 ( id  int,
                      subject varchar(30),
                      index index4_st ( subject(10) )
                     );


show create table index4;


5 , create multi-column index

Create multi-column index is to create an index on multiple fields of the table

create table index5 ( id  int,
                      name varchar(30),
                      sex varchar(4),
                      index index5_ns ( name,sex )
                     );


show create table index5;

As can be seen, the name and sex fields have established a separate index called the index5_ns.

When the multi-column index, only the query using these fields in the first field, the index will be used;

If the index in the first field is not used, then the multi-column index will not work.


6 , create a spatial index


When you create a spatial index, you must use the spatial parameters to set. Also, when you create a spatial index table storage engine must be myisam type. Moreover, the index field must have a non-null constraint.


create table index6 ( id int,
                      space geometry not null,
                      spatial index index6_sp ( space )
                    )engine=myisam;

show create table index6;


Note: space field must be non-empty, and the data type is the type of geometry. This type is a spatial data type. Spatial data types including: geometry, point, linestring, polygon types


==================================================================================================================================

Second, create an index on a table that already exists


In the table already exists, you can create an index directly into one or several fields on the table. Format is as follows:


the Create [UNIQUE | FULLTEXT | Spatial] index index name
                       
on the table name (attribute name [(length)]) [ASC | desc]);



unique is an optional parameter is the index of the unique index;

fulltext is an optional parameter indicates the index for the full-text index;

It is an optional spatial parameter index indicates the spatial index;

index field is used to specify the index;

"Index name" parameter is the new name of the index taken to create;

"Table" is the name of the table you need to create the index, the table must already exist, and if not, you need to create;

Field name "attribute name" parameter specifies the index corresponding to the front of the field must be defined fields;

"Length" is an optional parameter, an index which refers to the length of a string type can be used;

"Asc" and "desc" are optional parameters, "asc" indicates ascending sort, "desc" indicates descending order;


1 , to create a common index


create table example0 ( id int,
                        name varchar(20),
                        sex varchar(40)
                       );

show create table example0;


create index index7_id on example0( id );


2 , create a unique index

create unique index index8_id on index8( course_id );


3 , to create a full-text index

create fulltext index index9_info on index9( info );

4 , create a separate index

create index index10_addr on index10 ( address(4) );

5 , create multi-column index

create index index11_na on index11 ( name , address );

6 , create a spatial index

create spatial index index12_line on index12( line );


==================================================================================================

Third, with the alter table statement to create an index


On the table already exists, you can create an index by alter table statements directly into one or several fields on the table

Syntax is as follows:


alter table table the Add [UNIQUE | FULLTEXT | Spatial] index index name (attribute name [(length)]) [ASC | desc]);


1 , to create a common index

alter table example0 add index index13_name ( name );


2 , create a unique index

alter table index14 add unique index index14_id ( course_id );


3 , to create a full-text index

alter table index15 add fulltext index index15_info( info );


4 , create a separate index


alter table index16 add index index16_addr( address(4) );


5 , create multi-column index

alter table index17 add index index17_na( name,address );


6 , create a spatial index

alter table index18 add spatial index index18_line( line );


====================================================================================================================

Delete Index

Refers to the table already exists in the index removed.

Syntax is as follows:

drop index index name on table;

========================================================================================================================

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12097059.html