MySQL SQL tuning of the index

Benpian record of MySQL index knowledge learning notes, also easy to look after their own review

First, the concept of index
MySQL official gives the index definition: Index (Index) to help MySQL efficiently get the data structure of the data. So the index is to quickly find the data structure sorted

Second, the classification of the index
MySQL indexes can be divided into categories:

Single-valued index: Index is only a single value index of the column contains a
unique index: index column requires a unique index must be unique, unique or primary key index for example, but still allows null values
composite index: Index is a composite comprising a plurality of columns index composed of
three index usage
to create an index
create [unique] index indexName on tableName (columnName (length));
such as creating an index idx_dept_id dept table to
create idx_dept_id on dept (id);
modify the index
ps: modify the index can also be used for new index increase
syntax:

alter tablename add [unique] index indexname on columnName (length);
delete the index
drop index indexname on tablename;
see the index
show index from tablename;
if you want to display line breaks can add \ G, but you can not add a semicolon
show index from tablename \ G
Appendix:
methods from the new index is still Silicon Valley teacher summary of:
# add a primary key, the primary key is unique and can not Null to
the ALTER the Table TableName the Add pRIMARY kEY (columnName);
# create a unique index, requiring the index unique, but allow allow nulls
alter table tablename the Add uNIQUE index IndexName ON (columnName);
# Create a common index, the index data may not be unique
alter table tablename index IndexName the Add ON (columnName);
# specify the FullText index, a full-text index
alter table tablename add FULLTEXT indexname on (columnName);
Fourth, the index architecture Introduction
MySQL indexing structure can be divided into:

BTree index
Hash index
full-text indexing text
R-Tree index

It can be seen from FIG BTree index structure is actually a B + tree, a binary tree is not necessarily, there may be more than one tree, MySQL segment block is divided into such a structure, as shown, where the blue is a disk block, data items that represent the blue portion, while the yellow portion represents a pointer

If I want to find 29 this value is to walk down, respectively, to find disk block 1, the disk block 3, disk blocks 8, according to the pointer look down, very fast, if there are millions of data, it can take the case of the index, is very fast, performance comparison can be imagined

Note: Only leaf nodes (bottom node) is stored to look for data, the data stored in the non-leaf node is only used for data entry pointer of the index it

Fifth, the applicable index case
1, the conditions for frequent queries for index
2, the default primary key index to add Primary
3, and other tables have foreign key relationships indexed column
4, column behind the condition where conditions can be built on index
5, the column can be used to sort order by adding the index
6 for grouping group by columns can be indexed and
attention: the need for frequent changes of the column is not suitable for indexing, as updated data will also rebuild the index, Comparative resistance performance; in the case of high concurrent, tend to establish a composite index because, in general, there is little a query condition, usually a plurality of conditions, a composite index is more suitable

VI where the index does not apply
1, requires frequent additions and deletions in Table
case 2, the table is recorded a few, little effect index plus
3, such as gender of the user information table if a data column contains many duplicate data this column generally only two cases, so the addition of the index, is not much practical effect
Note: the index should be added to the regular query or sort columns, data duplication and distribution of very average case is not suitable indexed

 

Programmer tools site: grassroots tool www.idevtool.com  

Personal Notes site: grassroots notes note.idevtool.com

Guess you like

Origin www.cnblogs.com/benpao/p/11600535.html