Bug #78040 UPDATE is changing incorrect field
Submitted: 12 Aug 2015 11:16 Modified: 23 Nov 2018 9:25
Reporter: Michael Prokopiv Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:mysqld Ver 5.6.23-ndb-7.4.4-cluster-com OS:Oracle Linux (Oracle Linux Server release 6.3)
Assigned to: CPU Architecture:Any
Tags: UPDATE PRIMARY

[12 Aug 2015 11:16] Michael Prokopiv
Description:
"UPDATE ST_Traces SET text=CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax) WHERE id = LastId;"
is changing id in the table
CREATE TABLE IF NOT EXISTS ST_Traces (
        id TIMESTAMP(6) NOT NULL COMMENT 'Trace date time' PRIMARY KEY,
        section VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'To separate parallel traces',
        text VARCHAR(1024) NOT NULL COMMENT 'Human meaning of the trace'
    ) ENGINE=MEMORY;

How to repeat:
DELIMITER $$
DROP PROCEDURE IF EXISTS RecalcRefCount $$
CREATE PROCEDURE RecalcRefCount() NOT DETERMINISTIC
BEGIN
    DECLARE LastId TIMESTAMP(6);
    DECLARE e            INT DEFAULT 0;
    DECLARE i            INT DEFAULT 0;
    
    SET @ProcedureName = 'RecalcRefCount';
    CREATE TABLE IF NOT EXISTS ST_Traces (
        id TIMESTAMP(6) NOT NULL COMMENT 'Trace date time' PRIMARY KEY,
        section VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'To separate parallel traces',
        text VARCHAR(1024) NOT NULL COMMENT 'Human meaning of the trace'
    ) ENGINE=MEMORY;

    INSERT ST_Traces SET section = @ProcedureName, text="Started!";
    
    INSERT ST_Traces SET section = @ProcedureName, text="Calcs BL_Values";

    SELECT id INTO LastId from ST_Traces where section = @ProcedureName and text="Calcs BL_Values";
    
    SELECT IFNULL(MIN(id),0), IFNULL(MAX(id),0) INTO @iCnt, @iMax FROM BS.BL_Values;
    REPEAT
      IF (@iCnt MOD 10000) = 1  THEN
         SELECT id from ST_Traces where section = @ProcedureName and text="Calcs BL_Values";
        UPDATE ST_Traces SET text=CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax) WHERE id = LastId;
         SELECT id  from ST_Traces where section = @ProcedureName and text=CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax);
        select @iCnt, LastId, CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax);
        #INSERT ST_Traces SET section = @ProcedureName, text=CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax);
        COMMIT;
      END IF;
      SET @iCnt = @iCnt + 1;
    UNTIL @iCnt > @iMax END REPEAT;

    COMMIT;
END $$
DELIMITER ;

mysql> CALL TMP_PROKOPIV.RecalcRefCount();
+----------------------------+
| id                         |
+----------------------------+
| 2015-08-12 11:00:28.603816 |
+----------------------------+
1 row in set (0.04 sec)

+----------------------------+
| id                         |
+----------------------------+
| 2015-08-12 11:00:28.631726 |
+----------------------------+
1 row in set (0.04 sec)

+-------+----------------------------+-----------------------------------------------+
| @iCnt | LastId                     | CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax) |
+-------+----------------------------+-----------------------------------------------+
|     1 | 2015-08-12 11:00:28.603816 | Calcs BL_Values 1 of 107874076                |
+-------+----------------------------+-----------------------------------------------+
1 row in set (0.04 sec)

Empty set (5.76 sec)

Empty set (5.76 sec)

+-------+----------------------------+-----------------------------------------------+
| @iCnt | LastId                     | CONCAT("Calcs BL_Values ",@iCnt," of ",@iMax) |
+-------+----------------------------+-----------------------------------------------+
| 10001 | 2015-08-12 11:00:28.603816 | Calcs BL_Values 10001 of 107874076            |
+-------+----------------------------+-----------------------------------------------+
1 row in set (5.76 sec)
[23 Nov 2018 9:25] MySQL Verification Team
We're sorry, but the bug system is not the appropriate forum for asking help on using MySQL products. Your problem is not the result of a bug.

Support on using our products is available both free in our forums at http://forums.mysql.com/ and for a reasonable fee direct from our skilled support engineers at http://www.mysql.com/support/

The problem of this logic in the stored procedure is this query:

   SELECT IFNULL(MIN(id),0), IFNULL(MAX(id),0) INTO @iCnt, @iMax FROM BS.BL_Values;

MIN() and MAX()) without GROUP BY would return unwanted results.

Thank you for your interest in MySQL.