First, what is the index, why use an index?
1. The index is also called the "key" in MySQL storage engine is a data structure used to quickly find the record
2. index optimization should be the most effective means to optimize the performance of queries. Index can easily improve query performance by several orders of magnitude.
And on Chinese dictionary, we can follow the Pinyin, radical, strokes and other sort of table of contents (index), to quickly find the word need.
Second, the advantages and disadvantages of index
1. The biggest advantage is that greatly improve query efficiency
2. Disadvantages: If you want to make changes to the table, the database not only to save data changes, but also save index
Third, the index of classification
1. Ordinary Index: Create an index on any field
2. The only index, the index value must be unique requirements, allow null values
3. The primary key index, you must have a primary key when creating a table, the default data table has a primary key index, accelerated Find + constraint (unique and not empty)
4. The combination index, the index field is two or more
The full-text indexing, searching for a very long article, the best
Two Category Four, index
# When we can create the index, the index for the specified type, sub-categories
hash index types: single query speed, range query slow
btree types of indexes: b + tree, the more layers, the amount of data exponentially (we will use it, because innodb support it by default)
# Different storage engines support indexes are not the same type
InnoDB supports transactions, supports row-level locking, support for B-tree, Full-text indexing and so on, is not supported Hash index;
MyISAM does not support transactions, to support table-level locking, supports B-tree, Full-text indexing and so on, is not supported Hash index;
Memory does not support transactions, to support table-level locking, supports B-tree, Hash index, etc., does not support Full-text indexing;
NDB supports transactions, supports row-level locking, support Hash indexes do not support the B-tree, Full-text indexing and so on;
Archive does not support transactions, to support table-level locking, does not support the B-tree, Hash, Full- text indexing and so on;
V. index creation and deletion
1. Use the following table created
create index name (index name) on s1 (name); # add a normal index
create unique age (index name) on s1 (age); add a unique index
alter table s1 (table) add primary key (id); # add a primary key index, the id field is increased to a primary key constraint
create index name (index name) on s1 (id, name); # add a normal joint index
2. Delete Index
drop index id on s1; # delete the general index
drop index name on s1; # delete joint index
drop index age on s1; # delete unique index, and the general index, like, do not add unique to delete before the index, you can directly delete
alter table s1 drop primary key; # delete the primary key (as when it is added to increase according to alter, then we also used to alter deleted)
Sixth, the use of the index should pay attention!
1. The index is automatically called whenever you create the index, in the query, as long as the application to create an index of the field, the query
2. If your query is or so, if you or condition and promising to add an index field, the index failed
3. If your query is a string type, the value must be quoted
4. If your query is ! = , The general index fail
5. When the sort condition, if the field is not a query index field, the ordinary index is invalid
Seven, when to use an index?
(1) When you create an index, the index should ensure that applications in the SQL condition query
(2) creating an index, if blob and text type, must be specified length
(3) can create an index when you create a table, you can also create an index has been created on a good table
(4) a table is best not to create more than six index
(5) the amount of data exceeds 300 table should be indexed