Index of the SQL language
Foreword
In this chapter we will learn aspects of MySQL index, this article from the index role, the index classification, index creation syntax, the index strategy and realization of the principle of indexing, etc. take you to find the index.
The role of the index
The role of the index is to speed up queries, if the use of the index query seen as a Ferrari sports car, then do not use query index is equivalent to a bicycle. The current amount of data actually growing item table, the hundreds of millions of millions level, there is no index will become very slow query, use the index query optimization has become a must have item.
The concept of index
In fact, the index is a special kind of data is also stored in the database file, index data stored in the position data table of the actual data. Similar to the previous book catalog, this directory will save the pages of each chapter of the book, by looking at the catalog we can quickly locate sections of pages, thus speeding up the search speed.
We look at a query:
select * from book where id = 1000000;
Assumptions books table has millions of rows of data, not the index of the query will traverse in front of one million rows of data to find the result, if we build a primary key index on id, is positioned directly on the index results, much faster.
The advantages and disadvantages of index
Advantages: speed up the search
Disadvantages: the data itself, will take up disk space; create and maintain indexes also need time costs; delete, update and insert operations when, due to the maintenance of the index, so the speed will be reduced.
Using an index of grammar
Creating an index
At the same time construction of the table to create the index
create table 表名
(
Field Name Type,
...
Field Name Type,
index index name (field name)
);
After adding an index to build the table
alter table add index table index name (field name);
or
create index index name on table name (field name);
Delete Index
alter table drop index table index name;
or
drop index index name on table;
View the table index
show index from 表名;
Index View query use
explain the query;
Category Index
Index divided by function:
The general index, the index established in the general field, without any restrictions
Primary key index, primary key when you create automatically create the index, can not be empty, can not be repeated
Unique index, index data fields must be unique to allow null values
Full-text index, built on large text type (Text) field index
Composite index, a combination of multiple columns to create the index, multiple columns can not be null value
Code Example:
- Create table books
create table tb_book
(
- create a primary key index
id int primary key,
- create a unique index
title varchar(100) unique,
author varchar(20),
content Text,
time datetime,
- General Index
index ix_title (title),
- full-text index
fulltext index ix_content(content),
- a composite index
index ix_title_author(title,author)
);
- Add primary key index after the construction of the table
ALTER TABLE tb_book ADD PRIMARY KEY pk_id(id);
- Add a unique index after the construction of the table
ALTER TABLE tb_book ADD UNIQUE index ix_title(title);
- Add full-text index after the construction of the table
ALTER TABLE tb_book ADD FULLTEXT index ix_content(content);
- Use the full-text index query
SELECT * FROM tb_book MATCH(content) ANGAINST(‘胜利’);
- Add after the construction of a composite index table
ALTER TABLE tb_book ADD INDEX ix_book(title,author);
Note: When you create a composite index, to follow "the most left-prefix" principle, the most common queries, sort of left field put, in descending order of importance.
Index usage policy
Under what circumstances should be indexed?
On the index often you need to query and sort of field
Particularly data
Do not indexed under what circumstances?
Large number of repeating data fields, such as: gender
Few data
Often require additions and deletions to the field
The index will fail under what circumstances?
Fuzzy query, use like 'Zhang%%' will fail, and the like 'Zhang%' does not
Use is null or is not null query
When using a composite index, a field is null
When using multiple criteria or query
When used in the field functions, such as where year (time) = 2019
Index structure
Different storage engines use different structures of the index:
Clustered index, InnoDB supports consistent physical order, and the order of the index data, and the like pinyin characters are arranged in the order catalog dictionary Xinhua coincide, a clustered index table can have only one.
Non-clustered index, the physical order MyISAM support, and data inconsistency index order, Xinhua similar radical catalog dictionary characters and the order is inconsistent, non-clustered index table may have more.
Data structure of the index mainly: BTree and B + Tree
BTree following data structure, the Search is a balanced tree, each node consisting of a key and data, the key is an index key, the key data is data corresponding to the nodes on both sides of the two pointers point to another index position, and all the keys are sorted, so that when the search index, a binary search may be used, faster, the time complexity is h * log (n), h is the height of the tree, is a more efficient the BTree Search structure.
B + Tree data structure follows BTree is an upgraded version, the difference between non-specific data is not stored in the leaf node, the index key stored only, save the data to the leaf nodes, and leaf nodes and the key data is not only the pointers. B + Tree advantages are: higher search efficiency, because the non-leaf nodes without saving the data, you can save more keys, the more bonds each layer, will reduce the height of the tree, which will improve the query speed .
to sum up
The index is an important means to improve query speed, in this chapter we learned strategies classification and the syntax for creating and using an index of the index, not all of the tables are suitable for creating an index, and finally we also learned about the internal structure of the index, so we'll index I have a basic understanding.