Mysql performance tuning (9)

Preface

  In the previous article, we introduced application optimization , query cache optimization in mysql, memory management optimization in mysql, and concurrency parameter adjustment in mysql. Next, we will introduce the locks in MySQL, including the contention of locks and the related content of row locks and table locks. In addition, we will introduce some commonly used techniques in SQL. First, we will introduce the related content of locks.

One, MySQL lock

1. Overview of locks

  Lock is a certain resource mechanism that the computer coordinates concurrent access by multiple processes or threads. In the database, in addition to the contention of the system's computing resources (including CPU, RAM, I/O, etc.), data is also a resource shared by many users. How to ensure the consistency and effectiveness of concurrent access to data is a problem that all databases must solve, and lock conflicts are also an important factor that affects the performance of concurrent access to databases. From this perspective, locks are particularly important for databases, and they are more complicated.

2. Lock classification

  From the granularity of data operations, it can be divided into the following categories:

  • Table lock: During operation, the entire table will be locked
  • Row lock: During operation, the current operation row will be locked

The types   of data operations are divided into the following categories:

  • Read lock (shared lock) : For the same piece of data, multiple read operations can be performed at the same time without affecting each other.
  • ②. Write lock (exclusive lock) : Before the current operation is completed, it will block other write locks and read locks.

3. MySQL lock

  Compared with other databases, MySQL's lock mechanism is relatively simple. Its most notable feature is that different storage engines support different lock mechanisms. The following table shows the lock support status of each storage engine:

  The characteristics of the three types of locks in MySQL It can be roughly summarized as follows:

  from the above characteristics, it is difficult to say in general which lock is better, only which lock is more suitable in terms of specific application characteristics. Only from the perspective of locks: table-level locks are more suitable for query-based applications, such as Web applications, which update data based on index conditions; while row-level locks are more suitable for a large number of concurrent updates of a small amount of different data based on index conditions. , And there are concurrent query applications, such as some online transaction processing (OLTP) systems.

4. MyISAM table lock

  The MyISAM storage engine only supports table locks, which is the only lock type supported in the first few versions of MySQL.

  • 1, how to increase table lock
      MyISAM executing query ( SELECTbefore), will automatically give all the tables plus a read lock involved in the update operation ( UPDATE, DELETE, INSERTetc. ) before automatically to the table write-lock involved in this process and No user intervention is required. Therefore, users generally do not need to directly use LOCK TABLEcommands to lock the MyISAM table display. The specific commands are as follows:
加表锁:lock table table_name read;
加写锁:lock table table_name write;

  We first create a tb_booktable named , and insert some data, the specific code is as follows:

create datebase demo_03 default charset=utf8mb4;
use demo_03;
create table `tb_book`(
	`id` int(11) auto_increment,
	`name` varchar(50) default null,
	`publish_time` date default null,
	`status` char(1) default null,
	primary key(`id`)
)engine=myisam default charset=utf8;
insert into tb_book(id, name, publish_time, status)values(null, 'mysql从入门到精通','2020-01-01','1');
insert into tb_book(id, name, publish_time, status)values(null, 'java从入门到精通','2020-02-02','0');

create table `tb_user`(
	`id` int(11) auto_increment,
	`name` varchar(50) default null,
	primary key(`id`)
)engine=myisam default charset=utf8;
insert into tb_suer(id, name) values(null, 'stefan');
insert into tb_suer(id, name) values(null, 'napoleon');
  • 2. The write lock case
      obtains the write lock of the tb_book table
lock table tb_book write;

  Perform query operations

select * from tb_book;

  The execution result is as follows: the

  query operation is executed successfully.
  Perform an update operation

update tb_book set name = 'java编程思想(第二版)' where id = 1;

  The execution result is as follows: the

  update operation is executed successfully.
  Therefore, the mutual compatibility of lock modes is shown in the

  table: as can be seen from the above table:

1). The read operation of the MyISAM table will not block other users' read requests to the same table, but will block the write request to the same table;
2), the write operation of the MyISAM table will block other users from the same table Read and write operations;

  In short, the read lock will block writing, but it will not block reading. The write lock will block both reading and writing. In addition, MyISAM's read-write lock scheduling is write-first, which is why MyISAM is not suitable as a storage engine for write-primary tables, because after the write lock, other threads cannot do any operations, and a large number of updates will make it difficult to query Get the lock, which causes permanent blocking.

  • 3. Check the contention of the lock
show open tables;

  The execution results are as follows:

  • In_user : The number of times the table is currently used by the query. If the number is zero, the table is open, but it is not currently being used.
  • Name_locked : Whether the table name is locked. Name lock is used to cancel or rename the table and other operations.
show status like 'Table%';

  The execution results are as follows:

  Table_locks_immediate : Refers to the number of times that table-level locks can be obtained immediately, each time the lock is obtained immediately, the value is increased by 1
  Table_locks_waited : Refers to the number of times that the table-level lock cannot be obtained immediately and need to wait, and the value is increased by 1, this value High indicates that there is more serious table-level lock contention.

5. InnoDB row lock

  The characteristics of row locks: bias towards the InnoDB storage engine, high overhead, slow locking; deadlocks will occur; locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is the highest. There are two biggest differences between InnoDB and MyISAM: First, it supports transactions; instead, it uses row-level locks .
  A transaction is a logical processing unit composed of a set of SQL statements. A transaction has the following four characteristics, referred to as transaction ACID attributes for short.

  Problems caused by concurrent transaction processing

  • 1. Transaction isolation level
      In order to solve the above-mentioned increased transaction concurrency questions, the database provides a certain transaction isolation mechanism to solve this problem. The stricter the database transaction isolation, the smaller the concurrency side effects, but the greater the price paid, because transaction isolation essentially uses transactions to be "serialized" to a certain extent, which is obviously in contradiction with "concurrency".
      There are four database isolation levels, from low to high, they are Read uncommitted, Read committed, Repeatable read, and Serializable. These four levels can solve the problems of dirty write, dirty read, non-repeatable read, and phantom read one by one.

      It should be noted here: √ means it may appear, x means it will not appear. The default isolation level of MySQL database is Repeatable read, the view method is:
show variables like 'tx_isolation'; 

  The execution result is as follows:

  We should create a table lock, as follows:

create table test_innodb_lock(
	id int(11),
	name varchar(16),
	sex varchar(1)
)engine=innodb default charset=utf8;
insert into test_innodb_lock values(1, '100', '1');
insert into test_innodb_lock values(3, '3', '1');
insert into test_innodb_lock values(4, '400', '0');
insert into test_innodb_lock values(5, '500', '1');
insert into test_innodb_lock values(6, '600', '0');
insert into test_innodb_lock values(7, '700', '0');
insert into test_innodb_lock values(8, '800', '1');
insert into test_innodb_lock values(9, '900', '1');
insert into test_innodb_lock values(10, '200', '0');
create index idx_test_innodb_lock_id on test_innodb_lock(id);
create index idx_test_innodb_lock_name on test_innodb_lock(name);
  • 2. The basic demonstration of row lock
      first turn off the automatic submission function
set autocommit = 0;

  The execution results are as follows:

  all data can be queried normally

select * from test_innodb_lock;

  The execution result is as follows:

  query the data with id 3;

select * from test_innodb_lock where id = 3;

  The execution result is as follows:

  update the data with id 3;

update test_innodb_lock set name = 'A1' where id = 3;

  The execution results are as follows:

  Submit the transaction through commit, unblock, and update normally

commit;

  The execution results are as follows: It is

  not difficult to see that the operations are all the data of the same row. Next, the data of different rows will be demonstrated;

update test_innodb_lock set name = 'B1' where id = 3;
update test_innodb_lock set name = 'C1' where id = 5;

  The execution results are as follows:

  • 3. If no index row lock is upgraded to table lock,
      if data is not retrieved through index conditions, InnoDB will lock all records in the table, and the actual effect is the same as table lock. View the index of the current table
show index from test_innodb_lock;

  The execution results are as follows:

  first turn off the automatic commit of the transaction

set autocommit = 0;

  The execution results are as follows:

  Next execute the update statement

update test_innodb_lock set sex = '2' where name = 400;
update test_innodb_lock set sex = '2' where id = 9;

  Next execute the update statement and

  finally commit the transaction

commit;

  Next execute the update statement

  • 4. Gap lock hazards.
      When we use range conditions instead of equal conditions to retrieve data and request shared or exclusive locks, InnoDB will lock the existing data that meets the conditions; for the key values ​​in the condition range but not The existing record is called "gap (GAP)", and InnoDB will also lock this "gap". This lock mechanism is the so-called gap lock (Next-key lock). details as follows:
  • 5. InnoDB row lock contention
show status like 'innodb_row_lock%';

  The execution results are as follows:

  • Innodb_row_lock_current_waits : the number of locks currently waiting
  • Innodb_row_lock_time : The total length of time that the system is locked since the start of the system
  • Innodb_row_lock_time_avg : the average time spent in each wait
  • Innodb_row_lock_time_max : The time it takes to wait for the longest time since the system was started
  • Innodb_row_lock_waits : The total number of waits since the system was started up to now

  When the number of waiting is high, and the length of each waiting is not small, we need to analyze why there are so many waiting in the system, and then proceed to formulate an optimization plan based on the analysis results.
  In short, because the InnoDB storage engine implements row-level locking, although the performance loss in the implementation of the locking mechanism may be higher than that of table locks, it is far superior to MyISAM table locks in terms of overall concurrent processing capabilities. of. When the system concurrency is high, the overall performance of InnoDB will have obvious advantages compared with MyISAM. However, InnoDB's row-level lock also has its fragile side. When we use it improperly, the overall performance of InnoDB may not only be higher than that of MyISAM, but may even be worse. Next are optimization suggestions:

  • As far as possible, all data retrieval can be completed through the index, to avoid the upgrade of non-indexed row locks to table locks
  • Design the index reasonably to minimize the scope of the lock
  • Minimize index conditions and index ranges as much as possible to avoid gap locks
  • Try to control the size of the transaction, reduce the amount of locked resources and the length of time
  • Use low-level transaction isolation as much as possible (but need to meet the requirements at the business level).

Two, commonly used SQL skills

1. SQL execution order

  The process of writing a statement is as follows:

select distinct <select list>
from <left_table> <join_type>
join <right_table> on <join_condition>
where <where_condition>
group by <grop_by_list>
having <having_condition>
order by <order_by_condition>
limit <limit_params>;

  The execution process is as follows:

from <left_table>
on <join_condition>
<join_type> join <right_table>
where <where_condition>
group by <grop_by_list>
having <having_condition>
select distinct <select list>
order by <order_by_condition>
limit <limit_params>;

2. Various functions in MySQL

  Regular expression refers to a single string used to describe or match a series of strings that conform to a certain syntax rule.

  The specific commands are as follows:

select * from emp where name regexp '^T';
select * from emp where name regexp '2$';
select * from emp where name regexp '[uvw]';

  Next is a series of functions such as mathematical summation in MySQL, specifically as follows:

  string function, specifically as follows:

  date function, specifically as follows:

  finally, MySQL aggregate function, specifically as follows:

to sum up

  In the previous article, we introduced application optimization , query cache optimization in mysql, memory management optimization in mysql, and concurrency parameter adjustment in mysql. This article introduces you to the locks in MySQL, including the contention of locks and the related content of row locks and table locks. In addition, I introduce you to some commonly used techniques in SQL, including the order in which SQL statements are executed and the major functions in SQL. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/111715323
Recommended