MySQL - Index Transactions

1. Index

1.1 Concept

An index is a special file that contains pointers to all the records in the data table. You can create an index on one or more columns in the table, and specify the type of the index. Each type of index has its own data structure implementation. (The specific details will be explained in the follow-up database principle course)

1.2 Function

  • The relationship between tables, data, and indexes in a database is similar to the relationship between books on the shelf, book content, and book catalogs.
  • The role of an index is similar to that of a book catalog, which can be used to locate and retrieve data quickly.
  • Indexes are of great help in improving database performance.

insert image description here

1.3 Usage scenarios

insert image description hereinsert image description here

To consider creating an index on a column or columns of a database table, the following points need to be considered:

  1. The amount of data is large, and conditional queries are often performed on these columns.
  2. Insert operations to the database table and modification operations to these columns are less frequent.
  3. Indexes take up additional disk space

When the above conditions are met, consider creating indexes on these fields in the table to improve query efficiency.
Conversely, if the column is unconditionally queried, or when inserting and modifying operations are frequently performed, or when the disk space is insufficient, the creation of an index is not considered.

1.4 Use

insert image description hereinsert image description here

When creating a primary key constraint (PRIMARY KEY), a unique constraint (UNIQUE), and a foreign key constraint (FOREIGN KEY), an index on the corresponding column is automatically created.

  • view index
show index from 表名;

Case: View the existing index of the student table

show index from student;
  • create index

For fields that are not primary keys, non-unique constraints, and non-foreign keys, ordinary indexes can be created

create index 索引名 on 表名(字段名);

Case: Create an index for the name field in the class table

create index idx_classes_name on classes(name);
  • drop index
drop index 索引名 on 表名;

Case: delete the index of the name field in the class table

drop index idx_classes_name on classes;

1.5 Cases

Prepare the test sheet:

-- 创建用户表
DROP TABLE IF EXISTS test_user;
CREATE TABLE test_user (
	id_number INT,
	name VARCHAR(20) comment '姓名',
	age INT comment '年龄',
	create_time timestamp comment '创建日期'
);

Prepare test data and insert user data in batches (the operation takes a long time, about 1 hour+):

-- 构建一个8000000条记录的数据
-- 构建的海量表数据需要有差异性,所以使用存储过程来创建, 拷贝下面代码就可以了,暂时不用理解

-- 产生名字
drop function if exists rand_name;
delimiter $$
create function rand_name(n INT, l INT)
returns varchar(255)
begin
	declare return_str varchar(255) default '';
	declare i int default 0;
	while i < n do
		if i=0 then
			set return_str = rand_string(l);
		else
			set return_str =concat(return_str,concat(' ', rand_string(l)));
		end if;
		set i = i + 1;
		end while;
	return return_str;
	end $$
delimiter ;

-- 产生随机字符串
drop function if exists rand_string;
delimiter $$
create function rand_string(n INT)
returns varchar(255)
begin
	declare lower_str varchar(100) default
		'abcdefghijklmnopqrstuvwxyz';
	declare upper_str varchar(100) default
		'ABCDEFJHIJKLMNOPQRSTUVWXYZ';
	declare return_str varchar(255) default '';
	declare i int default 0;
	declare tmp int default 5+rand_num(n);
	while i < tmp do
		if i=0 then
			set return_str
=concat(return_str,substring(upper_str,floor(1+rand()*26),1));
		else
		set return_str
=concat(return_str,substring(lower_str,floor(1+rand()*26),1));
		end if;
	
		set i = i + 1;
		end while;
	return return_str;
	end $$
delimiter ;

-- 产生随机数字
drop function if exists rand_num;
delimiter $$
create function rand_num(n int)
returns int(5)
begin
	declare i int default 0;
	set i = floor(rand()*n);
return i;
end $$
delimiter ;
-- 向用户表批量添加数据
drop procedure if exists insert_user;
delimiter $$
create procedure insert_user(in start int(10),in max_num int(10))
begin
declare i int default 0;
	set autocommit = 0;  	
	repeat
		set i = i + 1;
		insert into test_user values ((start+i) ,rand_name(2,
	5),rand_num(120),CURRENT_TIMESTAMP);
		until i = max_num
	end repeat;
	commit;
end $$
delimiter ;

-- 执行存储过程,添加8000000条用户记录
call insert_user(1, 8000000);

Query the information of the user whose id_number is 778899:

-- 可以看到耗时4.93秒,这还是在本机一个人来操作,在实际项目中,如果放在公网中,假如同时有1000
个人并发查询,那很可能就死机。
select * from test_user where id_number=556677;

insert image description here
You can use explain to view the execution of SQL:

explain select * from test_user where id_number=556677;
*************************** 1. row ***************************
          id: 1
  select_type: SIMPLE
        table: test_user
         type: ALL
possible_keys: NULL
          key: NULL <== key为null表示没有用到索引
      key_len: NULL
          ref: NULL
         rows: 6
        Extra: Using where
1 row in set (0.00 sec)

To provide query speed, create an index on the id_number field:

create index idx_test_user_id_number on test_user(id_number);

Change the ID number to query and compare the execution time:

select * from test_user where id_number=776655;

insert image description here
You can use explain to view the execution of SQL:

explain select * from test_user where id_number=776655;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: test_user
         type: ref
possible_keys: idx_test_user_id_number
          key: idx_test_user_id_number <= key用到了idx_test_user_id_number
      key_len: NULL
          ref: const
         rows: 1
        Extra: Using where
1 row in set (0.00 sec)

The data structure stored by the index is mainly the B+ tree and the hash method. The implementation principle will be explained in the later part of the database principle.

insert image description hereB-tree:
The data of B-tree is stored on both leaf nodes and non-leaf nodes.
insert image description hereB+ tree:

insert image description here
insert image description here
insert image description here

2. Affairs

insert image description hereinsert image description here
insert image description hereinsert image description here
insert image description here
insert image description hereWhen a transaction modifies a certain data, another transaction reads the data. For some reason, the previous transaction revokes the modification of the modified data, and the modified data is about to be restored to its original value, then the latter transaction reads The data is inconsistent with the data available, which is called reading dirty data
insert image description hereinsert image description here

insert image description here
insert image description here
insert image description here

insert image description here

2.1 Why use transactions

Prepare the test sheet:

drop table if exists accout;
create table accout(
	id int primary key auto_increment,
	name varchar(20) comment '账户名称',
	money decimal(11,2) comment '金额'
);
insert into accout(name, money) values
('阿里巴巴', 5000),
('四十大盗', 1000);

For example, the forty thieves stole 2,000 yuan from Alibaba's account

-- 阿里巴巴账户减少2000
update accout set money=money-2000 where name = '阿里巴巴';
-- 四十大盗账户增加2000
update accout set money=money+2000 where name = '四十大盗';

If there is a network error or the database is down when executing the first SQL sentence above, Alibaba's account will be reduced by 2000, but the account of Forty Thieves will not have the increased amount.
Solution: Use transactions to control, to ensure that the above two SQL sentences are either executed successfully or all execution fails.

2.2 The concept of transactions

A transaction refers to a logical group of operations, and the units that make up this group of operations either all succeed or all fail.
In different environments, there can be transactions. Corresponding in the database, is the database transaction.

2.3 Use

(1) Start transaction: start transaction;
(2) Execute multiple SQL statements
(3) Rollback or commit: rollback/commit;
Description: rollback means all failures, and commit means all successes.

start transaction;
-- 阿里巴巴账户减少2000
update accout set money=money-2000 where name = '阿里巴巴';
-- 四十大盗账户增加2000
update accout set money=money+2000 where name = '四十大盗';
commit;

The characteristics and settings of transactions will be further explained in the follow-up database principle section.

3. Summary of key content

Index:
(1) For tables with high frequency of inserting and deleting data, index is not applicable
(2) For a column with high modification frequency, this column is not applicable for index
(3) For a column or columns with high query frequency , you can create indexes on these columns

Transaction:

start transaction;
...
rollback/commit;

4. Homework

Guess you like

Origin blog.csdn.net/qq_43398758/article/details/123346765