当前位置:网站首页>[MySQL] index and transaction
[MySQL] index and transaction
2022-06-13 03:49:00 【Bryant tapping the code】
Blog home page : Bryant typing the code
Welcome to thumb up Collection Leaving a message. Welcome to discuss !
This paper is written by 【 Bryant typing the code 】 original , First appeared in CSDN
Because the blogger is learning Xiaobai one , There are bound to be mistakes , If you have any questions, please leave a message in the comment area to point out , Be deeply grateful !
Boutique column ( Update from time to time )【JavaSE】 【Java data structure 】【LeetCode】
【MySQL】 Indexes and transactions
1. Indexes
What is index
Index is a special kind of file , Contains a reference pointer to all records in the data table . You can index one or more columns in a table , And specify the type of index , Each index has its own data structure implementation .
The function of index
- Tables in the database 、 data 、 The relationship between indexes ,
It's like a book on a shelf 、 The relationship between book content and book catalog . - The function of the index is similar to that of a book catalogue , Can be used for
Rapid positioning 、 Retrieving data. - The index is for
Improve database performanceIt helps a lot .
Index usage scenarios
Consider creating an index on a column or columns of a database table , The following points need to be considered :
- Large amount of data , And we often make conditional queries on these columns .
- The insert operation of the database table , And modify these columns less frequently .
- Indexes take up extra disk space .
- according to where Conditional query to create the appropriate index .
- The more indexes you create, the better , When there is a large amount of data, updating the index also takes time
When the above conditions are met , Consider indexing these fields in the table , To improve query efficiency . conversely , If the non conditional query column , Or insert it often 、 Modify the operating , Or when there is not enough disk space , Don't think about creating indexes
Simple use of index
Create a primary key constraint (PRIMARY KEY)、 Unique constraint (UNIQUE)、 Foreign key constraints (FOREIGN KEY) when , Meeting Automatically create the index of the corresponding column .
Look at the index
show index from Table name ;
Create index
create index Index name on Table name ( Field name );
Delete index
drop index Index name on Table name ;
2. Business
What is a transaction and why it is used
Concept of transactions
Business ( It's atomic ) A logical set of operations , The units that make up this set of operations , All or nothing , All or nothing . In different environments , You can have business . In the database , It's database transactions .
Reasons for using transactions
In some cases , If there are no transactions, there will be big problems . For example, the problem of bank transfer .
Prepare the test sheet :
drop table if exists accout;
create table accout(
id int primary key auto_increment,
name varchar(20) comment ' title of account ',
money decimal(11,2) comment ' amount of money '
);
insert into accout(name, money) values
(' Alibaba ', 5000),
(' The Forty Thieves ', 1000);
for instance , Forty thieves stole from Alibaba's account 2000 element
-- Alibaba accounts are down 2000
update accout set money=money-2000 where name = ' Alibaba ';
-- The accounts of the forty thieves have increased 2000
update accout set money=money+2000 where name = ' The Forty Thieves ';
If you are executing the first sentence above SQL when , There was a network error , Or the database hangs up , Alibaba's account will be reduced 2000, But there is no increase in the account of the forty thieves .
Solution : Use transactions to control , Make sure the above two sentences SQL Or it all works , Or all failed
How to start a transaction
Start the trilogy of affairs
- Open transaction :start transaction;
- Execute more than one SQL sentence
- Roll back or commit rollback( All failed )/commit( All success ).
explain :rollback It's all failure ,commit It's all success .
An example of starting a transaction
start transaction;
-- Alibaba accounts are down 2000
update accout set money=money-2000 where name = ' Alibaba ';
-- The accounts of the forty thieves have increased 2000
update accout set money=money+2000 where name = ' The Forty Thieves ';
commit;
After opening the transaction , If not implemented to commit, Then the data in the database has not been modified , Although the current database shows that money has changed , But if you open another database port and re-enter, you will find that there is no change , This is the benefit of starting a transaction , If the execution is not successful, the data in the database will not be modified .
Four characteristics of transactions
- Atomicity : For a set of operations ( Mainly updates ), All or nothing , All or nothing .
- Uniformity : In a business , The results of multiple queries are consistent .
- Isolation, : Different transactions , Inquire about / The modified data is isolated from each other . A transaction is not committed / Before rolling back , Only you can see the modified data .
- persistence : After the transaction is committed , The data will be persisted to the hard disk .
summary
Indexes :
(1) For inserting 、 Delete tables with high data frequency , Not applicable index
(2) For a column with high modification frequency , This column is not applicable to index
(3) Those with high query frequency through the conditions of a certain column or several columns , You can create indexes on these columns
Business :
start transaction;
...
rollback/commit;
边栏推荐
- Express 100
- 【youcans 的 OpenCV 例程200篇】201. 图像的颜色空间转换
- Student management system
- 【多线程】多线程到底是个甚——多线程初阶(复习自用)
- 单片机:A/D 差分输入信号
- Lambda end operation collect
- How much can I get after the insurance period of annuity insurance products expires?
- Very simple installation and configuration of nodejs
- GoFrame第五天
- Multi thread implementation of selling tickets and producers and consumers
猜你喜欢

单片机:Modbus 通信协议介绍

An error is reported in the JDBC connection database: connection to 139.9.130.37:15400 referred

Very simple installation and configuration of nodejs

无人机避障四种常见技术中,为何大疆首选双目视觉

OKR和KPI的区别

【测试开发】自动化测试selenium(二)——webdriver常用的API

【面试复习】自用不定时更新

leetcode.1 --- 两数之和
![[test development] automatic test selenium (I)](/img/cd/b6dc4ac53b4f30f745ec0590ac384b.png)
[test development] automatic test selenium (I)

【测试开发】用例篇
随机推荐
How can a sweeping robot avoid obstacles without "mental retardation"? Analysis of five mainstream obstacle avoidance techniques
谈谈激光雷达的波长
SQL injection case demonstration and preventive measures
Window and various windowfunctions in Flink
Lambda end operation collect
Database object, sequence, view, index
不卷了!团队又一位成员离职了。。
【测试开发】自动化测试selenium篇(一)
单片机:EEPROM 多字节读写操作时序
H5 jump to mobile app store
[test development] blog system - LoadRunner performance test (publish blog function benchmark test)
Spark optimization - data skew solution
LVS四層負載均衡集群(3)集群功能分類 - HPC
【MySQL】索引与事务
Stream流的注意事项
单片机:D/A 输出
Spark kernel (execution principle) environment preparation /spark job submission process
大五人格学习记录
单片机:Modbus 通信协议介绍
leetcode.1 --- 两数之和
