MySQL index transaction

table of Contents

MySQL index transaction

1. Index

1.1 Concept

1.2 Role

1.3 Use

1.4 Case

2. Affairs

2.1 Concept

2.2 Use

3. Summary of content highlights


MySQL index transaction

1. Index

1.1 Concept

An index is a special file that contains reference pointers to all records in the data table. You can create an index on one or more columns in the table,
And specify the type of index, each type of index has its own data structure to achieve.

1.2 Role

  • The relationship among tables, data, and indexes in the database is similar to the relationship among books, book contents, and book catalogs on the bookshelf.
  • The index plays a role similar to a book catalog, and can be used to quickly locate and retrieve data.
  • Indexes are of great help to improve the performance of the database

1.3 Use

Create a primary key constraint ( PRIMARY KEY ), unique constraint ( UNIQUE ), foreign key constraint ( FOREIGN KEY ), it will be automatically created
The index of the corresponding column.
  • View index
show index from 表名;
 
  • Create index
create index 索引名 on 表名(字段名);
For non-primary key, non-unique constraint, non-foreign key fields, you can create ordinary indexes
  • Delete index
drop index 索引名 on 表名;

1.4 Case

Prepare test form:
-- 创建用户表
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 to insert user data in batches:

-- 构建一个40000条记录的数据
-- 构建的海量表数据需要有差异性,所以使用存储过程来创建, 拷贝下面代码就可以了,暂时不用理解
-- 产生名字
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 ;
-- 执行存储过程,添加40000条用户记录
call insert_user(1, 40000);

 Query the information of the user whose id_number is 399 99 :

select * from test_user where id_number=39999;

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

create index idx_test_user_id_number on test_user(id_number);

Show the index we created: 

Compare execution time: 

The data structure saved by the index is mainly B+ tree, and the way of hash .

2. Affairs

2.1 Concept

A transaction refers to a logical set of operations. The units that make up this set of operations either all succeed or all fail.
There can be transactions in different environments. Corresponding to the database, it is the database transaction.

2.2  Use

 
Prepare test form:
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
('A', 5000),
('B', 1000);

 For example, A transfers 2000 yuan to B:

--A账户减少2000
update accout set money=money-2000 where name = A';
--B账户增加2000
update accout set money=money+2000 where name = 'B';
 
If a network error occurs during the execution of the first SQL sentence above , or the database is down, A’s account will be reduced by 2000 , but
There is no additional amount in B's account.
Solution: Use transactions to control and ensure that all of the above two SQL statements are executed successfully or all executions fail.
 
  1. Start the transaction: start transaction;
  2. Execute multiple SQL statements
  3. Rollback or commit: rollback/commit;
start transaction;
-- A账户减少2000
update accout set money=money-2000 where name = 'A';
-- B账户增加2000
update accout set money=money+2000 where name = 'B';
commit;
Note: rollback means all failures, commit means all successes.
 

3. Summary of content key points

  • index:
  1. For tables with high frequency of inserting and deleting data, indexes are not applicable
  2. For a column with a high frequency of modification, the column does not apply to the index
  3. If the query frequency is high through a certain column or certain columns, you can create an index on these columns
  • Affairs
start transaction;
...
rollback/commit;

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/XKA_HRX/article/details/114931067