Unusual: MySQL

PL/SQL:
I wouldn’t recommend to use the following construction:

IF EXISTS (SELECT 1 FROM `table_a` WHERE `id` = `_ID`) THEN
    UPDATE `table_a` SET `value` = 1 WHERE `id` = `_ID`;
ELSE
    INSERT INTO `table_a` [...];
END IF;

In my case there was a trigger (on table_a) who additionaly modifed “table_b”. When I changed the engine of table_a from MyISAM into InnoDB then I saw plenty of processes in state (Waiting for table level lock). The tables were locked (it was in production environment – huge traffic, it was not observable in the test environment). The solution was to replace this “EXISTS” into query and variable…

SELECT IF(COUNT(*)>0, TRUE, FALSE) FROM `table_a` WHERE `id` = `_ID`
    INTO EXISTS_v;
IF EXISTS_v IS TRUE THEN
     UPDATE [...];
ELSE
     INSERT [...];
  1. No comments yet.

  1. No trackbacks yet.