Mysql- indexing works and Detailed

Principle index

Advantages and disadvantages of using the principles and indexes

Index advantages:

You can accelerate the speed of data retrieval, improve query speed.
All MySql column type (field type) can be indexed, that is, any of the fields to be indexed.
Full text search field to search optimization.
Index Cons:

Creating indexes and index maintenance takes time, and with the increasing amount of data will increase the time it takes.
When the data in the table to add, delete, modify, and maintain dynamic indexes also need to reduce the speed of data maintenance.
Indexes also need to take up physical storage space (database directory: / var / lib / mysql) .
We know that the data in the data table will have the biggest on-line settings, if there are a large number of the index, the index file may reach values on the line faster than the data file.
Use principles:

The index is not possible, not every index like the fields are set, but needs its own rational use.
Regularly updated list of them to avoid too many indexes on a field often used to query the index should be created, not the data indexed constantly changing.
A small amount of data tables is best not to use the index, because less data, all data may query time even shorter than the time it takes to traverse the index, the index may not produce optimal performance.
In the column with a value less not to index (on the field), for example, only men and women of two different values on the "gender" student table fields. Instead, in a different field values more can be indexed.
Add an index when:

In the where, the field order by clause often used.
Field value is a plurality (e.g., not suitable gender field).
Field content is not constantly changing, constantly changing field, add an index but lower performance.
Not too much to add an index, an index will each add up disk space.
Category Index

Common Index: acceleration find
unique index: acceleration find + constraint (unique)
primary key index: acceleration find + constraint (not null and only)
foreign key index:
composite index: primary key (id, name) : the primary key index, unique ( id, name): co-unique index, index (id, name): joint general index
full-text indexing: when searching for a very long article, the best. (Full text)
space index: Learn enough, almost no. (Spatial)
general index (index)

Copy the code
usage rule
1. a table can have multiple index field
values of the field can be repeated 2, may be a null value. Field values unconstrained
3. often do the query criteria field to index field
key sign 4.index field is: mul

Create a regular index (three ways)
Method 1: Create index index When creating a table
the Create the Table Student (
the above mentioned id int,
name VARCHAR (25),
Score float (5,2),
index (name), # create a name index
index (score ) # create an index score
);
method 2: after you create a table to create the index index: index general and field names, as long as they recognize on the line, you can easily named.
mysql> create index index name on table name (field name)
mysql> create index name on Student (name);
Method 3: Create Index index create tables after
mysql> alter table table the Add index index name (field name);
MySQL> Table Student the Add index ALTER (name);
MySQL> Table Student the Add index ALTER name6 (name);

View the general index: key is an indexed column, we will find that there is value in the name and score MUL.
mysql> desc table;
mysql> desc Student; # Key flag: MUL
MySQL> Show index from table name; # Key_name value index name
mysql> show index from student \ G ; # If the field name too much, add a \ G.

Delete the general index: general index can only delete a delete a
mysql> drop index index name on table name;
mysql> drop index name6 on Student;
copy the code
unique index (unique)

Copy the code
usage rule
1. a table can have multiple unique field
value must be unique 2.unique field can be null null
Key UNI flag is 3.unique

Create unique index (index creating substantially equivalent)
Method 1: creating an index table creation
Create Table Student (
ID int,
name VARCHAR (25),
Score a float (5,2),
UNIQUE (name),
UNIQUE (Score)
);
Method 2: create an index after creating the table: general index and field names, as long as they recognize on the line, you can easily named.
mysql> create unique index index name on table name (field name);
Method 3: Create Create a table index after
MySQL> ALTER Table Student the Add UNIQUE (name);
MySQL> ALTER Table Student the Add UNIQUE name6 (name);
MySQL> ALTER Table the Add UNIQUE index Student (name);
MySQL> UNIQUE index ALTER Table Student name6 the Add (name);

View unique index
mysql> desc table;
MySQL> Show index from table name;

Delete unique index
mysql> drop index index name on table;

Guess you like

Origin blog.51cto.com/14549989/2439753