【问题标题】:Same SQL runs fast in QUERY but very slowly in SP?相同的 SQL 在 QUERY 中运行速度很快,但在 SP 中运行速度很慢?
【发布时间】:2021-05-02 23:09:39
【问题描述】:

我曾尝试在变量或参数之前添加或删除“@”,但没有任何反应。

查询

 start transaction;
   set @recordClient = (select ClientId  from by_test_db1.recordcd where SN = 'abc' );
   set @logClient    = (select ClientId  from by_test_db1.log      where SN = 'abc' );
   select concat(@recordClient,@logClient);
 commit;

SP

delimiter $$
create procedure TEST(newSN varchar(50))
begin 
     start transaction;
        set @recordClient = (select ClientId  from by_test_db1.recordcd where SN = newSN );
        set @logClient    = (select ClientId  from by_test_db1.log      where SN = newSN );
        select concat(@recordClient,@logClient);
     commit;
end $$
delimiter ;

call TEST('abc');

MySQL 版本 5.7

recordcd表中有1亿行,QUERY运行得又快又好,但是SP 运行太慢以至于超时并报错

错误代码:2013。在查询期间丢失与 MySQL 服务器的连接

我尝试了很多方法,但都没有奏效,我不知道为什么会出现如此荒谬的情况,我什至不知道如何寻找这种情况的答案。

【问题讨论】:

标签: mysql stored-procedures transactions


【解决方案1】:

添加索引

INDEX(SN)  -- on both tables

简化查询

SELECT  CONCAT(
           ( select ClientId  from by_test_db1.recordcd where SN = 'abc' ),
           ( select ClientId  from by_test_db1.log      where SN = 'abc' ) );

【讨论】:

    猜你喜欢
    • 2015-11-20
    • 2019-11-18
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多