MySQL index, transaction and storage engine-personal organization, thanks for watching

One, index

1.1 Concept

  • Sorted list, index value and position corresponding to this value, physical address;
  • Convenient to query, you can find the required data through the physical address;
  • The method of sorting the values ​​of one or several columns in the table;
  • Take up additional disk space;

1.2 Advantages

  • Fast positioning and speed up the query rate;
  • When the data table is large, query multiple representations to improve efficiency;
  • Reduce the IO cost of the database and reduce the sorting cost of the database;
  • Create a unique index to ensure the uniqueness of the data in the data table;
  • Speed ​​up the connection between data tables and tables;
  • When using grouping and sorting, it can greatly reduce the combined sorting time;

1.3 Classification

  • Normal index
  • The most basic index type, there are no restrictions such as uniqueness
  • The way to create a normal index
  • Unique index
  • Basically the same as'normal index'
  • Difference between ordinary index: all values ​​of the index column can only appear once and must be unique
  • Primary key index
  • Is a special unique index, designated as'primary key' (lower case)
  • A table can only have one primary key, no null values ​​are allowed
  • Combined index (single column and multiple columns)
  • It can be created on a single column or on multiple columns
  • The leftmost principle, executed from left to right
  • Full-text index
  • mysql supports full-text indexing and full-text retrieval from version 3.23.23
  • Index type is fulltext
  • Can be created on CHAR, VARCHAR or TEXT type columns

Icon:
Insert picture description here

Second, the principle basis for creating an index

Conditions suitable for indexing

  • Primary key of the table, foreign key
  • Tables with more than 300 rows
  • Tables that are often connected to other tables, create an index on the connection field
  • Fields that often appear in the where clause, especially fields of large tables
  • On fields with high selectivity
  • Small fields can also be created

Conditions not suitable for indexing

  • Fields with poor uniqueness
  • Fields that are updated too frequently
  • Large text fields or even very long fields

Three, view, delete, index method

  • View index

show index from table name;

show keys from 表名;

  • Delete index

drop index index name on table name;

alter table table name drop index name;

Four, affairs

4.1 Concept

  • A protection mechanism, an operation sequence contains a set of database operation commands, and all the commands as a whole are submitted or cancelled to the system, that is, this set of database commands are either executed or not executed
  • An indivisible logical unit of work. When performing concurrent operations on the database system, the transaction is the smallest unit of control
  • It is suitable for the scenario of a database system operated by multiple users at the same time, and critical operations with sensitive data. Such as: banks, securities trading systems...
  • Through the integrity of the transaction to ensure data consistency! !

4.2 ACID characteristics of transactions

  • Atomicity

A transaction is a complete operation, and the elements of the
transaction are indivisible . All elements in the transaction must be committed or rolled back as a whole.
If any element in the transaction fails, the entire transaction will fail

  • consistency
  • When the transaction is completed, the data must be in a consistent state
  • Before the transaction begins, the data stored in the database is in a consistent state
  • In the ongoing transaction, the data may be in an inconsistent state
  • When the transaction is successfully completed, the data must be returned to a known consistent state again
  • Isolation
  • All concurrent transactions that modify data are isolated from each other, indicating that the transaction must be
    independent, and it should not depend on or affect other transactions in any way
  • Modify data transactions can use the same data in another transaction before the start of this visit
    visit these numbers after the end of some data, or use the same data in another transaction
    data
  • Endurance
  • Refers to whether the system fails or not, the result of transaction processing is permanent
  • Once the transaction is committed, the effect of the transaction will be permanently retained in the database

4.3 Transaction Control Statement

  • Mysql transaction is automatically submitted by default, and the transaction is automatically submitted when the sql statement is submitted
  • Control statement
  • BEGIN或START TRANSACTION
  • COMMIT
  • ROLLBACK
  • SAVEPOINT transaction name
  • RELEASE SAVEPOINT transaction name
  • ROLLBACK TO transaction name
  • SET TRANSACTION
  • Control statement explanation: (explained in order from top to bottom:)

Show to start a transaction;
commit the transaction and make all the modifications made to the database permanent;
rollback will end the user's transaction and undo all uncommitted modifications in progress;
allow a save point to be created in the transaction , There can be multiple
savepoints for deleting a transaction in a transaction. When no savepoint is specified, an exception will be displayed when the statement is executed. The
transaction will be rolled to the saved transaction point; it is
used to set the isolation level of the transaction. The lnnonDB storage engine provides transaction isolation levels, including read uncommitted, read committed...

  • First build the library, table, and add data;
创建数据库:
mysql> create database uou;
Query OK, 1 row affected (0.00 sec)

          
创建数据表:
mysql> create table lol(
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> address varchar(50) default 'taihe',
    -> age int(3) not null);
    
在表中添加数据:
mysql> insert into lol
    -> (name,address,age) values
    -> ('小明','北京',23),
    -> ('小刚','南京',20);

查看数据记录
mysql> select * from lol;
+----+--------+---------+-----+
| id | name   | address | age |
+----+--------+---------+-----+
|  1 | 小明   | 北京    |  23 |
|  2 | 小刚   | 南京    |  20 |
+----+--------+---------+-----+


  • Transaction operation
mysql> begin;   
Query OK, 0 rows affected (0.00 sec)

mysql> insert into lol
    -> (name,address,age)
    -> values
    -> ('小红','深圳',22);


mysql> select * from lol;
+----+--------+---------+-----+
| id | name   | address | age |
+----+--------+---------+-----+
|  1 | 小明   | 北京    |  23 |
|  2 | 小刚   | 南京    |  20 |
|  3 | 小红   | 深圳    |  22 |
+----+--------+---------+-----+
3 rows in set (0.00 sec)

mysql> savepoint a;          #保存到一个事务点名a;
Query OK, 0 rows affected (0.00 sec)


mysql> insert into lol   #写入数据验证
    -> (name,address,age)
    -> values
    -> ('小兰','上海',25);
Query OK, 1 row affected (0.00 sec)

mysql> select * from lol;
+----+--------+---------+-----+
| id | name   | address | age |
+----+--------+---------+-----+
|  1 | 小明   | 北京    |  23 |
|  2 | 小刚   | 南京    |  20 |
|  3 | 小红   | 深圳    |  22 |
|  4 | 小兰   | 上海    |  25 |
+----+--------+---------+-----+
4 rows in set (0.00 sec)



mysql> rollback to a;    #回滚到保存点a;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from lol;
+----+--------+---------+-----+
| id | name   | address | age |
+----+--------+---------+-----+
|  1 | 小明   | 北京    |  23 |
|  2 | 小刚   | 南京    |  20 |
|  3 | 小红   | 深圳    |  22 |
+----+--------+---------+-----+
3 rows in set (0.00 sec)

mysql> insert into lol      #重新写入数据
    -> (name,address,age)
    -> values
    -> ('小兰','上海',25);
Query OK, 1 row affected (0.00 sec)

mysql> commit;	        #提交数据之后,保证一致性,才会显示!!

退出当前管理用户账号,从新登录进来,会看到数据已经写进去!

Five, storage engine

5.1 Introduction

  • Data in MySQL using different technologies stored in the file, each
    technique uses a different storage mechanism, indexing techniques, lock level
    and ultimately provide different functions and capabilities of these different technologies as well as with
    sets of features Called storage engine in MySQL

  • The storage engine is the storage
    method or storage format in which MySQL stores data in the file system

  • MySQL commonly used storage engine
    ●MyISAM
    ●InnoDB

  • A component in the MySQL database responsible for performing actual data I/0 operations

  • In the MySQL system, the storage engine is on the file system. Before data is
    saved to the data file, it is transferred to the storage engine, and then
    stored in accordance with the storage format of each storage engine

5.2 MyISAM

  • MyISAM does not support transactions or foreign keys
  • Fast access
  • No requirement for transaction integrity
  • MyISAM is stored as three files on the disk.
    ●.frm file storage table definition
    ●The extension of the data file is .MYD (MYData)
    ●The extension of the index file is .MYI (MYIndex)

■Table-level locking mode, the entire table is locked when data is updated.
■Database blocks each other during reading and writing.
●It will block user data reading during data writing.
●It will also block user data during data reading. Writing
■Data is written or read separately, the speed process is faster and the resources are relatively small
■The storage format supported by MyIAM
●Static table
●Dynamic table
●Compressed table

5.3 Production scenarios applicable to MyISAM

  • The company's business does not require support
  • Services that unilaterally read or write more data
  • MyISAM storage engine data reads and writes are frequent, not suitable for scenarios
  • Use read and write concurrent access to relatively low business
  • Businesses with relatively little data modification
  • Data business-business that does not require very high compliance
  • Server hardware resources are relatively poor

5.4 Introduction to InnoDB

  • Support 4 transaction isolation levels
  • Row-level locking, but full table scan will still be table-level locking
  • MyISAM storage engine data reads and writes are frequent, not suitable for scenarios
  • Use read and write concurrent access to relatively low business
  • Businesses with relatively little data modification
  • Data business-business that does not require very high compliance
  • Server hardware resources are relatively poor

5.5 Analysis of applicable production scenarios

  • Business needs support
  • Row-level locking has good adaptability to high concurrency, but you need to ensure that the query is
    completed through the index
  • Scenarios
    where business data is updated frequently, such as forums, Weibo, etc.
  • Business data consistency requirements are high,
    such as: banking
  • Hardware devices have large memory, use Innodb's better caching capabilities to improve
    memory utilization and reduce disk IO pressure

5.6 Basis for Enterprise Selection of Storage Engine

  • Need to consider the core functions and application scenarios provided by each storage engine
  • Supported fields and data types
    All engines support common data types,
    but not all engines support other field types, such as binary objects
  • Lock type: different storage engines support the same level of lock
    table lock
    row lock
  • Indexing support to
    establish a search
    engine | can significantly improve performance when searching and restoring data in the database. Different storage engines | engines provide different indexing technologies.
    Some storage engines do not support indexes at all.
  • Transaction processing support
    Improve the reliability during the update and insertion of information in the table. The
    storage engine can be selected according to whether the business needs to support transactions.

Six, modify the storage engine method

方法1:
alter table 表名  engine=引擎;

方法2:
vi  /etc/my.cnf
在[mysqld]配置段内添加,指定默认引擎并重启服务;
default-storage-engine=innoDB
重启mysqld

方法3:
create table  表名  (字段....)  engine = 引擎;

方法4:
Mysql_convert_table_format--user=root--password=密码 --sock=/tmp/mysql.sock --engine = 引擎  库名  表名

yum -y install perl-DBI perl-DBD-MySQL
/usr/local/mysql/bin/mysql_convert_table_format --user=root --password='123456' --sock=/tmp/mysql.sock auth

Guess you like

Origin blog.csdn.net/m0_46563938/article/details/108531716