Table of contents
5. MySQL's official website description about locks
A mysql FAQ
1. Server returns invalid timezone caused by wrong time zone
Solution: mysql> set global time_zone= '+8:00';
Production configuration generally adds a time zone to the url, such as:
jdbc:mysql://ip:port/xk?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
2. Data too long for column table field definition is too short, increase the field length or type
alter table table_name modify column_name varchar(100) comment 'check type';When the table is large, it is recommended to use the gh-ost method for lock-free table update; or the temporary table method?
3.1064 syntax
If you execute the stored procedure: escape the content
delimiter // stored procedure //If it is a sql statement: it is generally caused by special characters
4 ORA-00907 error in ibatis Use attribute plus [] to solve (ids is a comma-separated string)
<iterate property="ids" conjunction="," open="(" close=")">
#ids[]#
</iterate>
Two, commonly used
String concatenation:
update table_name set column_name=concat(column_name,'xxxx') where id ='xx';
date formatting
date_format(create_time, '%Y-%m-%d %H:%i:%S') create_time
create_time datetime default now() comment 'Creation time', create_by varchar(60) default 'system' not null comment 'creator', update_time datetime default now() on update now() comment 'update time', update_by varchar(60) comment 'update founder'
Current time minus seconds minutes, hours
select * from check_config where create_time<=now() -interval 6 secondselect * from check_config where create_time<=now() -interval 6 minuteselect * from check_config where create_time<=now() -interval 6 hour
tips:
If you compare numbers in mybatis mybatis-plus xml, it is recommended to use .toString()
<if test="status!=null and status=='0'.toString()"> status=#{status} </if>
Combined class is judged by size <if test="xxlist!=null and xxlist.size()>0"></if>
Character set view
show variables like '%char%';
Check the number of connections. If you encounter insufficient connections, you can use jstack -l pid > /tmp/xx.txt to download: sz /tmp/xx.txt to see if the specific thread pool is too large
#Currently all connections show full processlist; #Query the maximum number of connections show variables like 'max_connections'; -- Set the maximum number of connections, set as needed set global max_connections=200; show variables like '%max_connections%';
Modify table fields or delete
alter table table_name add column xx varchar(200) comment 'check model' after xx0; alter table table_name rename column xx to xx2;alter table table_name drop xx;
Index creation or deletion
create index idx_xx on table_name(xx); create unique index uq_xx on table_name(xx);drop index idx_xx on table_name;
insert statement
Ignore the insert, and ignore it if a primary key conflict is encountered INSERT IGNORE INTO table_name ...Repeated updates, as few as possible inserting a large amount of data will cause table locks
INSERT INTO check_config on duplicate key update xx=values(xx)insert into ... select insert method (too much data leads to table lock, a small amount of data backup can be used)
insert into xx select * from xx2 on duplicate key update column_name=values(column_name);
Special symbol escape processing: use <![CDATA[content]]> in mybatis and mybati-splus
idea simplifies sql shortcut keys (the premise is that it can be configured through the navigation bar on the right side of idea, if you encounter mysql-connector problems, you can download them first through maven and then solve them by adding them locally) as shown in the figure
Similar to the shortcut key commands that can be used in plsql: such as SF space, it will generate select * from
Organizing tables: (regular delete data cannot clean up the data stored in the data storage, but you can use the structuring table statement, but it is recommended not to operate less, you can search for keywords to query the problems introduced by structuring tables) For example, it may be useful to clean up historical data
optimize table user_address;
delete table
truncate table xx; drop table if exists xx;
Check the lock
show engine innodb status; show status like 'innodb_row_lock%'; select * from information_schema.innodb_trx; SELECT * from information_schema.`PROCESSLIST`;
mybati-plus integrates springboot maven
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
3. Cautions
Lock table problem: It is not recommended to use the update xx select xx from xx statement on the main business table to update
When a certain in field is updated, the less the primary key content control, the better, and try not to exceed 200-300?: update xx set xx=xxx where id in() Syntax The less data in in, the better, and more will cause lock upgrade problems?
Insert leads to improper insertion and also causes deadlock problems (unique index and joint index). In addition, some large transactions that have not been submitted for repeated consumption and consumption will occasionally cause insert deadlock problems. For insert deadlock problems, you can refer to other articles on the Internet or the official website of mysql.
Check the lock timeout configuration: SHOW VARIABLES LIKE 'innodb_lock_wait_timeout';
Index: Commonly used fields, it is recommended to increase the status value to distinguish data, not as many as possible Make a "trade-off between time and space" Time for space, space for time
In terms of query efficiency: avoid calculations on index columns, combine index leftmost matching rules, and add indexes to other indexes that should be added
Mysql query back to the table: set the primary key through the index, and find the specific data through the primary key; if the query column is an index column, then it is not necessary to set it as data twice through the query back to the table (that is, the part of the explain explanation plan in the index coverage index optimization)
Use the following test code to see the specific situation:
SELECT user_name FROM user_address where user_name='u2'; SELECT * FROM user_address where user_name='u2';
Do not go back to the table query, direct index coverage (Using index)
Index coverage is not used, and the table is queried
Special primary key id: if it is an auto-incrementing primary key, it is both data continuous and physical continuous
If there is a non-auto-increment primary key, there will be page splitting : for details, please refer to other articles
Four, slow sql positioning
Mainly to open the slow sql log, and if you know where it is, use explain to explain and execute it to see the specific index
windows environment
show variables like '%slow_query_log%';
set global slow_query_log = on;
# DESKTOP-R3DF1QE-slow.log
set global slow_query_log_file="C:/data/mysql/slow_query_log.log";
show variables like '%long_query_time%';
# 10.000000 默认10秒
set global long_query_time = 1.000000;
Such as slow sql test of local query:
select sleep(2);
C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe, Version: 8.0.18 (MySQL Community Server - GPL). started with:
TCP Port: 3306, Named Pipe: MySQL
Time Id Command Argument
# Time: 2022-09-28T05:45:03.937414Z
# User@Host: root[root] @ localhost [127.0.0.1] Id: 8
# Query_time: 11.003896 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 1
use xk;
SET timestamp=1664343892;
/* ApplicationName=IntelliJ IDEA 2022.1 */ select sleep(11);
# Time: 2022-09-28T08:44:23.059353Z
# User@Host: root[root] @ localhost [127.0.0.1] Id: 16
# Query_time: 2.001915 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 1
SET timestamp=1664354661;
/* ApplicationName=IntelliJ IDEA 2022.1 */ select sleep(2);
linux environment
After logging out, log in again to view
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> show variables like '%slow_query_log%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/localhost-slow.log |
+---------------------+-----------------------------------+
mysql> set global slow_query_log = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%long_query_time%';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
mysql> set global long_query_time = 1.000000;
Query OK, 0 rows affected (0.00 sec)
mysql> exit;
再次进入
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> show variables like '%long_query_time%';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
mysql> show variables like '%slow_query_log%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /var/lib/mysql/localhost-slow.log |
+---------------------+-----------------------------------+
2 rows in set (0.00 sec)
If it is some cloud platform, it will have the function of exporting mysql files
How much is considered slow SQL depends on the actual business
5. MySQL's official website description about locks
MySQL :: MySQL 8.0 Reference Manual :: 15.7.1 InnoDB Locking