MySQL database --- MySQL index transaction

MySQL Index Transactions

1. Index

1.1 The concept of index

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. More generally speaking, the database index is like the table of contents in front of a book, which can speed up the query speed of the database.

1.2 The role of indexes

Now we have a student table that needs to find the information of the student whose student number is 8.
insert image description here
In the absence of an index, the database search process is similar to a "sequence table" search. The
sequence table is searched in memory, and the memory access speed is fast , the data is not much, and its speed is also possible. The
database is searched sequentially, the data of the database is on the disk, the disk access speed is slow, and the amount of data is large, the speed is very slow.

The index is to avoid sequential search in the database and improve the efficiency of search

1.3 Index data structure

1.3.1 Why not use a hash table

We know that the lookup efficiency of the hash table is O(1), so using the hash table to find a data will be very fast, so why not use the hash table for the index?

Reason: If you use a hash table, you can only deal with equal cases, but cannot handle > < >=such cases. Because hash table storage is not sequential.

1.3.2 Why not use a binary search tree

We know that the result of in-order traversal of binary search tree is ordered. If you want to find id<6 且 id>3the data of , you can find it first id =3and id = 6then the data between 3 and 6 in in-order traversal. Compared with the hash table , a binary tree can handle range searches, so why not use a binary search tree?

Reason: If a binary search tree is used, each node of the binary tree has at most 2 forks. When the amount of data is large, the height of the tree will be higher, and the efficiency of the final operation will be very low. The efficiency of order traversal is not very efficient O(N). This is very inefficient , just like the efficiency of ordinary search.

1.3.3 What is a B-tree

insert image description here

Advantages of B-TREE:

  •  不再是二叉搜索,而是N叉搜索,树的高度会降低,查询快
    
  •  叶子节点,非叶子节点,都可以存储数据,且可以存储多个数据
    
  • 通过中序遍历,可以访问树上所有节点
    

1.3.4 What is a B+ tree

The real index is the B+ tree used.
insert image description here

Advantages of B+TREE:

  1. It is still an N-ary tree with a small level. Non-leaf nodes no longer store data. Data is only stored on leaf nodes of the same layer. The length of the path from the root to each node of the B+ tree is the same, while the B tree is not like this. (represents a query The speed of any record is relatively average, and there will be no large difference in efficiency.)
  2. Between the leaves, a linked list (pointed by the red arrow in the figure) is added to obtain all nodes, no in-order traversal is required, and the next node of the linked list can be used to quickly access the
  3. In terms of range search, after locating min and max, the middle leaf node is the result set, without in-order backtracking (range query is used a lot in SQL, which is the biggest advantage of B+ tree over B tree)
  4. The leaf node stores the actual record row, and the record row is relatively compactly stored, which is suitable for large data volume disk storage; the non-leaf node stores the PK of the record, which is used for query acceleration and is suitable for memory storage
  5. If the non-leaf node does not store the actual record, but only stores the KEY of the record, then in the case of the same memory, the B+ tree can store more indexes

1.4 Usage scenarios

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

  • The amount of data is large, and conditional queries are often performed on these columns.
  • Insert operations to the database table and modification operations to these columns are less frequent.
  • Indexes consume 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.5 Use of indexes

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

1.5.1 View Index

show index from 表名;

Example:
insert image description here

1.5.2 Create Index

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

Example:
insert image description here

1.5.3 Deleting an index

drop index 索引名 on 表名;

Example:
insert image description here

1.6 Index Types in MySQL

  • Normal index : allows the indexed data column to contain duplicate values
  • Unique index : can guarantee the uniqueness of data records
  • Primary key index : It is a special unique index. Only one primary key index can be defined in a table. The primary key is used to uniquely identify a record. Use the keyword PRIMARY KEY to create
  • Joint index : An index can cover multiple data columns, such as an INDEX(columnA,columnB)index
  • Full-text indexing : By establishing a flashback index, the retrieval efficiency can be greatly improved, and the problem of judging whether a field is included is a key technology currently used by search engines, and ALTER TABLE table_name ADD FULLTEXT(column)a full-text index can be created by;

2. Transactions

2.1 Why use transactions

There is a data table that stores the bank accounts of some people, and
now there is a person A who needs to transfer 3000 yuan to B.

At this point, two operations need to be performed.

  1. A's account balance - 3000
  2. B's account balance + 3000

Suppose when performing operation 1, after the execution, there is an error in the network, or the database hangs, the money of A is less and the money of B is not increased, and the 3000 disappears out of thin air?

Solution: Use transactions to control, to ensure that the above two SQL sentences are either executed successfully or all fail to execute

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 to the database, it is the database transaction.

2.3 Basic Characteristics of Transactions (ACID)

The fundamental property of transactions is called "ACID"

  • Atomicity (or indivisible): Several operations in a transaction are either all executed successfully or none of them are executed. (The non-execution here is not really not executed, but once a certain step in the middle If there is an error in execution, rollback the previously executed steps.)

  • Consistency: The integrity of the database is not destroyed before the transaction begins and after the transaction ends. Before and after the transaction is executed, the data is always in a legal state. (For example, when the transfer operation is performed, when the account balance is reduced, the account cannot be reduced to a negative number)

  • Isolation (also known as independence): The ability of a database to allow multiple concurrent transactions to read, write and modify its data at the same time. Isolation can prevent data inconsistency due to cross execution when multiple transactions are executed concurrently.

  • Durability: Once the transaction is executed, the modification to the data is durable (the data is persistent when it is stored on the disk. When it is stored in the memory, it is not persistent)

2.4 Use of Transactions

(1) Open a transaction: start transaction;
(2) Execute multiple SQL statements
(3) Rollback or commit:rollback/commit;

Description: rollback means all failures, commit means all successes

test table

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);

insert image description here
Operation screenshot:
insert image description here

Guess you like

Origin blog.csdn.net/wwzzzzzzzzzzzzz/article/details/123340429