mysql common operation statistics-1

1. Query the deadlock situation to solve the lock table problem

        After our database table is locked, we can execute the following sql to query which thread is occupied

select * from information_schema.innodb_trx

        At this time, we find the id under the field trx_mysql_thead_id and kill him .

2. Modify sql in batches through stored procedures

        When we need to modify certain data in batches, it is more cumbersome to modify them one by one. At this time, we can solve the problem by creating a stored procedure.

-- 创建存储过程
CREATE PROCEDURE test1114(IN mName VARCHAR(255))
BEGIN
			-- 定义变量
			DECLARE s int DEFAULT 0;
			DECLARE d varchar(255);
			DECLARE c varchar (255);
			DECLARE m varchar(255);
			-- 定义游标,并将sql结果集赋值到游标中
			DECLARE report CURSOR FOR SELECT ds.deviceId,ds.`code`,ds.moduleId FROM deviceSupply ds 
																	WHERE ds.typeDictId = (
																		SELECT dic.dictId from dictInfo dic 
																			WHERE dic.superId = (
																				SELECT d.dictId FROM dictInfo d 
																					WHERE d.`name` = mName
																				)
																		);
			-- 声明当游标遍历完后将标志变量置成某个值
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET s=1;
	-- 打开游标
	open report;
			-- 将游标中的值赋值给变量,
			#注意:变量名不要和返回的列名同名,变量顺序要和sql结果列的顺序一致
				fetch report into d,c,m;
			-- 当s不等于1,也就是未遍历完时,会一直循环
			while s<>1 do
				UPDATE modbusPointPosition set modbusPointPosition.deviceId = d 
				WHERE modbusPointPosition.moduleId = m AND modbusPointPosition.deviceId = c;
					-- 将游标中的值再赋值给变量,供下次循环使用
					fetch report into  d,c,m;
			-- 当s等于1时表明遍历以完成,退出循环
			end while;
	-- 关闭游标
	close report;
END;

3. Call the created stored procedure

CALL test1114("1121"); //Stored procedure name, if there are parameters, pass them in brackets

4. Perform deduplication operation on a certain field in sql

        The distinct keyword is used here. Its function is to deduplicate data.

SELECT distinct ds.deviceId,ds.deviceType FROM deviceSupply ds

Like this, you only need to add the keyword before the corresponding field to complete the deduplication operation. Note : it can only be placed at the beginning when using it.

Guess you like

Origin blog.csdn.net/zjb1697922408/article/details/127705380