在MySQL中调用存储过程需要永远执行

时间:2015-11-09 09:10:32

标签: mysql stored-procedures innodb

我有一个我试图调用的存储过程,并且需要永远执行。我不知道出了什么问题。另一个数据库中的类似存储过程执行完美。我不熟悉MySQL Workbench,所以我不知道数据库设置是否不同或类似。

以下是我的存储过程:

CREATE
DEFINER = `admin`@`%`
PROCEDURE `calculate_daily_coil_moved_by_crane_data`()

BEGIN
    set @curr_date = curdate();
    set @pre_date = date_add(curdate(), interval -1 day);
    set @a_shift_start_ts = concat(@pre_date, ' 06:00:00');
    set @a_shift_end_ts = concat(@pre_date, ' 13:59:59');
    set @b_shift_start_ts = concat(@pre_date, ' 14:00:00');
    set @b_shift_end_ts = concat(@pre_date, ' 21:59:59');
    set @c_shift_start_ts = concat(@pre_date, ' 22:00:00');
    set @c_shift_end_ts = concat(@curr_date, ' 05:59:59');

    SELECT  @curr_date,
            @pre_date,
            @a_shift_start_ts,
            @a_shift_end_ts,
            @b_shift_start_ts,
            @b_shift_end_ts,
            @c_shift_start_ts,
            @c_shift_end_ts;


    #SET DATA
    insert into daily_coil_move_by_crane_data_for_report (crane_id, crane_name, date, a_shift, b_shift, c_shift)
    select cr.id, cr.name, @pre_date, 0, 0, 0
    from yms_phase3.crane cr
    where active = 1
    order by cr.name;

    #----------------------------------------------------------------------------------------------------
    #--> COILS MOVED BY CRANE A Shift <--
    #----------------------------------------------------------------------------------------------------
    SET @shift = 'A';

    #FETCH ROW DATA
    update daily_coil_move_by_crane_data_for_report
    set a_shift = ifnull((select COUNT(*)
                            FROM yms_phase3.workorder_history in_data
                            where in_data.crane_id = daily_coil_move_by_crane_data_for_report.crane_id
                                and current_execution_status IN (6 , 7)
                                and in_data.pick_ts between @a_shift_start_ts and @a_shift_end_ts
                            group by in_data.crane_name), 0)
    where (a_shift is null or a_shift = 0);

    #----------------------------------------------------------------------------------------------------
    #--> COILS MOVED BY CRANE B Shift <--
    #----------------------------------------------------------------------------------------------------
    SET @shift = 'B';

    #FETCH ROW DATA
    update daily_coil_move_by_crane_data_for_report
    set b_shift = ifnull((select COUNT(*)
                            FROM yms_phase3.workorder_history in_data
                            where in_data.crane_id = daily_coil_move_by_crane_data_for_report.crane_id
                                and current_execution_status IN (6 , 7)
                                and in_data.pick_ts between @b_shift_start_ts and @b_shift_end_ts
                            group by in_data.crane_name), 0)
    where (b_shift is null or b_shift = 0);

    #----------------------------------------------------------------------------------------------------
    #--> COILS MOVED BY CRANE C Shift <--
    #----------------------------------------------------------------------------------------------------
    SET @shift = 'C';

    #FETCH ROW DATA
    update daily_coil_move_by_crane_data_for_report
    set c_shift = ifnull((select COUNT(*)
                            FROM yms_phase3.workorder_history in_data
                            where in_data.crane_id = daily_coil_move_by_crane_data_for_report.crane_id
                                and current_execution_status IN (6 , 7)
                                and in_data.pick_ts between @c_shift_start_ts and @c_shift_end_ts
                            group by in_data.crane_name), 0)
    where (c_shift is null or c_shift = 0);

    #----------------------------------------------------------------------------------------------------
    #INSERT ALL CRANE ENTRY
    insert into daily_coil_move_by_crane_data_for_report (crane_id, crane_name, date, a_shift, b_shift, c_shift)
    select -1, 'ALL', @pre_date, SUM(a_shift), sum(b_shift), sum(c_shift)
    from daily_coil_move_by_crane_data_for_report
    where date = @pre_date
    group by date;

    #UPDATE TOTAL
    update daily_coil_move_by_crane_data_for_report
    set total_coils_moved = (a_shift + b_shift + c_shift)
    where date = @pre_date;
END

还尝试使用以下内容从Java执行查询:

jdbcTemplate.execute("CALL calculate_daily_coil_moved_by_crane_data;");

但它给了我以下例外:

java.sql.SQLException: Lock wait timeout exceeded

我可以采取哪些解决方法来解决这个问题?

1 个答案:

答案 0 :(得分:0)

Please try and edit the configuration file, also search for the same here on stack. 检查时有一定的可能性,

  1. 检查并编辑硬盘驱动器上的配置文件,增加缓存容量和默认值,因为默认值是KB,分配的内存非常少,执行这么大的程序至少应该是一些MB。

  2. 增加连接字符串超时,即以秒为单位设置正确的时间。默认情况下它是60秒,这对于执行这样的过程来说非常少,我认为在c#中至少我们将它设置为'0'秒,这意味着在执行查询之前它不会超时。

  3. 如果有任何左连接/内部查询请尝试检查内部连接是否产生相同的输出?因为内部连接比左连接或右连接快。

  4. 添加索引,正确映射外键引用,以便更快地执行查询。 希望它有效。