我正在使用PHP插入MYSQL数据库。在表1上有一个触发器BEFORE INSERT。然后,我将BULK数据插入MYSQL table2。 table2的插入查询在PHP while循环中。以下是交易和触发的顺序:
START TRANSACTION
INSERT INTO TABLE1 <-TRIGGER below
while(INSERT INTO TABLE2) // Comment: One INSERT per row
COMMIT
------------- TRIGGER NOT WORKING -----------------
delimiter $$
CREATE
TRIGGER `sample`.`new_gm_sequence`
BEFORE INSERT ON `sample`.`frtgoodsmovement`
FOR EACH ROW
begin
declare wno varchar(20);
select concat(numberprefix, cast(slno+1 as char)) into wno
from frtnumbercontrol
where codetype = 'GMTY'
and code = new.gmtype
and new.gmdate between frdate and todate;
set new.gmno = wno;
update frtnumbercontrol set slno = slno+1
where codetype = 'GMTY'
and code = new.gmtype
and new.gmdate between frdate and todate;
end
$$
上述触发器中的 gmno
是要在table1(frtgoodsmovement)中自动生成的字段。它不是在生产中工作,而是在非生产中工作。 table2的字段结果是另一个表。
以下是截图:https://imgur.com/a/7zHXH45
在手动数据输入期间,GM No正在生成。可能是什么问题?
注意:INSERT为100条记录大约需要1秒。我应该加快速度吗?然后触发器将与PHP BULK INSERT一起使用?
答案 0 :(得分:0)
MySql Lock Table会解决这个问题吗? https://dev.mysql.com/doc/refman/5.5/en/lock-tables.html
LOCK TABLES
tbl_name [[AS] alias] lock_type
[, tbl_name [[AS] alias] lock_type] ...
lock_type:
READ [LOCAL]
| [LOW_PRIORITY] WRITE
UNLOCK TABLES