MySQL index, transaction and storage engine (detailed graphic and text!) (1)
Article Directory
- MySQL index, transaction and storage engine (detailed graphic and text!) (1)
- One, MySQL index
-
- 1. The concept of index
- 2. The role of the index
- 3. Side effects of indexing
- 4. The principle basis for creating an index
- (1) Classification and creation of indexes
- (2) View the index
- (3) Delete the index
Preparation: first create a database and table
mysql -u root -p
create database SCHOOL;
use SCHOOL;
create table class (id int(10) not null,name varchar(10) not null,cardid varchar(18),phone varchar(11),address varchar(50));
desc class;
insert into class values (1,'zhangsan','12','111111','nanjing');
insert into class values (4,'lisi','123','444444','suzhou');
insert into class values (2,'wangwu','1234','222222','beijing');
insert into class values (5,'zhaoliu','12345','555555','nanjing');
insert into class values (3,'qianqi','123456','333333','shanghai');
select * from class;
Insert data and view table structure
One, MySQL index
1. The concept of index
1) The index is a sorted list, in which the value of the index and the physical address of the row containing the value of the data are stored (similar to the C language linked list points to the memory address of the data record through the pointer).
2) After using the index, you can locate the data of a row without scanning the entire table, but first find the physical address corresponding to the row of data through the index table and then access the corresponding data, so the query speed of the database can be accelerated.
3) The index is like a table of contents of a book, you can quickly find the content you need according to the page number in the table of contents.
4) Index is a method of sorting the values of one or several columns in a table.
5) The purpose of indexing is to speed up the search or sorting of records in the table.
2. The role of the index
1) After setting up a suitable index, the database uses various fast positioning technologies to greatly speed up the query speed. This is the main reason for creating an index.
2) When the table is very large or the query involves multiple tables, the use of indexes can increase the query speed by thousands of times.
3) The I/O cost of the database can be reduced, and the index can also reduce the sorting cost of the database.
4) By creating a unique index, the uniqueness of each row of data in the data table can be guaranteed.
5) Can speed up the connection between the table and the table.
6) When using grouping and sorting, the time for grouping and sorting can be greatly reduced.
3. Side effects of indexing
1) The index requires additional disk space.
- For the MyISAM engine, the index file and the data file are separated, and the index file is used to save the address of the data record.
- The InnoDB engine table data file itself is the index file.
2) It takes more time to insert and modify data, because the index will also change accordingly.
4. The principle basis for creating an index
Indexes can increase the speed of database queries, but they are not suitable for creating indexes under all circumstances. Because the index itself consumes system resources, if there is an index, the database will first perform an index query and then locate a specific data row. If the index is used improperly, it will increase the burden on the database.
1) The primary key and foreign key of the table must have indexes. Because the primary key is unique, the foreign key is related to the primary key of the main table, which can be quickly located when querying.
2) Tables with more than 300 rows of records should have indexes. If there is no index, the table needs to be traversed for each query, which will seriously affect the performance of the database.
3) For tables that are frequently connected to other tables, an index should be established on the connection field.
4) Fields with poor uniqueness are not suitable for indexing.
5) Fields that are updated too frequently are not suitable for index creation.
6) Fields that often appear in the where clause, especially fields of large tables, should be indexed.
7) Indexes should be built on highly selective fields.
8) Indexes should be built on small fields. Do not build indexes for large text fields or even long fields.
(1) Classification and creation of indexes
1. Ordinary index
- The most basic index type, there are no restrictions such as uniqueness
1.1 Directly create an index
CREATE INDEX 索引名 ON 表名 (列名[(length)]);
#(列名(length)):length是可选项,下同。如果忽略 length 的值,则使用整个列的值作为索引。如果指定使用列前的 length 个字符来创建索引,这样有利于减小索引文件的大小。
#索引名建议以“_index”结尾。
例:
create index phone_index on class (phone);
show keys from class\G;
1.2 Modify table creation
ALTER TABLE 表名 ADD INDEX 索引名 (列名);
例:
alter table class add index id_index (id);
show keys from class\G;
1.3 Specify the index when creating the table
CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));
例:
create table test(
id int(10) not null,
name varchar(20) not null,
index id_index (id));
show keys from test\G;
2. Unique index
- Similar to a normal index, but the difference is that each value of the unique index column is unique. The unique index allows null values (note that it is different from the primary key). If it is created with a composite index, the combination of column values must be unique. Adding a unique key will automatically create a unique index.
2.1 Create a unique index directly
CREATE UNIQUE INDEX 索引名 ON 表名(列名);
例:
create unique index address_index on class(address);
create unique index id_index on class(id);
show keys from class\G;
2.2 Modify table creation
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);
例:
alter table class add unique cardid_index (cardid);
show keys from class\G;
2.3 Specify when creating a table
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名));
例:
create table test01(
id int(10) not null,
name varchar(20) not null,
unique id_index (id));
show keys from test01\G;
3. Primary key index
- It is a special unique index and must be designated as "PRIMARY KEY". A table can only have one primary key, and no null values are allowed. Adding a primary key will automatically create a primary key index.
3.1 Specify when creating a table
CREATE TABLE 表名 ([...],PRIMARY KEY (列名));
例:
create table test02(
id int(10) not null,
name varchar(20) not null,
primary key (id));
或
create table test03(id int(10) not null primary key,name varchar(20) not null);
show keys from test02\G;
show keys from test03\G;
3.2 Modify table creation
ALTER TABLE 表名 ADD PRIMARY KEY (列名);
例:
alter table class add primary key(id);
show keys from class\G;
4. Combined index (single-column index and multi-column index)
- It can be an index created on a single column or an index created on multiple columns. The leftmost principle needs to be satisfied. Because the where conditions of the select statement are executed from left to right in turn, the order of the fields used in the where conditions must be consistent with the order in the composite index when using the select statement to query, otherwise the index will not take effect.
CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,INDEX 索引名 (列名1,列名2,列名3));
select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3='...';
例:
create table test04(id int(10) not null,
name varchar(20) not null,
sex char(2) not null,
index index_amd (id,name));
show keys from test04\G;
insert into test04 values(1,'zhangsan','男');
select * from test04 where id=1 and name='zhangsan';
select * from test04 where id=1;
select * from test04 where name='zhangsan' and id=1;
5. Full text index (FULLTEXT)
- It is suitable for fuzzy query and can be used to retrieve text information in an article.
- Before MySQL 5.6 version FULLTEXT index can only be used in MyISAM engine;
- After version 5.6, the innodb engine also supports FULLTEXT indexes.
- Full-text indexes can be created on columns of type CHAR, VARCHAR, or TEXT. Only one full-text index is allowed per table.
5.1 Directly create an index
CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);
例:
create fulltext index cardid_index on class (cardid);
5.2 Modify table creation
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);
例:
alter table class add fulltext cardid_index (cardid);
5.3 Specify an index when creating a table
CREATE TABLE 表名 (字段1 数据类型[,...],FULLTEXT 索引名 (列名));
#数据类型可以为 CHAR、VARCHAR 或者 TEXT
例:
create table test05(
id int(10) not null,
name varchar(20) not null,
address varchar(50),
fulltext address_full(address));
insert into test05 values ('1','zhangsan','shanghai');
insert into test05 values ('2','lisi','shanghai');
insert into test05 values ('3','wangwu','nanjing');
5.4 Use full-text index query
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');
例:
select * from test05 where match(address) against('shanghai');
(2) View the index
#两个命令查看的信息一样,选其一即可。
show index from 表名;
或者
show keys from 表名;
#---------------查看后个字段的解释----------------------------------------------------
各字段的含义如下:
Table:表的名称。
Non_unique:如果索引不能包括重复词,则为 0;如果可以,则为 1。
Key_name:索引的名称。
Seq_in_index:索引中的列序号,从 1 开始。
Column_name:列名称。
Collation:列以什么方式存储在索引中。在 MySQL 中,有值‘A’(升序)或 NULL(无分类)。
Cardinality:索引中唯一值数目的估计值。
Sub_part:如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为 NULL。
Packed:指示关键字如何被压缩。如果没有被压缩,则为 NULL。
Null:如果列含有 NULL,则含有 YES。如果没有,则该列含有 NO。
Index_type:用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)。
Comment:备注。
(3) Delete the index
3.1 Directly delete the index
DROP INDEX 索引名 ON 表名;
例:
drop index phone_index on class;
3.2 Modify the table to delete the index
ALTER TABLE 表名 DROP INDEX 索引名;
例:
alter table class drop index id_index;
3.3 Delete the primary key index
ALTER TABLE 表名 DROP PRIMARY KEY;
例:
alter table class drop primary key;