Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

mysql - Same SQL runs fast in QUERY but very slowly in SP?

I had tried to add or remove the '@' before variables or params but nothing happened.

QUERY

 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 version 5.7

Through there are 100 million rows in the recordcd table ,the QUERY just ran fast and well, but the SP was running so slowly that it timed out and reported an error

Error Code: 2013. Lost connection to MySQL server during query

I tried many ways but none of them worked, I don't know why there is such a ridiculous situation, I don't even know how to search the answer to this situation.

question from:https://stackoverflow.com/questions/65947563/same-sql-runs-fast-in-query-but-very-slowly-in-sp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add indexes

INDEX(SN)  -- on both tables

Simplify query

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...