数据重新排列循环超时服务器

时间:2014-08-10 03:50:19

标签: mysql loops timeout

我有以下循环:

  1. 在tableA中查找具有特定值(@centre)
  2. 的行
  3. 将@centre中的值复制到tableB
  4. 中的指定列
  5. 在@centre之前找到行并将它们添加到tableB(通过更新)
  6. 查找@centre之后的行并将它们添加到tableB(通过更新)
  7. 循环的每次迭代都会在tableB中生成一个新行。它做了应有的一切。然而,该功能需要永久(> 10分钟)来执行并且超时服务器。表A中有大约80k行,并且作为循环的结果,在tableB中生成~10k行。

    drop procedure if exists test_loop; 
    DELIMITER $$
    Create procedure test_loop ()
    begin 
    
    DECLARE b1Tr varchar(10);
    DECLARE b1S varchar(10);
    DECLARE b1Ty varchar(10);
    DECLARE b1US varchar(10);
    DECLARE b1LS varchar(10);
    DECLARE b1RC decimal(11,10);
    DECLARE b1RCo varchar(10);
    DECLARE ba1Tr varchar(10);
    DECLARE ba1S varchar(10);
    DECLARE ba1Ty varchar(10);
    DECLARE ba1US varchar(10);
    DECLARE ba1LS varchar(10);
    DECLARE ba1RC decimal (11,10);
    DECLARE ba1RCo varchar(10);
    DECLARE ba2RC varchar(10);
    
    DECLARE final datetime;
    DECLARE centre datetime;
    DECLARE x int;
    
    SET x = 0;
    
    SET @final = (select MQLTime from tableA where BarSize<=1
    order by mqltime desc limit 1);
    
    REPEAT
    
    SET @centre = (select MQLTime from tableA where BarSize<=1 
    order by mqltime asc limit x,1);
    
    SET @b1Tr = (select Trend from tableA where MQLtime < @centre order by mqltime desc limit 1);
    SET @b1S = (select Size from tableA where MQLtime < @centre order by mqltime desc limit 1);
    SET @b1Ty = (select Type from tableA where MQLtime < @centre order by mqltime desc limit 1);
    SET @b1US = (select US from tableA where MQLtime < @centre order by mqltime desc limit 1);
    SET @b1LS = (select LS from tableA where MQLtime < @centre order by mqltime desc limit 1);
    SET @b1RC = (select close from tableA where MQLtime = @centre order by MQLTime desc limit 1) - 
                (select close from tableA where MQLtime < @centre order by MQLTime desc limit 1);
    
    SET @ba1Tr = (select Trend from tableA where MQLtime > @centre order by mqltime desc limit 1);
    SET @ba1S = (select Size from tableA where MQLtime > @centre order by mqltime desc limit 1);
    SET @ba1Ty = (select Type from tableA where MQLtime > @centre order by mqltime desc limit 1);
    SET @ba1US = (select US from tableA where MQLtime > @centre order by mqltime desc limit 1);
    SET @ba1LS = (select LS from tableA where MQLtime > @centre order by mqltime desc limit 1);
    SET @ba1RC = (select close from tableA where MQLtime > @centre order by MQLTime desc limit 1) - 
                (select close from tableA where MQLtime = @centre order by MQLTime desc limit 1);
    SET @ba2RC = (select close from tableA where MQLtime > @centre order by MQLTime asc limit 1,1) - 
                (select close from tableA where MQLtime = @centre order by MQLTime desc limit 1);
    
    if @b1RC>0 then set @b1RCo = 'Up'; 
    elseif @b1RC<0 then set @b1RCo='Down'; 
    elseif @b1RC=0 then set @b1RCo='Flat';
    end if;
    
    if @ba1RC>0 then set @ba1RCo = 'Up'; 
    elseif @ba1RC<0 then set @ba1RCo='Down'; 
    elseif @ba1RC=0 then set @ba1RCo='Flat';
    end if;
    
    if @ba2RC>0 then set @ba2RCo = 'Up'; 
    elseif @ba2RC<0 then set @ba2RCo='Down'; 
    elseif @ba2RC=0 then set @ba2RCo='Flat';
    end if;
    
    INSERT IGNORE INTO tableB (MQLrefTime,dTr,dS,dTy,dUS,dLS) 
    select MQLTime,Trend,Size,Type,US,LS 
    from tableA where MQLTime = @centre 
    order by MQLTime asc limit 1; 
    
    update tableA_doji_model
    SET b1Tr = @b1Tr,
        b1S = @b1S,
        b1Ty = @b1Ty,
        b1US = @b1US,
        b1LS = @b1LS,
        b1RC = @b1RCo,
        ba1Tr = @ba1Tr,
        ba1S = @ba1S,
        ba1Ty = @ba1Ty,
        ba1US = @ba1US,
        ba1LS = @ba1LS,
        ba1RC = @ba1RCo,
        ba2RC = @ba2RCo
    order by mqlreftime desc limit 1;
    
    SET x=x+1;
    
    UNTIL @centre=@final
    END REPEAT;
    
    END $$
    DELIMITER ; 
    

    表A的一个例子:

          |colA | colB | colC | colD |
    row 1 |val1 | val2 | val3 | val4 |
    row 2 |val5 | val6 | val7 | val8 |
    row 3 |val9 |val10 |val11 |val12 |
    

    tableB(输出表)

    的示例
          |colA | colB | colC | colD | colE | colF | colG | colH | colI | colJ |
    row 1 |val5 | val2 | val3 | val4 | val6 | val7 | val8 |val10 |val11 |val12 |
    

    tableB中数据的重新排列代表了真实表格。

    任何人都可以看到循环超时服务器的任何原因(错误2013)?

0 个答案:

没有答案